在我的静态库的委托方法被触发后发生了一个奇怪的问题。首先,该项目有一个子项目,它是一个静态库(xcode 4.6 ios 6.x)。根据事件,静态库会激发自己的代理。 该应用程序实现了静态库的委托方法。在实现中,我使用以下来访问UI元素并触发其他事件。 Didgetnotified是lib的委托方法。从委托方法中选择器触发两次
- (void)didGetNotified
{
dispatch_async(dispatch_get_main_queue(), ^{
[self parseData];
NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
[notifyCenter addObserver:self
selector:@selector(updateUI)
name:@"updateUIN"
object:nil];
});
}
-(void) parseData {
//parse data and its ready now and send notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateUIN" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateUIN" object:nil];
}
-(void) updateUI {
//this method gets fired twice mostly
}
问题是,updateUI
被调用两次。我不能看到我做错了什么。这是线程的东西吗?静态lib委托不在主线程上。但我使用主线程上的调度。有人可以解释一下吗? 提前致谢。
看起来你正在发射一个'updateUIN'通知,它将被当前对象捕获。那是你要的吗? (你想通过'updateUIN'通知来做什么,为什么生成通知的对象也订阅了它?) – bdesham 2013-03-13 17:02:59
如果方法从相同的通知中被触发两次,您正在订阅通知两次。您必须在其他地方再次订阅。 @bdesham是对的,但是,你的模式很奇怪 – jackslash 2013-03-13 17:55:02
不要在委托方法中注册这个通知。 – iMash 2013-03-13 17:59:09