Question

Some of our users crash on Core Data migration.

There are already several questions about "Core Data Migration & crash", mainly about memory usage and UI response.

Migrating large Core Data database crash

Out-Of-Memory while doing Core Data migration

Core Data causing app to crash while migrating

Core Data lightweight migration crash

For high memory peak, Apple suggests multiple passes solution, and here is another large datasets solution.

When I try to reproduce the problem, like migrating large datasets using lightweight migration, Xcode will sometimes terminate my app due to memory usage. So I think that our users may come across crash due to the same reason - low memory.

But, @Marcus S. Zarra told me :

@Jason Lee: Lightweight migration doesn't run out of memory. Perhaps you are confusing it with a bad food crash or something else? I would suggest opening a new SO question and sending me the link. – Marcus S. Zarra

So I create this new question to ask:

  1. How to tell the reason when the app crash on Core Data Migration? Memory problem or no UI response.
  2. How to solve the problem?

Thanks .

UPDATE 1: add crash log

@Marcus S. Zarra , you are right. ATE BAD FOOD...

Date/Time:       2014-01-03 15:15:00.801 +0800
OS Version:      iPhone OS 5.1.1 (9B206)
Report Version:  104

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

Application Specific Information:
com.xxx.xxx failed to launch in time

Elapsed total CPU time (seconds): 7.330 (user 7.330, system 0.000), 36% CPU 
Elapsed application CPU time (seconds): 4.387, 21% CPU

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libsqlite3.dylib                0x317ae6ba ___lldb_unnamed_function176$$libsqlite3.dylib + 166
1   libsqlite3.dylib                0x317a2846 ___lldb_unnamed_function128$$libsqlite3.dylib + 7934
2   libsqlite3.dylib                0x317a06c2 sqlite3_step + 2098
3   CoreData                        0x36924e2e _execute + 50
4   CoreData                        0x36924d64 -[NSSQLiteConnection execute] + 2408
5   CoreData                        0x369c7d54 -[NSSQLConnection prepareAndExecuteSQLStatement:] + 40
6   CoreData                        0x36a19582 -[_NSSQLiteStoreMigrator performMigration:] + 590
7   CoreData                        0x36a102b8 -[NSSQLiteInPlaceMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 1948
8   CoreData                        0x369b5c02 -[NSMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 482
9   CoreData                        0x36a01bf4 -[NSStoreMigrationPolicy(InternalMethods) migrateStoreAtURL:toURL:storeType:options:withManager:error:] + 176
10  CoreData                        0x36a0119c -[NSStoreMigrationPolicy migrateStoreAtURL:withManager:metadata:options:error:] + 84
11  CoreData                        0x36a02b58 -[NSStoreMigrationPolicy(InternalMethods) _gatherDataAndPerformMigration:] + 2188
12  CoreData                        0x3691a20e -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 3086

So, migrate the data asynchronously?

Was it helpful?

Solution

Starting an answer to update while we work through this.

causing app to crash while migrating, is not a memory crash. That is a watch dog crash.

Migrating large Core Data database crash has no crash log but I suspect it is probably a watchdog as well.

Core Data lightweight migration crash is a watchdog crash as well, not a memory crash.

Once we see your crash we can see what is going on.

Update 1

Ok, 0x8badf00d is our watch dog crash. This means your app is taking too long to launch.

How to fix this?

Don't stand up the core data stack in -applicationDidFinishLaunching... is the short answer.

A quick fix is to have the initial view of your app be identical to the launch image (i.e. splash screen). Then when you launch it finishes -applicationDidFinish... quickly (very quickly) and you can take as long as you want to migrate.

My recommendation (since you don't know how long the migration is going to take is to kick off is to:

  1. Check to see if a migration is necessary with -[NSManagedObjectModel isConfiguration: compatibleWithStoreMetadata:]
  2. Update the view display to let the user know that a migration is occurring
  3. Do the migration in a background block (put the -addPersistentStore... in a block and call it on a background queue, it is thread safe although it is best to put locks around it).
  4. Remove the temporary view when the migration is complete
  5. Launch your real UI.

That should be easy enough to change and gets you past the watchdog.

OTHER TIPS

The following new book on Core Data has code and explanation for how to do a background manual migration as described in the previous answer.

http://www.amazon.com/gp/aw/d/0321905768

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top