【发布时间】:2014-02-17 03:25:53
【问题描述】:
我正在使用以下获取请求来删除核心数据对象:
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];
}
我需要的是仅当核心数据实体中[value1 isEqualToString: @"borrar"] 的对象数大于1 时才执行获取请求。如何添加此条件?
***编辑
属性value1 是一个临时属性。
【问题讨论】:
-
“borrar”是谓词中已使用的值
data1或data2之一吗? -
是的,它是值之一,但如果 countForFetchRequest 计算对象的数量,我可以更改谓词并仅考虑 data2。
-
所以你想删除所有具有 value1 == "borrar" 和 value == "someOtherString" 的对象,但只有如果至少有两个对象具有 value1 == “博拉尔”?
-
正是,这正是我所需要的。
-
您不能在核心数据获取请求中使用瞬态属性。我已经相应地更新了答案。