user79854
user79854

Reputation:

How to allow one thread to mutate an array property while another thread iterates on a copy of the array

I would like to implement an observer pattern in Objective-C where the observer implements an interface similar to SKPaymentTransactionObserver and the observable class just extends my base observable. My observable class looks something like what is below. Notice I'm making copies of the observers before enumeration to avoid throwing an exception .

I've tried adding an NSLock around add observers and notify observers, but I run into a deadlock.

What would be the proper way to handle concurrency when observers are being added as notifications are being sent?

@implementation Observable

-(void)notifyObservers:(SEL)selector {
    @synchronized(self) {
        NSSet* observer_copy = [observers copy];
        for (id observer in observer_copy) {
            if([observer respondsToSelector: selector]) {
                [observer performSelector: selector];
            }
        }
        [observer_copy release];
    }
}

-(void)notifyObservers:(SEL)selector withObject:(id)arg1 withObject:(id)arg2 {
    @synchronized(self) {
 NSSet* observer_copy = [observers copy];
        for (id observer in observer_copy) {
            if([observer respondsToSelector: selector]) {
                [observer performSelector: selector withObject: arg1 withObject: arg2];
            }
        }
       [observer_copy release];
    }
}

-(void)addObserver:(id)observer {
    @synchronized(self) {
        [observers addObject: observer];
    }
}

-(void)removeObserver:(id)observer {
    @synchronized(self) {
       [observers removeObject: observer];
    }
}

Upvotes: 4

Views: 301

Answers (2)

zoul
zoul

Reputation: 104065

What’s the problem with the current code? Is it the one that deadlocks? Could you think of the specific deadlock scenario? Did you try to only synchronize the array copy? Like this:

- (void) notify {
    @synchronized(self) {
        NSSet *observersCopy = [observers copy];
    }
    for (id observer in observersCopy)
        [observer doSomething];
    [observersCopy release];
}

- (void) addObserver: (id) observer {
    @synchronized(self) {
        [observers addObject:observer];
    }
}

Upvotes: 1

zoul
zoul

Reputation: 104065

Couldn’t you choose the easier way of notifications?

Upvotes: 2

Related Questions