Skip to content

Commit

Permalink
Use AFNetworkReachabilityManager to compare consistency of notificati…
Browse files Browse the repository at this point in the history
…on posts and gets, instead of SCNetworkReachabilityRef.

And test case reference.
  • Loading branch information
pengwp committed Jul 25, 2018
1 parent ffbcabe commit 91cc666
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
5 changes: 0 additions & 5 deletions AFNetworking/AFNetworkReachabilityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)setReachabilityStatusChangeBlock:(nullable void (^)(AFNetworkReachabilityStatus status))block;

/**
Returns the reachability of current networkReachabilityManager.
*/
- (SCNetworkReachabilityRef)currentNetworkReachability;

@end

///----------------
Expand Down
21 changes: 9 additions & 12 deletions AFNetworking/AFNetworkReachabilityManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
NSString * const AFNetworkingReachabilityNotificationStatusItem = @"AFNetworkingReachabilityNotificationStatusItem";

typedef void (^AFNetworkReachabilityStatusBlock)(AFNetworkReachabilityStatus status);
typedef AFNetworkReachabilityManager * (^AFNetworkReachabilityStatusCallback)(AFNetworkReachabilityStatus status);

NSString * AFStringFromNetworkReachabilityStatus(AFNetworkReachabilityStatus status) {
switch (status) {
Expand Down Expand Up @@ -78,20 +79,21 @@ static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetwork
* a queued notification (for an earlier status condition) is processed after
* the later update, resulting in the listener being left in the wrong state.
*/
static void AFPostReachabilityStatusChange(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, AFNetworkReachabilityStatusBlock block) {
static void AFPostReachabilityStatusChange(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, AFNetworkReachabilityStatusCallback block) {
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
dispatch_async(dispatch_get_main_queue(), ^{
AFNetworkReachabilityManager *manager = nil;
if (block) {
block(status);
manager = block(status);
}
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSDictionary *userInfo = @{ AFNetworkingReachabilityNotificationStatusItem: @(status) };
[notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:(__bridge id)target userInfo:userInfo];
[notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:manager userInfo:userInfo];
});
}

static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info) {
AFPostReachabilityStatusChange(target, flags, (__bridge AFNetworkReachabilityStatusBlock)info);
AFPostReachabilityStatusChange(target, flags, (__bridge AFNetworkReachabilityStatusCallback)info);
}


Expand Down Expand Up @@ -210,14 +212,15 @@ - (void)startMonitoring {
}

__weak __typeof(self)weakSelf = self;
AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {
AFNetworkReachabilityStatusCallback callback = ^(AFNetworkReachabilityStatus status) {
__strong __typeof(weakSelf)strongSelf = weakSelf;

strongSelf.networkReachabilityStatus = status;
if (strongSelf.networkReachabilityStatusBlock) {
strongSelf.networkReachabilityStatusBlock(status);
}


return strongSelf;
};

SCNetworkReachabilityContext context = {0, (__bridge void *)callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL};
Expand Down Expand Up @@ -252,12 +255,6 @@ - (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus s
self.networkReachabilityStatusBlock = block;
}

#pragma mark -

- (SCNetworkReachabilityRef)currentNetworkReachability {
return self.networkReachability;
}

#pragma mark - NSKeyValueObserving

+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
Expand Down
16 changes: 7 additions & 9 deletions Tests/Tests/AFNetworkReachabilityManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,23 @@ - (void)testDomainReachabilityBlock {
}

- (void)testDifferentReachabilityManagerNotificationPostsAndGets {
[self verifyReachabilityNotificationPostsAndGetsConsistenceWithManager:self.domainReachability notificationManager:self.addressReachability];
[self verifyReachabilityNotificationPostsAndGetsConsistenceWithPostsManager:self.domainReachability getsManager:self.addressReachability];
}

- (void)testSameReachabilityManagerNotificationPostsAndGets {
[self verifyReachabilityNotificationPostsAndGetsConsistenceWithManager:self.addressReachability notificationManager:self.addressReachability];
[self verifyReachabilityNotificationPostsAndGetsConsistenceWithPostsManager:self.addressReachability getsManager:self.addressReachability];
}

- (void)verifyReachabilityNotificationPostsAndGetsConsistenceWithManager:(AFNetworkReachabilityManager *)currentManager notificationManager:(AFNetworkReachabilityManager *)noteManager {
BOOL isSameReachabilityManager = [currentManager isEqual:noteManager];
- (void)verifyReachabilityNotificationPostsAndGetsConsistenceWithPostsManager:(AFNetworkReachabilityManager *)postsManager getsManager:(AFNetworkReachabilityManager *)getsManager {
BOOL isSameReachabilityManager = [postsManager isEqual:getsManager];
[self expectationForNotification:AFNetworkingReachabilityDidChangeNotification
object:nil
handler:^BOOL(NSNotification *note) {
id currentReachabilityRef = (__bridge id)currentManager.currentNetworkReachability;
id noteReachabilityRef = note.object;
BOOL isSameReachabilityRef = [currentReachabilityRef isEqual:noteReachabilityRef];
return isSameReachabilityManager == isSameReachabilityRef;
BOOL isSameReachabilityManagerPostsAndGets = [note.object isEqual:getsManager];
return isSameReachabilityManager == isSameReachabilityManagerPostsAndGets;
}];

[noteManager startMonitoring];
[postsManager startMonitoring];

[self waitForExpectationsWithCommonTimeout];
}
Expand Down

0 comments on commit 91cc666

Please sign in to comment.