Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keypath injection #295

Closed
sdkdimon opened this issue Dec 16, 2014 · 6 comments
Closed

Keypath injection #295

sdkdimon opened this issue Dec 16, 2014 · 6 comments

Comments

@sdkdimon
Copy link

Hi, i have a AFHTTPRequestOperationManager that comes from pods dependency. AFHTTPRequestOperationManager has property an instance of NSOperationQueue - operationQueue. How i can inject operationQueue maxConcurrentOperationCount property?

So my solution is:

-(id)httpOperationManager{
    return [TyphoonDefinition withClass:[AFHTTPRequestOperationManager class] configuration:^(TyphoonDefinition *definition){
        [definition injectProperty:@selector(operationQueue) with:[self operationQueue]];
    }];
}
-(id)operationQueue{
    return [TyphoonDefinition withClass:[NSOperationQueue class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(maxConcurrentOperationCount) with:@(1)];
    }];
}

I am trying this implementation, i know that it is worng, because TyphoonInjectionByFactoryReference has no functionality to inject in it something, it is used vice versa.

-(id)httpOperationManager{
    return [TyphoonDefinition withClass:[AFHTTPRequestOperationManager class] configuration:^(TyphoonDefinition *definition){
        id operationQueue = [definition property:@selector(operationQueue)];
        [operationQueue injectProperty:@selector(maxConcurrentOperationCount) with:@(1)];
    }];
}
@alexgarbarev
Copy link
Contributor

Hi,
I'm not sure that I understand what you want to do with second code snippet.
I saw your first snippet, it's works and looks clear, but I can't understand the second snippet.
And if first one works for you, then what is the question?

@sdkdimon
Copy link
Author

I mean

-(id)httpOperationManager{
    return [TyphoonDefinition withClass:[AFHTTPRequestOperationManager class] configuration:^(TyphoonDefinition *definition){
     [definition injectProperty:@selector(maxConcurrentOperationCount) atKeypath:@"operationQueue.maxConcurrentOperationCount" with:@(1)];
    }];
}

In this case AFHTTPRequestOperationManager inside its init method has simple implementation of NSOperationQueue.

- (instancetype)initWithBaseURL:(NSURL *)url {
    self = [super init];
    if (!self) {
        return nil;
    }
............................................ SOME CODE HERE ............................................
self.operationQueue = [[NSOperationQueue alloc] init];
return self;
}

But what to do, if AFHTTPRequestOperationManager has a complex initialization of NSOperationQueue (this is just a guess)? Because AFHTTPRequestOperationManager is a library i am can not control of it part initialization configuration.
Current solution for me just recreate NSOperationQueue with all settings that i need, and inject it into AFHTTPRequestOperationManager.

@alexgarbarev
Copy link
Contributor

Ah, I see that you mean.
Yes, there is no short API for that, but I think it maybe possible via circular reference, try this:

  • manager
    inject property queue with: queue
  • queue
    with factory: manager selector: operationQueue
    configuration:
    Inject property max operations count here

I'll write the code later today, since I'm from mobile now

Îòïðàâëåíî ñ iPhone

16.12.2014, â 17:28, Dmitry [email protected] íàïèñàë(à):

I mean

-(id)httpOperationManager{
return [TyphoonDefinition withClass:[AFHTTPRequestOperationManager class] configuration:^(TyphoonDefinition *definition){
[definition injectProperty:@selector(maxConcurrentOperationCount) atKeypath:@"operationQueue.maxConcurrentOperationCount" with:@(1)];
}];
}
In this case AFHTTPRequestOperationManager inside its init method has simple implementation of NSOperationQueue.

  • (instancetype)initWithBaseURL:(NSURL *)url {
    self = [super init];
    if (!self) {
    return nil;
    }
    ............................................ SOME CODE HERE ............................................
    self.operationQueue = [[NSOperationQueue alloc] init];
    return self;
    }
    But what to do, if AFHTTPRequestOperationManager has a complex initialization of NSOperationQueue (this is just a guess)? Because AFHTTPRequestOperationManager is a library i am can not control of it part initialization configuration.
    Current solution for me just recreate NSOperationQueue with all settings that i need, and inject it into AFHTTPRequestOperationManager.


Reply to this email directly or view it on GitHub.

@alexgarbarev
Copy link
Contributor

Try this code:

- (id)httpOperationManager
{
    return [TyphoonDefinition withClass:[AFHTTPRequestOperationManager class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(operationQueue) with:[self operationQueue]];
    }];
}

- (id)operationQueue
{
    return [TyphoonDefinition withFactory:[self httpOperationManager] selector:@selector(operationQueue) parameters:nil configuration:^(TyphoonFactoryDefinition *definition) {
        [definition injectProperty:@selector(maxConcurrentOperationCount) with:@(1)];    
    }];
}

@sdkdimon
Copy link
Author

Thanks.

@jasperblues
Copy link
Member

@sdkdimon works? Can we close?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants