Tag: barrier

如何防止temporaryContext与migratePersistentStore同时运行

我有一个代码部分,我调用migratePersistentStore ,我想阻止任何temporaryContext在同一时间做任何事情,如何? 我的想法是基于semaphore和dispatch_group 。 代码A: dispatch_group_wait(dgLoadMain, DISPATCH_TIME_FOREVER) dispatch_semaphore_wait(semaLoadMain, DISPATCH_TIME_FOREVER) mainMOC!.performBlockAndWait({ mainMOC!.persistentStoreCoordinator!.migratePersistentStore(/* … */) }) dispatch_semaphore_signal(semaLoadMain) 代码B: dispatch_group_enter(dgLoadMain) dispatch_semaphore_wait(semaLoadMain, DISPATCH_TIME_FOREVER) dispatch_semaphore_signal(semaLoadMain) let context = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType) context.parentContext = mainMOC var context: NSManagedObjectContext context.performBlockAndWait({ // .. some code I do not want to run when migrating persistent store context.save(nil) }) mainMOC.performBlockAndWait({ mainMOC.save(nil) }) dispatch_group_leave(dgLoadMain) 你怎么看待这件事? 任何bette解决scheme? 在这种情况下可以使用dispatch_barrier_async吗?