Why AFURLRequestSerialization use KVO to observe value changing?
Asked Answered
Z

0

7

After reading the source code of AFURLRequestSerialization, I found that the author uses AFHTTPRequestSerializerObservedKeyPaths and NSStringFromSelector to get multiple keys such as allowsCellularAccess and cachePolicy, and uses itself as the observer to observe the value changing of these keys:

[self addObserver:self
       forKeyPath:keyPath
          options:NSKeyValueObservingOptionNew
          context:AFHTTPRequestSerializerObserverContext];

Why doesn't he just override some setters and use setters to observe the value changing?

- (void)setAllowsCellularAccess:(BOOL)allowsCellularAccess {
    //here do something else
    _allowsCellularAccess = allowsCellularAccess;
}

Is there any benefits?

Zoara answered 29/8, 2017 at 3:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.