belkevich / reachability-ios

Flexible network reachability with blocks for iOS and OS X.
MIT License
68 stars 15 forks source link

Fixed issue that sometimes caused status block not to get called. #5

Closed pti closed 9 years ago

pti commented 9 years ago

This issue occurred in an app that called SCNetworkReachability.initWithHost: from application:didFinishLaunchingWithOptions:. In SCNetworkReachability.initWithReachabilityRef: the method SCReachabilityScheduler.initWithReachabilityRef: is called before setting statusChangesBlock with method observeStatusChanges:. In some cases SCReachabilityScheduler.callbackForReachabilityRef callback function could get called before observeStatusChanges: got called -> statusChangesBlock was nil and the callback got ignored. So the app didn't get notified of the network status until it changed again. This issue was quite easy to reproduce with an iPhone4 (7.1.2) with WiFi connection (no SIM card was inserted).

The fix was to pass the statusChangesBlock in the SCReachabilityScheduler.initWithReachabilityRef method.

belkevich commented 9 years ago

Hi! 1) Blocks in 'init' method looks not very good. Sorry)) 2) Your changes destroy backward compatibility with previous versions. Many users will be unhappy after update 3) There are better way to fix this bug. Just run 'observeChanges' block immediately if networks status is valid. Like in 'reachabilityStatus' method.

Thank you for the bug. I'll fix it

belkevich commented 9 years ago

Fixed. New release 2.0.3 is on cocoapods

pti commented 9 years ago

Hi, Thanks for the feedback. Unfortunately the fix done for 2.0.3 doesn't seem to fix the issue. If SCReachabilityScheduler.callbackForReachabilityRef is called before SCReachabilityScheduler.observeStatusChanges then the application never gets the first status event.

This happens in my tests about 50% of the time on the iPhone4 7.1.2.

Perhaps SCReachabilityScheduler could store the status in the case where callbackForReachabilityRef is called and statusChangesBlock is still nil. Then later when statusChangesBlock is set the stored status would be passed to the block immediately?

belkevich commented 9 years ago

Thanks for feedback. I'll check it

belkevich commented 9 years ago

Oh, there are still a bug. You are right! Hope I finally fix it. Check out version 2.0.4 on cocoapods