Saving Core Data Context before Crashing

天涯浪子 提交于 2020-01-05 08:19:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/15210276/saving-core-data-context-before-crashing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!