Reputation: 609
I have object like this
@interface Clubs : NSManagedObject
@property (nonatomic, retain) NSString * catname;
@property (nonatomic, retain) NSString * clubdescription;
@property (nonatomic, retain) NSString * clubid;
@property (nonatomic, retain) NSString * clubname;
@property (nonatomic, retain) NSString * distance;
@property (nonatomic, retain) NSString * logopath;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * clubtype;
@end
I want to delete only those object have are particular clubid?
Suppose there are twenty clubs are added to CoreData, only these object I want to delete whose clubid is 120.
Thanks
Upvotes: 0
Views: 1460
Reputation: 108101
There's no batch Core Data method for deleting a set of objects matching a predicate. You will have to retrieve them and delete them by iterating through the results.
NSManagedObjectContext *moc = // get the managed object context
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Clubs"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"clubid == 120"];
NSError *error = nil;
NSArray *entitiesToDelete = [moc executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *entity in entitiesToDelete) {
[moc deleteObject:entity];
}
[moc save:&error];
if (error) {
// handle error
}
Upvotes: 1
Reputation: 4091
-(void)deleteClubsEntity:(NSString*)clubId // pass the value 120
{
NSError *error = nil;
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Clubs" inManagedObjectContext: context]];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"(clubid
contains[cd] %@)",clubId]];
NSArray * records = [context executeFetchRequest:fetch error:&error];
for (NSManagedObject * record in records) {
[context deleteObject:record];
}
[context save:&error];
}
Upvotes: 1