1. 程式人生 > >ios NSNotificationCenter接受通知不能操作UI執行緒

ios NSNotificationCenter接受通知不能操作UI執行緒

今天遇到了一個很奇怪的問題,整合融雲聊天,,獲取群組未讀訊息數量的時候,我寫了一個通知,再通知中更新tableview  Cell中的訊息數量,但是訊息數量改變的很慢,而且有時候不更新  我是更新某一個cell  

                        [self.tableViewreloadRowsAtIndexPaths:@[[NSIndexPathindexPathForRow:i inSection:0]]withRowAnimation:UITableViewRowAnimationAutomatic];在測試的時候發現,cell的drawRect方法不執行,如果是直接reloadData  ,就沒問題,,

在log中發現了一句    This application is modifying the autolayout engine from a background thread after the engine was a  ,下面是我的通知方法,這樣在main執行緒中重新整理就沒什麼問題了,

- (void)receiveMessage:(NSNotification *)notification

{

RCMessage *message = [[notification object] objectForKey:@"message"];

if (message) {

for (NSInteger i=0; i<self.customerArray.count; i++) {

CustomerModel *model = self.customerArray[i];

if ([model.ChatRoomIdisEqualToString:message.targetId

]) {

                model.unreadMsg = @([[RCIMClientsharedRCIMClient] getUnreadCount:ConversationType_GROUPtargetId:message.targetId]);

dispatch_async(dispatch_get_main_queue(), ^{

NSArray *visiableCells = self.tableView.visibleCells;

HomeListCell *cell = [self.tableViewcellForRowAtIndexPath:[NSIndexPath

indexPathForRow:i inSection:0]];

if ([visiableCells containsObject:cell]) {

                        [self.tableViewreloadRowsAtIndexPaths:@[[NSIndexPathindexPathForRow:i inSection:0]]withRowAnimation:UITableViewRowAnimationAutomatic];

                    }

                });

break;

            }

        }

    }

}