ios - 核心数据 : Timeout adding persistent store on application launch

标签 ios core-data

我有一个在 application:didFinishLaunchingWithOptions 中创建持久存储的应用程序。添加商店显然会花费太长时间,这会导致 iOS 在应用程序完成启动之前终止该应用程序。超时时的堆栈跟踪如下所示。有谁知道如何防止这种情况发生?

Exception Type:  00000020
Exception Codes: 0x000000008badf00d
Highlighted Thread:  0

Application Specific Information:
com.foo.bar failed to launch in time

Elapsed total CPU time (seconds): 23.490 (user 23.490, system 0.000), 78% CPU 
Elapsed application CPU time (seconds): 8.406, 28% CPU

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib          0x31b8939c pread + 20
1   libsqlite3.dylib                0x31ed95d0 unixRead
2   libsqlite3.dylib                0x31eec106 readDbPage
3   libsqlite3.dylib                0x31eeb2a2 sqlite3PagerAcquire
4   libsqlite3.dylib                0x31f04096 moveToChild
5   libsqlite3.dylib                0x31f052c6 sqlite3BtreeNext
6   libsqlite3.dylib                0x31f01490 sqlite3VdbeExec
7   libsqlite3.dylib                0x31efa48a sqlite3_step
8   CoreData                        0x364f8892 _execute
9   CoreData                        0x364f878c -[NSSQLiteConnection execute]
10  CoreData                        0x3658bd94 -[NSSQLConnection prepareAndExecuteSQLStatement:]
11  CoreData                        0x365dd4f2 -[_NSSQLiteStoreMigrator performMigration:]
12  CoreData                        0x365d70dc -[NSSQLiteInPlaceMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:]
13  CoreData                        0x36577428 -[NSMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:]
14  CoreData                        0x365c8670 -[NSStoreMigrationPolicy(InternalMethods) migrateStoreAtURL:toURL:storeType:options:withManager:error:]
15  CoreData                        0x365c79c4 -[NSStoreMigrationPolicy migrateStoreAtURL:withManager:metadata:options:error:]
16  CoreData                        0x365c8ece -[NSStoreMigrationPolicy(InternalMethods) _gatherDataAndPerformMigration:]
17  CoreData                        0x364ec3b0 -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:]

最佳答案

badfood 是一种常见的启动崩溃。您需要从主线程中创建核心数据堆栈。我过去曾在这里和其他地方多次讨论过这个问题。

您还可以查看 Aplle 的 iCloud Core Datavidros,因为该启动代码也可以解决您的问题。

请记住,您的应用需要能够在没有堆栈的情况下启动。对于现有的应用程序,这可能是一个重大变化。

更新2

这一直被推荐,但不幸的是直到最近它才开始出现在模板中。

有几件事会导致持久存储的添加花费更长的时间:

  1. 如果您要将数据库从一个版本迁移到另一个版本;迁移发生在您将商店添加到协调器时。
  2. 如果您要将 iCloud 添加到您的应用程序中,那么在首次启动时将显着花费更长的时间。
  3. 如果 Core Data 确定需要对数据库进行维护,则可能需要比预期更长的时间。

“正确”的答案是将存储添加到主线程之外的协调器。

快速/创可贴的答案是为核心数据打开 sql 日志记录,然后查看启动期间发生了什么。一旦您了解导致延迟的原因,您就可以解决它。如果是迁移,那么正确的答案可能是唯一的答案。

更新3

  1. 在仪器中对此进行分析。这会告诉您哪里慢了。
  2. dataToMigrate = [self fetchSomeDataFromDatabase]; 做什么?

查看您的时间配置文件,它会告诉您什么是缓慢的。将仪器的时间曲线发给我,我也会查看。

关于ios - 核心数据 : Timeout adding persistent store on application launch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13333289/

相关文章:

iphone - 如何正确使用 ManagedObjectID?

ios - swift 错误 : Could not determine generated file paths for Core Data code generation:

ios - 由于核心数据迁移,应用程序在从应用程序商店更新后崩溃

xcode - Coredata NSFetchRequest DictionaryResultType 空属性 Swift

objective-c - JSON 结果到 Objective-C 中的 NSDictionary

ios - 使用 ARKIT Image 识别应用商店中的应用发布问题

ios - AVCaptureFileOutputRecordingDelegate函数给出的URL无法播放

ruby-on-rails - iOS 核心数据 : Approach for setting relationship for Rails polymorphic association

ios - UIPageViewController NSInternalInconsistencyException

ios - 如何使用通知请求禁用 View ?