2013-09-25 41 views
0

使用NSNotificationCenter,它運行良好,直到我開始發送非常快的消息到其他類。NSNotificationCenter有最高速度嗎?

什麼是快速?其大約在30-40通知秒。我甚至沒有得到他們中的一個。 有沒有其他方法可以做到這一點? 我應該更新一個全球?

//post data out . 
- (void)post:(NSString*)string 
{ 
    NSLog(@"done"); //the log is printing 
    NSDictionary *userInfo = nil; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"connector" 
                 object:string 
                 userInfo:userInfo]; 
} 

我知道觀察員是好的,因爲相同的代碼工作過。 非常感謝。

+4

使用通知中心進行_two_類之間的直接一對一通信有什麼意義? – holex

+0

在開始戰爭的風險中,OP詢問使用'NSNotificationCenter'。不管這是否是個好主意(正如@holex指出的那樣)是一個[不同的問題](http://stackoverflow.com/search?q=When+to+use+NSNotificationCenter)。 –

+0

@MikeD,沒有開始任何戰爭,必須有一個非常明確的理由要求,因爲我們都知道通知中心並不是基於這種壓力而設計的。這就是爲什麼我問了一點... – holex

回答

2

它看起來像你設置了錯誤的object,這應該是發佈通知的對象(或nil)。我覺得你應該做添加stringuserInfo

NSDictionary *userInfo = @{@"somekey" : string }; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"connector" 
                object:self 
                userInfo:userInfo]; 
+0

sh * t!你是對的:)但它是如何工作到現在?這段代碼是從其他條件複製過來的,這是很好的工作,現在只有在我改變你所說的話之後 - 它才能工作。 – Curnelious

3

如果你發送30-40通知的第二你真的應該重新考慮您的實現。

下面有一些替代方案:

  1. Creating your own delegates for callbacks
  2. Using a block

與NSNotificationCentre的問題是,它發送消息到每一個觀察者 - 這可能很慢,通常用於更新視圖處於狀態變化(登錄/註銷)。