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

Creating multiple instances of AFHTTPRequestOperationManager causes reachability block to be overriden #3014

Closed
mobitar opened this Issue Sep 29, 2015 · 4 comments

Comments

Projects
None yet
3 participants
@mobitar
Contributor

mobitar commented Sep 29, 2015

When you create an instance of AFHTTPRequestOperationManager, in the initWithBaseURL: initializer, the operation manager is assigned the shared AFNetworkReachabilityManager:

self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];

This means if your app has multiple AFHTTPRequestOperationManager's, setting the networkReachabilityStatusBlock on the reachabilityManager property of the operation manager instance overrides the block you may have set on other instances of an operation manager's reachabilityManager.

This is undesired behavior, as ultimately, only the last block set will be executed in the event of a reachability change.

The solution would be to assign each AFHTTPRequestOperationManager its own unique instance of AFNetworkReachabilityManager, rather than using the sharedManager.

Possible solution: mobitar@4829c0b

mobitar added a commit to mobitar/AFNetworking that referenced this issue Sep 29, 2015

@tommypeps

This comment has been minimized.

Show comment
Hide comment
@tommypeps

tommypeps Oct 12, 2015

I think this doesn't a bug, really Singleton implementation define a unique instance for all the execution.
If you call the singleton on 2 instance different normally one must disappear.

If you want, instances

self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];

in appDelegate:

self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
[self.reachabulityManager startMonitoring];
.
.
.
[self.reachabilityManagersetReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
   [[NSNotificationCenter defaultCenter] postNotificationName:@"newConection"
                                                                             object: @{@"status":AFNetworkReachabilityStatus}];
}]

⬆️ Send notification at your other class for know when arrive conection

other Option, implemented only a instance but not Singleton

self.reachabilityManager =  [AFNetworkReachabilityManager managerForDomain:@"my domain"];
    [self.sharedManagerAF startMonitoring];
.
.
.
[self.reachabilityManagersetReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//TODO:
}]

tommypeps commented Oct 12, 2015

I think this doesn't a bug, really Singleton implementation define a unique instance for all the execution.
If you call the singleton on 2 instance different normally one must disappear.

If you want, instances

self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];

in appDelegate:

self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
[self.reachabulityManager startMonitoring];
.
.
.
[self.reachabilityManagersetReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
   [[NSNotificationCenter defaultCenter] postNotificationName:@"newConection"
                                                                             object: @{@"status":AFNetworkReachabilityStatus}];
}]

⬆️ Send notification at your other class for know when arrive conection

other Option, implemented only a instance but not Singleton

self.reachabilityManager =  [AFNetworkReachabilityManager managerForDomain:@"my domain"];
    [self.sharedManagerAF startMonitoring];
.
.
.
[self.reachabilityManagersetReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//TODO:
}]
@kcharwood

This comment has been minimized.

Show comment
Hide comment
@kcharwood

kcharwood Oct 27, 2015

Contributor

The AF Documentation does state:

The network reachability manager. AFURLSessionManager uses the sharedManager by default.

But I do see your point as well. It would require a call to startMonitoring for each manager you want to set up. Let me think about this some more.

Contributor

kcharwood commented Oct 27, 2015

The AF Documentation does state:

The network reachability manager. AFURLSessionManager uses the sharedManager by default.

But I do see your point as well. It would require a call to startMonitoring for each manager you want to set up. Let me think about this some more.

@kcharwood

This comment has been minimized.

Show comment
Hide comment
@kcharwood

kcharwood Oct 27, 2015

Contributor

Linked to #3015

Contributor

kcharwood commented Oct 27, 2015

Linked to #3015

@kcharwood

This comment has been minimized.

Show comment
Hide comment
@kcharwood

kcharwood Oct 27, 2015

Contributor

Closing this. Please direct future conversation to #3111

Contributor

kcharwood commented Oct 27, 2015

Closing this. Please direct future conversation to #3111

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