Nosrettap
Nosrettap

Reputation: 11320

How to remove many core data objects at once?

I'm writing an iOS app and I'm using Core Data to store a series of Event objects (think birthdays, or christmas or meeting...etc). Each Event object has a date attribute associated with it of type NSDate.

Throughout the course of my program I gather an NSArray of NSDate objects and I was wondering how do I delete the core data objects associated with these dates? For example, if I have an array consisting of the dates 11/7 and 12/9 how do I delete any Event objects that have a date attribute of 11/7 or 12/9?

One possibility that I've thought of is to fetch all of the corresponding Event objects using predicates and fetch requests and then turn around and delete them all; however, this seems kinda inefficient. What if one of my Event objects is really, really large...then won't it take forever to fetch it, just so that I can turn around and delete it?

I'm just wondering if there is a more elegant way to handle this...Thanks

Upvotes: 1

Views: 127

Answers (1)

Tim
Tim

Reputation: 60110

You can handle this with a single fetch - use the IN operator to construct a predicate using your NSArray of dates as the collection, execute the fetch request (which, as Mike said above, will still be relatively quick), then delete the objects. See Aggregate Operators in the Predicate Programming Guide for more info.

Upvotes: 1

Related Questions