核心数据:在应用程序启动时添加持久存储的超时 [英] Core Data: Timeout adding persistent store on application launch

查看:295
本文介绍了核心数据:在应用程序启动时添加持久存储的超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序在应用程序中创建一个持久存储:didFinishLaunchingWithOptions 。添加商店显然需要的时间过长,导致iOS在完成启动之前终止应用程序。在超时时的堆栈跟踪看起来像这样。有没有人知道如何防止这种情况发生?

I have an app that creates a persistent store in application:didFinishLaunchingWithOptions. Adding the store can apparently take too long, which causes iOS to terminate the app before it finishes launching. The stack traces at the time of timing out looks like this. Does anyone know how to prevent this from happening?

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是一个常见的启动崩溃。你需要从主线程创建Core Data栈。我已经在这里和过去的其他地方讨论过这几次。

badfood is a common launch crash. You need to get the creation of the Core Data stack off the main thread. I have discussed this a few times on here and other places in the past.

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

You can also look at Aplle's iCloud Core Datavidros as that launch code will solve your problem as well.

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

Keep in mind that your app will need to be able to launch without the stack being in place. For existing apps this can be a major change.

这总是推荐 ,但不幸的是,已开始在模板中显示。

This has always been recommended but unfortunately only recently has it been starting to show in the templates.

有几种情况会导致持续存储的添加需要更长时间:

There are several things that can cause the adding of the persistent store to take longer:


  1. 如果要将数据库从一个版本迁移到另一个版本;

  2. 如果您正在将iCloud添加到应用程序,则在第一次启动时会显示更长的时间。 li>
  3. 如果Core Data确定需要对数据库进行维护,则可能需要比预期更长的时间。

  1. If you are migrating your database from one version to another; the migration occurs when you add the store to the coordinator.
  2. If you are adding iCloud to your application it will take significantly longer on the first launch.
  3. If Core Data determines that something needs to be done to the database for maintenance, it can take longer than expected.

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

The "right" answer is to add the store to the coordinator off the main thread.

快速/拼接的答案是打开核心数据的sql日志记录,然后看看启动期间发生了什么。一旦你明白了什么是造成延迟,你可以解决它。如果是迁移,那么正确的答案可能是唯一的答案。

The quick/bandaid answer would be to turn on sql logging for core data and then see what is going on during launch. Once you understand what is causing the delay you may be able to address it. If it is a migration then the right answer is probably the only answer.


  1. 在仪器中对此进行配置。

  2. 是什么?dataToMigrate = [self fetchSomeDataFromDatabase]; do?

  1. Profile this in instruments. That will tell you where the slowness is.
  2. What does dataToMigrate = [self fetchSomeDataFromDatabase]; do?

查看您的时间档案,并告诉您什么是缓慢的。给我从乐器的时间剖面,我会看看它。

Look at your time profile and that will tell you what is slow. Send me the time profile from instruments and I will look at it as well.

这篇关于核心数据:在应用程序启动时添加持久存储的超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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