Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios removing an observer before it exists raises exception

In an effort to prevent multiple observers from being added, I'm removing the observer before I add one, which was recommended here: iPhone - testing if a notification exists.

[[self getPlayer] removeObserver:self forKeyPath:@"position"];
[[self getPlayer] addObserver:self forKeyPath:@"position" options:NSKeyValueObservingOptionNew context:nil];

However, doing so causes an exception: __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__

Any ideas on how to check if an observer already exists for a key path and if so not add a new one?

like image 945
stewart715 Avatar asked Dec 29 '13 02:12

stewart715


1 Answers

Add a boolean that keeps track of whether or not you've added your observer. Set it to true after adding, and only remove your observer if the flag indicates that you have added one.

like image 135
Duncan C Avatar answered Oct 05 '22 23:10

Duncan C