Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of addObserver is incorrect to handle all KVO Options #5

Open
RVijay007 opened this issue Apr 21, 2015 · 0 comments
Open

Comments

@RVijay007
Copy link

In particular, NSKeyValueObservingOptionInitial will not be triggered correctly as the existing implementation calls addObserver before storing the block, when in fact the block should be saved first as the default addObserver needs to call the block before the method itself even returns.

- (void)addObserver:(NSObject *)observer
         forKeyPath:(NSString *)keyPath
            options:(NSKeyValueObservingOptions)options
            context:(void *)context
          withBlock:(KVOBlock)block
{
    NSMutableDictionary *blocks = objc_getAssociatedObject(self, (void *)[keyPath hash]);
    if (!blocks) {
        blocks = [[NSMutableDictionary alloc] init];
        objc_setAssociatedObject(self, (void *)[keyPath hash], blocks, OBJC_ASSOCIATION_RETAIN);
    }

    blocks[@([observer hash])] = [block copy];
    [self addObserver:self forKeyPath:keyPath options:options context:context];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant