iOS 自动移除KVO观察者

对NSObject写一个分类:

#import <Foundation/Foundation.h>

@interface NSObject (FMObserverHelper)

- (void)fm_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;

@end

//  对象被释放之前, 会调用dealloc方法, 其持有的实例变量也会被释放.

//  在监听注册时, 为self和Observer关联个临时对象, 当两者在释放实例变量时, 借助这个时机, 在临时对象的dealloc方法中, 移除Observer

//  self在被释放之前, 会先释放其持有的关联属性, self并未完全释放, 可在临时对象中target却成了nil.

//  weak: 持有者不会对目标进行retain, 当目标销毁时, 持有者的实例变量会被置空

//  unsafe_unretained: 持有者不会对目标进行retain, 当目标释放后, 持有者的实例变量还会依然指向之前的内存空间(野指针)

//  如果Observer提前释放,而添加关联属性, 两者还不能同时持有临时对象, 否则临时对象也不会及时的释放,既然一个不行, 那就各自关联一个.

//  两个关联属性释放的同时, 进行了两次观察移除的操作. 为避免这个问题, 需要判断weak引用的实例变量factor是否为空即可

#import "NSObject+FMObserverHelper.h"

#import <objc/runtime.h>

@interface FMObserverHelper : NSObject

@property (nonatomic, unsafe_unretained) id target;

@property (nonatomic, unsafe_unretained) id observer;

@property (nonatomic, strong) NSString * keyPath;

@property (nonatomic, weak) FMObserverHelper * factor;

@end

@implementation FMObserverHelper

- (void)dealloc {

if ( _factor ) {

[_target removeObserver:_observer forKeyPath:_keyPath];

}

}

@end

@implementation NSObject (FMObserverHelper)

- (void)fm_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath {

[self addObserver:observer forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:nil];

FMObserverHelper * helper = [FMObserverHelper new];

FMObserverHelper * sub = [FMObserverHelper new];

sub.target = helper.target = self;

sub.observer = helper.observer = observer;

sub.keyPath = helper.keyPath = keyPath;

helper.factor = sub;

sub.factor = helper;

const char * helpeKey = [NSString stringWithFormat:@"%zd", [observer hash]].UTF8String;

objc_setAssociatedObject(self, helpeKey, helper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

objc_setAssociatedObject(observer, helpeKey, sub, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

@end

原文地址:https://www.cnblogs.com/fengmin/p/8125870.html

时间: 2024-08-29 11:27:30

iOS 自动移除KVO观察者的相关文章

iOS 中KVC、KVO、NSNotification、delegate 总结及区别

iOS 中KVC.KVO.NSNotification.delegate 总结及区别 1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的关键技术之一. Demo: @interface myPerson : NSObject { NSString*_name; int      _age; int      _height; int      _w

【iOS】KVC 与 KVO

一.KVC与KVO *"KVC":key value Coding(键值编码) *目的:间接的修改或获取对象的属性,降低程序(类与类)之间的耦合度. *"KVO":key value Observer(键值观察),观察者模式.(检测模型变化用的多) *目的:通常用于观察某个对象的某个属性发生变化时,及时做出相应. 二.KVC的使用方式 KVC被称为iOS开发平台的大招!!! 能快速修改对象属性. *[p1setValue:@"xxxx"forKe

iOS NSNotificationCenter 移除通知带来的crash

Where to remove observer for NSNotification? 在dealloc方法中移除通知观察者带来crash NSNotificationCenter中的通知消息已经发出,而观察者对象子线程释放,也就是抛送通知消息的线程和观察者对象子线程释放的线程不一致时,存在crash风险,原因是NSNotificationCenter不是线程安全的. 解决办法:尽早移除通知 或者保证释放和抛送通知在同一个线程.

不用自动移除的通知中心

源码 // // DefaultNotificationCenter.h // TotalCustomTabBarController // // Created by YouXianMing on 16/6/3. // Copyright © 2016年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @class DefaultNotificationCenter; @protocol Defau

iOS自动打包[转载]

敲一下enter键,完成iOS的打包工作[转载] http://ios.jobbole.com/84677/ 作为开发人员,免不了要为测试人员打包,让其测试.而打包这个行为是非常无聊的,特别是在每个新版本上线前一两天,总会出现一些莫名其妙的bug,然后这两天打包活动也是特别频繁.一天要重复好几次同样的动作,作为一个程序员,去做这样的事情,是完全无法容忍的.自动化打包,好在有你.所以今天就来谈一谈iOS应用自动化打包,但是本篇文章主要谈的是打单一的测试包,并不是在讲持续集成. xcodebuild

iOS 中KVC、KVO、NSNotification、delegate 总结及区别-b

1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的关键技术之一. Demo: @interface myPerson : NSObject { NSString*_name; int      _age; int      _height; int      _weight; } @end @interface testViewController

【原】iOS中KVC和KVO的区别

在iOS开发中经常会看到KVC和KVO这两个概念,比较可能混淆,特地区分一下 KVC(Key Value Coding) 1> 概述 KVC:Key Value Coding,键值编码,是一种间接访问实例变量的方法. KVC 提供了一个使用字符串(Key)而不是访问器方法,去访问一个对象实例变量的机制. 2> KVC部分源码(头文件) 1 // NSKeyValueCoding.h 2 @interface NSObject(NSKeyValueCoding) 3 4 + (BOOL)acce

IOS开发之旅-KVO

在设计模式中,有一种模式称为观察者模式,Objective-c也提供了类似的机制,简称为KVO[Key-Value Observing].当被观察者的属性改变时立即通知观察者触发响应的行为. 在KVO中,首先被观察者与观察者应该先建立关系,当被观察的特定属性改变时,立刻通知观察者,建立联系调用如下方法: /* Register or deregister as an observer of the value at a key path relative to the receiver. The

iOS通讯模式(KVO、Notification、Delegate、Block、Target-Action的区别)

文章翻译自 https://www.objc.io/issues/7-foundation/communication-patterns/ 每个Application或多或少都有一些松耦合的对象(模块)组成,他们必须彼此通讯来完成工作.这篇文章将会通过可用的通讯机制,并以Apple的Framework来举例,并给出最佳的实践建议关于使用哪种通讯机制. 虽然这个问题是关于Foundation框架的,但是我们可以通过Foundation的通讯机制,差不多有这几个通讯方法 - KVO,Notifica