核心数据:超时添加应用程序启动时的持久存储



我有一个在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. 如果核心数据确定需要在数据库中进行维护的某些事情,则可能需要比预期的时间更长。

"正确"的答案是将商店添加到主线程中的协调器中。

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

更新3

  1. 在仪器中概述了这一点。那会告诉你慢跑在哪里。
  2. dataToMigrate = [self fetchSomeDataFromDatabase];做什么?

查看您的时间配置文件,这会告诉您什么慢。向我发送仪器的时间配置文件,我也会查看它。

代码很容易:

    dispatch_async(DISPATCH_QUEUE_PRIORITY_HIGH, ^{
        [self setupCoreDataStack]; //TODO
    })

但是,正如马库斯所说,请确保您的应用程序可以处理未准备好Coredata的发射(现在是异步)。设置一个飞溅屏幕!?/不使用DB

的基本菜单

最新更新