Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long can core data migration take on startup?

I have seen successful core-data migrations of my 4Gb database on my iPad on application launch taking several minutes. And now suddenly, some users report crashes after installing a new version and the app is kicked out with a: failed to launch in time error.

I just tested again by restoring an old database and I am sure that core data migration can take way more than 10 seconds.

But other people are concerned it should not and try to take it to the background, or at least out of the run loop at launch time:

iPhone app launch times and Core Data migration

Can this have anything to to with other conditions, e.g. being connected to a power source? Or have a battery level of more than 50 %?

Update: I reproduced a crash by just starting the app on the device (unplugged) instead of debugging. Then I tried starting the app on the device with USB attached: Crash. Then started the app via the debugger: No crash (and the migration took about 4 minutes.)

Extra info: I have only enterprise users (about 75 of them) and they all have a database of 4.5Gb. Some users have no problem upgrading and some have. The upgrades all take minutes if they succeed. The crashes always come after 20 seconds. (And they keep crashing if you try again on these devices).

I followed the advice to place the migration out of the run loop, but I am still wondering why the old method works on some devices and not on others. All users are on iOS 7.

like image 322
Bjinse Avatar asked Jan 20 '14 13:01

Bjinse


2 Answers

This is a common launch problem. A Core Data migration can take any amount of time, 0 to N depending on the complexity of the model and the amount of data and the type of migration occurring.

Ideally you should not be creating your Core Data stack in the -applicationDidFinish... method and migration is one of the reasons.

My recommendation is to rework your launch so that you display something until the stack has initialized. This could be just your default image in a view. Then when the Core Data stack has initialized you can switch over to your full view controller stack.

I would also recommend taking this a bit further so that you can tell the user that a migration is in process and I would further put the migration on a background queue so that you can update the UI while the migration is happening.

Lastly, if you are doing a heavy migration, I would look into doing a lightweight migration instead. The lightweight migration is far faster as well as other benefits.

like image 110
Marcus S. Zarra Avatar answered Sep 20 '22 14:09

Marcus S. Zarra


If you look at the crash log it will likely say that the app was killed because it took too long to startup. The watchdog process kills apps that take too long to startup - >20 seconds I think. This is because the core data migration process was run during app startup.

I'd recommend you manually run the migration in the background. The following new book on Core Data has code and explanation for how to do a background manual migration.

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

like image 31
Jesse Avatar answered Sep 19 '22 14:09

Jesse