Question

I use CoreData and I am thinking about when I should delete waste (or expired) data.

From what I can imagine the points are as follows:

  1. When the app launches.
  2. When the app terminates. (in the app delegate's applicationWillTerminate:)

One is no-risk, I guess, but I don't want users to wait during the purging. (I know the watch dog exception. If the purge time is over 20 seconds, I will prepare a view controller for waiting.)

Two looks nice. The users don't see the waiting time. However, I'm not sure whether this point is suitable or not? Some people say that background process has limited time.

Was it helpful?

Solution

Either way you will most likely benefit from running your clean up on a secondary thread. You will most likely not be able to use your second option though:

Per the Apple docs in relation to applicationWillTerminate:

Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.

One option you could use is cleaning up in applicationDidEnterBackground: but that also has some complications:

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your app is terminated and purged from memory.

This may be a better solution and you can dig into some recommendations via the documentation I linked to. It would probably easiest though to do it during application launch.

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