2016-09-06 13 views
1

我有一個私人的NSManagedObjectContext隊列,我用它將實體保存到核心數據。保存完成後,我想發出NSNotification。但是,它似乎不喜歡我從私人隊列發出通知。這是我的私人隊列代碼:如何調用主線程上的更新

let parentManagedContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext! 
let privateManagedContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType) 
privateManagedContext.persistentStoreCoordinator = parentManagedContext.persistentStoreCoordinator 

privateManagedContext.performBlock { 
    ... 
    // Save the entity 
    do { 
     try privateManagedContext.save() 
     // Send out NSNotification here 
    } 
} 

如何添加內performBlock塊主線程上運行?

回答

1

好吧,等我發佈了這個問題後,我想出了答案。我所要做的就是在try privateManagedContext.save()代碼之後添加以下代碼:

NSOperationQueue.mainQueue().addOperationWithBlock({ 
    NSNotificationCenter.defaultCenter().postNotificationName(kNotificationName, object: nil) 
}) 
0

希望這有助於

dispatch_async(dispatch_get_main_queue()) { 
    NSNotificationCenter.defaultCenter().postNotificationName(kNotificationName, object: nil) 
} 
相關問題