What I\'m trying to accomplish is the following: I need to limit the amount of core data entries to 50. So if the user enters their 50th entry then the app would delete the
Let's say you have an entity called History
. The easiest solution would be to add a creationDate
attribute to your entities. Then use that to manage your History
objects.
You will need three fetches:
History
objects and then count them. If the count is <50, then just add the new History
object and your done. creationDate
. (As luck would have it the example at the link it pretty much exactly what you need.) creationDate
returned by (2) and delete it. Then add the new history object.
OK, that's fine. CoreData is not going to do this for you, but you can do it yourself.
You can retrieve objects from you context using an NSFetchRequest
, and you can delete them using -[NSManagedObjectContext deleteObject:]
. You can sort them using NSSortDescriptor
objects.