[英]Cannot invoke code on the main thread from the background thread in iOS6
當在后台線程上保存托管對象上下文時,我在聽NSManagedObjectContextDidSaveNotification並嘗試在主線程上合並它。
但是,當我嘗試將請求轉發到主線程時,都不會執行任何代碼
[self performSelectorOnMainThread:@selector(executeThisCode:) withObject:saveNotification waitUntilDone:NO];
也沒有
dispatch_async(dispatch_get_main_queue(), ^{
...execute this code
});
奇怪的是,這一切都適用於iOS 5.1和iOS 5.0,但不適用於iOS6。有什么想法嗎?
首先檢查是否已經在主線程中嗎? 如果executeThisCode
是您在調用performSelectorOnMainThread
時當前正在執行的方法的選擇器,則這將特別相關。 像這樣:
- (void) executeThisCode: (NSNotification*) notification{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(executeThisCode:)
withObject:notification
waitUntilDone:YES];
return;
}
// merge logic goes here and executes on the main thread
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.