iPhone app launch times and Core Data migration

后端 未结 5 1547
有刺的猬
有刺的猬 2021-02-02 01:51

I have a Core Data application which I plan to update with a new schema. The lightweight migration seems to work, but it takes time proportional to the amount of data in the dat

5条回答
  •  孤街浪徒
    2021-02-02 02:03

    Sorry Marcus, I have to respectfully disagree. You can migrate in the background.

    My migriation runs on a background thread. It can take over 10 seconds on slow devices, so I start it on a background thread and have a specific modal view controller to show progress.

    The way to do this is split your normal loading sequence into two phases.

    Phase 1) Do everything you would normally do at launch that doesn't require managed objects. The end of this phase is defined by a check to determine if migration is required.

    Phase 2) Do everything that normally happens at launch that requires managed objects. This phase is an immediate continuation of Phase 1) when no migration was required.

    This way, your app finishes launching regardless of the duration of migration processing.

    To perform a long migration successfully, I use a modal view controller showing feedback of the migration progress to the user. I then commence the migration in the background thread, while the modal view controller uses KVO to update it's progress bar.

    At the end of the migration, it closes down the whole core data "stack", and a callback to the main thread will dismiss the modal and continue on to Phase 2).

    This whole process works flawlessly, although I still have an open question on a way to have the automatic lightweight migration reveal it's progress like the manual migration does.

提交回复
热议问题