后台线程中的NSNotification-iOS

后台线程中的NSNotification-iOS,ios,objective-c,multithreading,delegates,nsnotificationcenter,Ios,Objective C,Multithreading,Delegates,Nsnotificationcenter,我正在开发聊天应用程序,其中消息是通过委托方法接收的。通过委托方法,我调用NSNotification来更新UI中的消息 //委托方法 - (void)didReceiveMessage:(XMPPMessage *)message{ [[NSNotificationCenter defaultCenter]postNotificationName:@"MessageReceived" object:nil userInfo:@{@"message":message}]; } 我收

我正在开发聊天应用程序,其中消息是通过委托方法接收的。通过委托方法,我调用NSNotification来更新UI中的消息

//委托方法

- (void)didReceiveMessage:(XMPPMessage *)message{
     [[NSNotificationCenter defaultCenter]postNotificationName:@"MessageReceived"  object:nil userInfo:@{@"message":message}];
}

我收到的每一条新消息都将调用上述委托方法。假设我同时收到大量消息,这会导致UI挂起。如果我为通知添加一个后台线程,那么它将为每个委托方法调用创建一个新线程。这不是一个好的解决办法。我如何处理这种情况

您可以直接向负责更新UI的ViewController发送消息,而不是在收到消息后发布通知。比如说

- (void)didReceiveMessage:(XMPPMessage *)message{
    MessageListController *messageListController; //get reference as per your UI implementation
    [messageListController updateUIWithMessage:message];
}

在updateUIWithMessage方法中,您可以编写优化的代码来更新您的UI。

您应该使用
NSOperationQueue
并将其设置为全局(类级别)。添加
NSOperations

您可以使用
maxConcurrentOperationCount
属性设置最大并发操作数。通过这种方式,您可以同时控制多个操作。但对于你的情况,我想

maxConcurrentOperationCount = 1;
这很好。因为您需要消息来逐个更改UI。或者根据您的需要进行设置