我正在使用以下获取请求来删除核心数据对象:
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
是 transient 属性。
最佳答案
要检查有多少具有给定属性值的对象存在,使用countForFetchRequest:
:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"entityName"];
[request setPredicate:[NSPredicate predicateWithFormat:@"value1 = %@", @"borrar"]];
NSError *error;
NSUInteger count = [self.moc countForFetchRequest:request error:&error];
if (count == NSNotFound) {
// some error occurred
} else if (count > 1) {
// more the one object with "value1 == borrar"
}
更新(根据已编辑的问题):您不能使用 transient 属性 在核心数据获取请求中。如果“value1”是一个临时属性,你只能 使用“value2 == something”获取所有对象,然后遍历获取的对象 数组来检查是否有多个对象具有“value1 == borrar”。
关于ios - 具有已定义属性值的核心数据对象的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21361401/