简体   繁体   中英

What's the best way to limit the size of a Core Data store?

I have an app that will get Core Data objects from a server. The number of items may be very large. What's the best way to limit the number of items that Core Data will store so I don't use too much space on the phone? I was thinking that for ordered items, in applicationWillTerminate I could mark all but the first N items as toDelete and then delete them the next time the app starts (per this article http://inessential.com/2014/02/22/core_data_and_deleting_objects ). Any thoughts?

As often happens, what strategy is good depends on how people use your data. What data is more important to keep available? What is less important?

Keeping the first N items in an ordered relationship is a simple rule, and fairly easy to implement. But whether it's good for your app depends on what that data is, how a person would use it, and whether not having the rest of the related objects is likely to matter. You don't even need a toDelete flag, you just need to know the value of N . But keep in mind that you can't rely on applicationWillTerminate actually being called, so it's a bad place to put critical code.

Other strategies might include:

  • Delete the oldest data as measured by the length of time since it was downloaded. Local data matches what's newest on the server.
  • Delete the oldest data as measured by the length of time since the user has accessed it. Local data matches what the user is interested in, while also allowing for new data from the server.

These are more complex, requiring date tracking in your persistent store. Only you can really say whether the advantages are worth that complexity.

Starting out though, a more important question is: does this even matter? How many items is "very large"? Does a "very large" number of items translate into a lot of data, or just a lot of little items?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM