IOS Auto-Remove KVO Viewer

Source: Internet
Author: User

Write a category to NSObject:

#import <Foundation/Foundation.h>

@interface NSObject (Fmobserverhelper)

-(void) Fm_addobserver: (NSOBJECT *) Observer Forkeypath: (NSString *) KeyPath;

@end

The Dealloc method is called before the object is freed, and the instance variables it holds are also freed.

When listening for registration, a temporary object is associated for self and observer, and when both are releasing the instance variable, the opportunity is taken to remove the observer in the Dealloc method of the temporary object.

The self is freed before it is released, and the self is not completely freed, but the target is nil in the temporary object.

Weak: The holder does not retain the target, and when the target is destroyed, the holder's instance variable is empty

Unsafe_unretained: The holder does not retain the target, and when the target is released, the holder's instance variable still points to the previous memory space (wild pointer)

If the Observer is released early, and the associated attribute is added, both cannot hold the temporary object at the same time, otherwise the temporary object will not be released in time, since one does not, then it is related to each other.

Two associated properties were released at the same time, the removal operation was observed two times. To avoid this problem, you need to determine whether the instance variable factor referenced by weak is empty.

#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

IOS Auto-Remove KVO Viewer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.