Coolant
Coolant

Reputation: 448

Saving Core Data Context before Crashing

For example if we hit "Stop" in XCode, it will close the app, mimicking the crash behaviour.

But if my Core Data Context hasn't been saved, when I go back, the data won't be there.

  1. Are there any workaround for this?

  2. Should I save the context every time a big operation is finished?

Thanks.

Upvotes: 1

Views: 899

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

Based on my experience you should decide the right granularity when you use Core Data save mechanism.

IMHO (maybe someone else could have different opinions) there is no standard to follow. My rule of thumb is taking into considerations two different aspects. The user and performances.

In the first case, you should save whenever the user performs critical operations. e.g. the user has inserted a lot of values in a form and hence he will expect to not insert them again. Regarding the second aspect, save operations could impact the performance of your app. If you frequently write changes to disk the app will be less responsive. On the contrary having so many objects in memory could led to memory warning (those will cause Core Data to take specific behaviors).

A tradeoff could be using background operations to save changes or take advantage of new Core Data API. Obviously, previous rules still remain valid.

Upvotes: 2

Related Questions