FBKVOController源碼剖析與學(xué)習(xí)

建議查看原文:http://www.itdecent.cn/p/4a3f9fe13e5a(不定時更新)

源碼剖析學(xué)習(xí)系列:(不斷更新)

1、FBKVOController源碼剖析與學(xué)習(xí)
2、MJRefresh源碼剖析與學(xué)習(xí)
3、YYImage源碼剖析與學(xué)習(xí)


FBKVOController是對KVO的封裝,本文會分為兩大部分:

一、針對FBKVOController進(jìn)行源碼解讀,剖析其封裝思路

二、針對源碼,抽取其精要,模仿學(xué)習(xí),變?yōu)榧河?/p>

優(yōu)勢

相對于原生 API 優(yōu)勢

1、可以以數(shù)組形式,同時對 model 的多個 不同成員變量進(jìn)行 KVO。

2、利用提供的 block,將 KVO 相關(guān)代碼集中在一塊,而不是四處散落。比較清晰,一目了然。

3、不需要在 dealloc 方法里取消對 object 的觀察,當(dāng) FBKVOController 對象 dealloc,會自動取消觀察。

使用

//1、在當(dāng)前類創(chuàng)建一個KVO的控制器,并且指明監(jiān)聽者為當(dāng)前類
// create KVO controller with observer
FBKVOController *KVOController = [FBKVOController controllerWithObserver:self];
self.KVOController = KVOController;

//2、監(jiān)聽對象
// observe clock date property
[self.KVOController observe:clock keyPath:@"date" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew block:^(ClockView *clockView, Clock *clock, NSDictionary *change) {
  // 更新UI
  // update clock view with new value
  clockView.date = change[NSKeyValueChangeNewKey];
}];

使用步驟很簡短,我們關(guān)鍵是理解里面的封裝。

FBKVOController

一、我們先看一下創(chuàng)建KVO controller實例的方法,以及銷毀方法--(生命周期)

#pragma mark Lifecycle - 
//1、
+ (instancetype)controllerWithObserver:(nullable id)observer
{
  return [[self alloc] initWithObserver:observer];
}
//2、初始化observer,并依據(jù)retainObserved值決定內(nèi)存策略
- (instancetype)initWithObserver:(nullable id)observer retainObserved:(BOOL)retainObserved
{
  self = [super init];
  if (nil != self) {
    _observer = observer;
    NSPointerFunctionsOptions keyOptions = retainObserved ? NSPointerFunctionsStrongMemory|NSPointerFunctionsObjectPointerPersonality : NSPointerFunctionsWeakMemory|NSPointerFunctionsObjectPointerPersonality;
    _objectInfosMap = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:NSPointerFunctionsStrongMemory|NSPointerFunctionsObjectPersonality capacity:0];
    //初始化互斥鎖
    pthread_mutex_init(&_lock, NULL);
  }
  return self;
}
//3、
- (instancetype)initWithObserver:(nullable id)observer
{
  return [self initWithObserver:observer retainObserved:YES];
}
//4、在dealloc注銷所有監(jiān)聽并且銷毀上面的互斥鎖
- (void)dealloc
{
  [self unobserveAll];
  pthread_mutex_destroy(&_lock);
}

總結(jié):1、NSPointerFunctionsStrongMemory創(chuàng)建了一個retain/release對象的集合,非常像常規(guī)的NSSet或NSArray。
NSPointerFunctionsWeakMemory使用等價的__weak來存儲對象并自動移除被銷毀的對象。
2、比較陌生的是 NSMapTable 。簡單來說,它與 NSDictionary 類似。不同之處是 NSMapTable 可以自主控制 key / value 的內(nèi)存管理策略。而 NSDictionary 的內(nèi)存策略是固定為 copy。當(dāng) key 為 object 時, copy 的開銷可能比較大!因此,在這里只能使用相對比較靈活的 NSMapTable。具體可以移步關(guān)于 NSMapTable

3、pthread_mutex:這是一種超級易用的互斥鎖,使用的時候,只需要初始化一個 pthread_mutex_t,用 pthread_mutex_lock 來鎖定, pthread_mutex_unlock 來解鎖,當(dāng)使用完成后,記得調(diào)用 pthread_mutex_destroy 來銷毀鎖

二、接下來看一下注冊監(jiān)聽對象的方法

- (void)observe:(nullable id)object keyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options block:(FBKVONotificationBlock)block
{
  NSAssert(0 != keyPath.length && NULL != block, @"missing required parameters observe:%@ keyPath:%@ block:%p", object, keyPath, block);
  if (nil == object || 0 == keyPath.length || NULL == block) {
    return;
  }
  //創(chuàng)建FBKVOInfo
  // create info
  _FBKVOInfo *info = [[_FBKVOInfo alloc] initWithController:self keyPath:keyPath options:options block:block];
  
  //利用FBKVOInfo觀察對象
  // observe object with info
  [self _observe:object info:info];
}

- (void)observe:(nullable id)object keyPaths:(NSArray<NSString *> *)keyPaths options:(NSKeyValueObservingOptions)options block:(FBKVONotificationBlock)block
{
  NSAssert(0 != keyPaths.count && NULL != block, @"missing required parameters observe:%@ keyPath:%@ block:%p", object, keyPaths, block);
  if (nil == object || 0 == keyPaths.count || NULL == block) {
    return;
  }
//遍歷每個keyPath,再遞歸
  for (NSString *keyPath in keyPaths) {
    [self observe:object keyPath:keyPath options:options block:block];
  }
}

使用斷言,提示用戶缺少必要參數(shù);
為了避免保留循環(huán),該block必須避免引用KVO控制器或其所有者。觀察已經(jīng)觀察到的對象keyPath或nil的結(jié)果是沒有操作的。

看一下FBKVOInfo的init方法

- (instancetype)initWithController:(FBKVOController *)controller
                           keyPath:(NSString *)keyPath
                           options:(NSKeyValueObservingOptions)options
                             block:(nullable FBKVONotificationBlock)block
                            action:(nullable SEL)action
                           context:(nullable void *)context
{
  self = [super init];
  if (nil != self) {
    _controller = controller;
    _block = [block copy];
    _keyPath = [keyPath copy];
    _options = options;
    _action = action;
    _context = context;
  }
  return self;
}

重寫init方法,把值分別賦值給屬性,對于為什么要if (nil != self),我認(rèn)為,當(dāng)應(yīng)用程序在更有限的內(nèi)存中運行,這是一個傳統(tǒng)的編碼建議。具體請看各位大神的回答--> In Objective-C why should I check if self = [super init] is not nil?

看一下觀察FBInfo的方法

- (void)_observe:(id)object info:(_FBKVOInfo *)info
{
  // lock
  pthread_mutex_lock(&_lock);
//1
  NSMutableSet *infos = [_objectInfosMap objectForKey:object];
//2
  // check for info existence
  _FBKVOInfo *existingInfo = [infos member:info];
  if (nil != existingInfo) {
    // observation info already exists; do not observe it again

    // unlock and return
    pthread_mutex_unlock(&_lock);
    return;
  }
//3
  // lazilly create set of infos
  if (nil == infos) {
    infos = [NSMutableSet set];
    [_objectInfosMap setObject:infos forKey:object];
  }

  // add info and oberve
  [infos addObject:info];

  // unlock prior to callout
  pthread_mutex_unlock(&_lock);
//4
  [[_FBKVOSharedController sharedController] observe:object info:info];
}

NSMutableSet是一個集合,它有幾個特點:
1、沒有順序,所有元素并非按照加入順序排列
2、重復(fù)元素只會添加一個,因此不用擔(dān)心里面的元素有重復(fù)
NSMapTable是比Dicitionary更強大的一個類。我們定義一個Person類,用來記錄人名,我們再創(chuàng)建一個Favourite類用來創(chuàng)建愛好對象,現(xiàn)在有Rose和Jack兩個人,分別的愛好是ObjC和Swift,人和愛好必須要用對象實現(xiàn),而且必須關(guān)聯(lián)起來在一個表里,以便我們進(jìn)行查詢和記錄。如果是以前的話需要自己建立一個Dictionary,把人名的name字段作為key,favourite的對象作為value。但是這樣有一個問題,如果突然某一天,我Person里面增加了個字段age,我這個表還要記錄每個人的年齡,供我以后來查詢不同年齡段的人統(tǒng)計使用呢?這下就很尷尬了,因為Dicitionary沒辦法實現(xiàn)我們要的這個效果,不過沒關(guān)系NSMapTable可以實現(xiàn),詳細(xì)請移步關(guān)于 NSMapTable

1、根據(jù)被觀察的object獲取其對應(yīng)的infos set。這個主要作用在于避免多次對同一個keyPath添加多次觀察,避免crash。因為每調(diào)用一次addObserverForKeyPath就要有一個對應(yīng)的removeObserverForKey。

2、從infos set判斷是不是已經(jīng)觀察此次info了,避免重復(fù)觀察。

3、如果infos為空,就把object當(dāng)做Key、infos當(dāng)做Object存入 NSMapTable,[infos addObject:info];再把info與infos關(guān)聯(lián)起來。這里聽起來可能有點別扭,我做個比喻:object是上面所說的是Rose,infos愛好ObjC,而info則是他的age

4、使用了單例,將觀察的信息及關(guān)系注冊到_FBKVOSharedController中,并且調(diào)用iOS自帶的KVO方法觀察

_FBKVOSharedController作為一個傳達(dá)者,用來接收和轉(zhuǎn)發(fā)KVO通知

- (void)observe:(id)object info:(nullable _FBKVOInfo *)info
{
  if (nil == info) {
    return;
  }

  // register info
  pthread_mutex_lock(&_mutex);
  [_infos addObject:info];
  pthread_mutex_unlock(&_mutex);
  //1
  // add observer
  [object addObserver:self forKeyPath:info->_keyPath options:info->_options context:(void *)info];

  if (info->_state == _FBKVOInfoStateInitial) {
    info->_state = _FBKVOInfoStateObserving;
  } else if (info->_state == _FBKVOInfoStateNotObserving) {
    // this could happen when `NSKeyValueObservingOptionInitial` is one of the NSKeyValueObservingOptions,
    // and the observer is unregistered within the callback block.
    // at this time the object has been registered as an observer (in Foundation KVO),
    // so we can safely unobserve it.
    [object removeObserver:self forKeyPath:info->_keyPath context:(void *)info];
  }
}

根據(jù)info的狀態(tài)來選擇添加或移除觀察者

1、代表所有的觀察信息都首先由FBKVOSharedController進(jìn)行接收,隨后進(jìn)行轉(zhuǎn)發(fā)。

//當(dāng)屬性的值發(fā)生變化時,自動調(diào)用此系統(tǒng)KVO方法


- (void)observeValueForKeyPath:(nullable NSString *)keyPath
                      ofObject:(nullable id)object
                        change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change
                       context:(nullable void *)context
{
  NSAssert(context, @"missing context keyPath:%@ object:%@ change:%@", keyPath, object, change);

  _FBKVOInfo *info;

  {
    // lookup context in registered infos, taking out a strong reference only if it exists
    pthread_mutex_lock(&_mutex);
    info = [_infos member:(__bridge id)context];
    pthread_mutex_unlock(&_mutex);
  }

  if (nil != info) {

    // take strong reference to controller
    FBKVOController *controller = info->_controller;
    if (nil != controller) {

      // take strong reference to observer
      id observer = controller.observer;
      if (nil != observer) {

        // dispatch custom block or action, fall back to default action
        if (info->_block) {
          NSDictionary<NSKeyValueChangeKey, id> *changeWithKeyPath = change;
          // add the keyPath to the change dictionary for clarity when mulitple keyPaths are being observed
          if (keyPath) {
            NSMutableDictionary<NSString *, id> *mChange = [NSMutableDictionary dictionaryWithObject:keyPath forKey:FBKVONotificationKeyPathKey];
            [mChange addEntriesFromDictionary:change];
            changeWithKeyPath = [mChange copy];
          }
          info->_block(observer, object, changeWithKeyPath);
        } else if (info->_action) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
          [observer performSelector:info->_action withObject:change withObject:object];
#pragma clang diagnostic pop
        } else {
          [observer observeValueForKeyPath:keyPath ofObject:object change:change context:info->_context];
        }
      }
    }
  }
}

根據(jù)info的block回調(diào)或者actioin等等進(jìn)行消息轉(zhuǎn)發(fā)。

至此,對FBKVOController的源碼剖析基本結(jié)束,下面是剖析后的學(xué)習(xí)

學(xué)習(xí)

1、NSHashTable
+ (instancetype)personWithName:(NSString *)name
{
    DWPerson *person = [[DWPerson alloc] init];
    person.name = name;
    //1、待會替換
    person.family = [[NSMutableArray alloc] init];
    [person.family addObject:person];
    return [person autorelease];
}
- (NSString *)description
{
    return [NSString stringWithFormat:@"%@'s retainCount is %lu",self.name,[self retainCount]];
}
- (void)dealloc
{
    self.name = nil;
    self.family = nil;
    [super dealloc];
}
int main(int argc, const char * argv[]) {
    @autoreleasepool {

        DWPerson *person_1 = [DWPerson personWithName:@"iOS"];
        DWPerson *person_2 = [DWPerson personWithName:@"swift"];
        DWPerson *person_3 = [DWPerson personWithName:@"android"];
        DWPerson *person_4 = [DWPerson personWithName:@"java"];
        DWPerson *person_5 = [DWPerson personWithName:@"ruby"];
        
        id list = @[person_1, person_2, person_3, person_4, person_5];
        NSLog(@"%@",list);

    }
    return 0;
}

打?。?/p>

(
"iOS's retainCount is 3",
"swift's retainCount is 3",
"android's retainCount is 3",
"java's retainCount is 3",
"ruby's retainCount is 3"
)

可以看出每個person的retainCount為3,因為family持有person,person持有family,如果我們運用NSHashTable,則可以完美解決此問題

我們替換1中的代碼,

+ (instancetype)personWithName:(NSString *)name
{
    DWPerson *person = [[DWPerson alloc] init];
    person.name = name;
    person.family = [NSHashTable hashTableWithOptions:NSHashTableWeakMemory];
    [person.family addObject:person];
    return [person autorelease];
}

打印:

(
"iOS's retainCount is 2",
"swift's retainCount is 2",
"android's retainCount is 2",
"java's retainCount is 2",
"ruby's retainCount is 2"

)
可看出,已解決循環(huán)引用

2、宏定義魔法

先看一下系統(tǒng)的KVO方法

[testPerson addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

這樣寫keyPath,如果age屬性不存在,也不會告知,導(dǎo)致后續(xù)的排查困難,但這種低級錯誤在FBKVOController不復(fù)存在,因為其使用了宏定義

FBKVOController中的宏定義

#define FBKVOKeyPath(KEYPATH) \
@(((void)(NO && ((void)KEYPATH, NO)), \
({ const char *fbkvokeypath = strchr(#KEYPATH, '.'); NSCAssert(fbkvokeypath, @"Provided key path is invalid."); fbkvokeypath + 1; })))

#define FBKVOClassKeyPath(CLASS, KEYPATH) \
@(((void)(NO && ((void)((CLASS *)(nil)).KEYPATH, NO)), #KEYPATH))

該宏定義使用了C語言的逗號表達(dá)式,(3+5,6+8)稱為逗號表達(dá)式,其求解過程先表達(dá)式1,后表達(dá)式2,整個表達(dá)式值是表達(dá)式2的值,如:(3+5,6+8)的值是14,a=(a=3 x 5,a x 4)的值是60,而(a=3 x 5,a x 4)的值是60, a的值是15。

使用逗號表達(dá)式,我覺得主要是為了FBKVOClassKeyPath

FBKVOClassKeyPath(DWPerson, name)==(((void)(NO && ((void)((DWPerson *)(nil)).name, NO)), #KEYPATH)),其會檢查DWPerson中是否有name屬性

3、自釋放

FBKVOController通過自釋放的機制來實現(xiàn)observer的自動移除,其實就是給observer的類中添加一個FBKVOController的成員變量,然后在FBKVOController中的dealloc移除observer,下面是個例子

#import "DWTestViewController.h"
#import "DWObserViewController.h"

@interface DWTestViewController ()
@property (nonatomic, strong) DWObserViewController *obserVC;
@end

@implementation DWTestViewController

- (instancetype)init
{
    self = [super init];
    if (nil != self) {
        _obserVC = [[DWObserViewController alloc] init];
        NSLog(@"DWTestVC創(chuàng)建");
        NSLog(@"DWObserVC創(chuàng)建");
    }
    return self;
}
#import "DWObserViewController.h"

@implementation DWObserViewController

- (void)dealloc {
    NSLog(@"DWObserVC跟著銷毀");
}

打印:

2018-02-05 15:32:39.299859+0800 FBKVOController_Demo[6804:208216] DWTestVC創(chuàng)建
2018-02-05 15:32:39.300209+0800 FBKVOController_Demo[6804:208216] DWObserVC創(chuàng)建
2018-02-05 15:32:41.271585+0800 FBKVOController_Demo[6804:208216] DWTestVC銷毀
2018-02-05 15:32:46.520148+0800 FBKVOController_Demo[6804:208216] DWObserVC跟著銷毀

參考:

NSHashTable和NSMapTable用法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容