I have an entity in my core data model like this:
@interface Selection : NSManagedObject
@property (nonatomic, retain) NSString * book_id;
@property (nonatomic, retain) NSString * contenu;
@property (nonatomic, retain) NSNumber * page_id;
@property (nonatomic, retain) NSNumber * nbrOfOccurences;
@property (nonatomic, retain) NSString * next;
@property (nonatomic, retain) NSString * previous;
I have created many Selections and saved them in Core Data and now I would like to delete some selections with some criteria. For example, I would like to delete a Selection object if matches the following:
content = test
page_id = 5
book_id = 1331313
How I can do this?
What Mike Weller wrote is right. I’ll expand the answer a little bit.
First you need to create a
NSFetchRequestlike the following:Then you have to set the predicate for that request like the following:
where
I’m using
%@since I’m supposing you are using objects and not scalar values.Now you perform a fetch in the context with the previous request:
resultscontains all the managed objects that match that predicate.Finally you could grab the objects and call a deletion on them.
Once done you need to save the context as per the documentation.
Hence
Note that
savemethod returns a bool value. So you can use an approach like the following or display an alert to the user. Source NSManagedObjectContext save error.