美文网首页
FBKVOController 使用

FBKVOController 使用

作者: wustzhy | 来源:发表于2018-04-15 23:28 被阅读83次
- 使用way1 (最正常用法)
#import "NSObject+FBKVOController.h"

    [self.KVOController observe:self.label keyPath:@"text" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
        //weakSelf
        weakSelf.count ++;
        if (weakSelf.count == 1) {
            NSLog(@"weakSelf:%@",weakSelf);
            NSLog(@"label:%@",weakSelf.label);
        }
    }];

FBKVOController 方便之处在于,不需要在dealloc里调用
-(void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath

- 使用way2(当self observe self时)
    [self.KVOControllerNonRetaining observe:self keyPath:@"numStr" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
        //weakSelf
        NSLog(@"%@",weakSelf.numStr);
    }];

  1. 这时,需要使用KVOControllerNonRetaining,否则出现retainCycle
  2. 这时,还需要手动调用removeObserver:... or self.KVOControllerNonRetaining unobserve:...
    具体原因可以看源码~

另外,这种self observe self实在没有必要用kvo,直接在setter方法中处理即可嘛。

image.png

相关文章

  • FBKVOController 使用

    - 使用way1 (最正常用法) FBKVOController 方便之处在于,不需要在dealloc里调用-(v...

  • FBKVOController的使用

    一、下载FBKVOController第三方库; 二、导入头文件 三、接下来就是开始使用了ViewControll...

  • FBKVOController 的使用

    系统 KVO 实现的替代方案优势1、不用手动移除观察者了2、 不允许多次 addObserver 同一个 keyp...

  • FBKVOController

    FBKVOController介绍 简单来说,Facebook 开源的这套代码,主要是对我们经常使用的 KVO 机...

  • FBKVOController详解

    前言 前段时间刚到公司,公司在使用FBKVOController,本人一直在使用系统的KVO,没有使用过Faceb...

  • iOS组件化(四)-FBKVOController源码分析

    FBKVOController实现原理 在上一篇文章中,有用到FBKVOController去实现MVVM模式。现...

  • KVO

    写在前面 基础使用 基本原理 最佳实践 FBKVOController 基础使用 监听一个对象的属性变化,比如监听...

  • FBKVOController实现原理

    1.系统KVO的问题2.FBKVOController优点3.FBKVOController的架构设计图4.FBK...

  • FBKVOController

    学习cocoapods下的KVOController github源码地址 facebook/KVOControl...

  • 使用FBKVOController替换系统kvo

    FBKVOController是Facebook开源框架(https://github.com/facebook/...

网友评论

      本文标题:FBKVOController 使用

      本文链接:https://www.haomeiwen.com/subject/nrojkftx.html