我的代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
@autoreleasepool {
// Now on a background thread
// Setup background task
__block UIBackgroundTaskIdentifier bgTask;
void (^finishBackgroundTask)(void) = ^(void) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
};
// Start background task
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:finishBackgroundTask];
// The method below migrates a core data database and takes ages
[MyClass migrateCoreDataStuff];
finishBackgroundTask();
}
});我得到的错误是NSUnderlyingException = "Fatal error. The database at /var/mobile/Applications/55B83D5F-CCF5-438E-BECA-B97DB5505541/Documents/Blah.sqlite is corrupted. SQLite error code:11, 'database disk image is malformed'";
仅当满足以下所有条件时,才会发生迁移错误:*迁移在后台线程上*迁移以UIBackgroundTask身份运行*我在设备上运行,而不是在模拟器上运行
我运行的是为iOS 4.0构建的iOS 4.3.5。
发布于 2012-01-25 20:13:24
如果不查看migrateCoreDataStuff的内容,就很难看出确切的问题所在。然而,非主线程上的核心数据是一个棘手的问题。请阅读http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html。对于新线程,您可能至少需要一个单独的托管对象上下文。
https://stackoverflow.com/questions/9002421
复制相似问题