hpios icon indicating copy to clipboard operation
hpios copied to clipboard

What is MyObserver?

Open Liqiankun opened this issue 6 years ago • 4 comments

I purchased this book! Some of code in the book is really bad! wechatimg2 wechatimg1

Liqiankun avatar Apr 21 '18 06:04 Liqiankun

MyObserver is an example observer here. The complete details have been omitted for brevity.

A typical key-value observer requires the object to implement the method observeValue(forKeyPath:of:change:context:).

Let me know how can I help.

gvaish avatar Apr 22 '18 08:04 gvaish

wechatimg6 @gvaish

Liqiankun avatar Apr 22 '18 23:04 Liqiankun

Thanks for the errata. Will add these to the book.

The correct code should be:

-(id)init {
  //... omitted extraneous code for brevity
  //add observer
  [self.reachability startNotifier];
}

//self. is optional, self.reachability ≡ reachability
-(void)networkStatusChanged:(NSNotification *)notification {
  if(!reachability.isReachableViaWiFi) {
    self.networkOperationQueue.suspended = YES;
    [reachability stopNotifier];
  } else {
    self.networkOperationQueue.suspended = NO;
    [reachability startNotifier];
  }
}

gvaish avatar Apr 23 '18 04:04 gvaish

-(void)networkStatusChanged:(NSNotification *)notification {
  if(!reachability.isReachableViaWiFi) {
    self.networkOperationQueue.suspended = YES;
  } else {
    self.networkOperationQueue.suspended = NO;
  }
}

@gvaish You can not call [reachability startNotifier]!If you call it, it would not can networkStatusChanged : when the next time network is reachable!

Liqiankun avatar Apr 24 '18 07:04 Liqiankun