如何防止temporaryContext与migratePersistentStore并发运行 [英] How to prevent temporaryContext run concurrently with migratePersistentStore

查看:190
本文介绍了如何防止temporaryContext与migratePersistentStore并发运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码部分,其中我调用 migratePersistentStore ,我想阻止任何 temporaryContext 同时,如何?

I have a code part where I call migratePersistentStore and I want to prevent any temporaryContext to do anything in the same time, how?

我的想法是基于信号量 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解决方案?在这种情况下是否可以使用 dispatch_barrier_async

What do you think about it? Any bette solution? Is it possible to use dispatch_barrier_async in this case?

推荐答案

更好的解决方案是设计应用程序,使这不是必需的。您正在有效地阻止整个应用程序(包括UI线程)在迁移过程中进行任何操作。

A far better solution is to design the application so that this is not necessary. You are effectively blocking the entire application (including the UI thread) from doing anything while a migration is happening.

更好地将其开发为状态引擎,背景,然后在迁移完成时通知UI。这样,您的应用程序可以响应系统(并且不会被操作系统杀死),并且还可以为用户提供潜在的状态更新。

Better to develop this as a state engine, do the migration in the background and then notify the UI when the migration is complete. That way your application is responsive to the system (and you won't get killed by the OS) and can also provide potential status updates to the user.

在这里只是乞求操作系统杀死你的应用程序中间迁移。

What you are doing here is just begging for the OS to kill your app mid migration.

这篇关于如何防止temporaryContext与migratePersistentStore并发运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆