mvasco
mvasco

Reputation: 5101

Deleting objects depending on two attributes from core data entity

In my app I need to delete core data objects if two attributes values are equal to a string variable. That must be done from a button action. How should I determine the objects to be deleted?

Upvotes: 0

Views: 41

Answers (1)

codercat
codercat

Reputation: 23271

 NSEntityDescription *entity=[NSEntityDescription entityForName:@"entityName" inManagedObjectContext:context];
  NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
  [fetch setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(value1 == %@) AND (value2 == %@)", data1, data2];
 [fetch setPredicate:predicate];
  //... add sorts if you want them
  NSError *fetchError;
  NSArray *fetchedData=[self.moc executeFetchRequest:fetch error:&fetchError];
 for (NSManagedObject *product in fetchedProducts) {
    [context deleteObject:product];
  }

Upvotes: 1

Related Questions