根据核心数据实体的两个属性删除对象

时间:2014-01-26 02:01:45

标签: ios core-data

在我的应用程序中,如果两个属性值等于字符串变量,我需要删除核心数据对象。必须通过按钮操作完成。我该如何确定要删除的对象?

1 个答案:

答案 0 :(得分:1)

 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];
  }