在我的靜態庫的委託方法被觸發後發生了一個奇怪的問題。首先,該項目有一個子項目,它是一個靜態庫(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