ios - 具有已定义属性值的核心数据对象的数量

标签 ios core-data

我正在使用以下获取请求来删除核心数据对象:

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/

相关文章:

ios - 在NSInteger的情况下设置 key 的值

swift - 从核心数据中保存 CKAsset

ios - 数据被覆盖 : Core Data

ios - AVCaptureSession 和 AVCaptureMovieFileOutput 帧时间戳

ios - 如何将 IB 中的自动布局约束从一个 View 复制到另一个 View ?

ios - 如何以编程方式更改名称以区分 Xcode 可视化调试器中的 UIView?

ios - Phonegap/Cordova Barcodescanner 在最新的 ios 13 上再次全屏显示

iphone - App Store 下载旧版本应用程序

arrays - 将核心数据库数据提取到数组中

ios - 创建复杂核心数据数据库的技巧