尝试从CoreData获取数据时NSPredicate抛出异常

时间:2016-03-25 12:55:05

标签: ios objective-c core-data nspredicate nsfetchrequest

我有以下CoreDataModel:

Core Data Model

每个公园都与一组检查有关,每次检查都与公园成反比。我正在从Web服务响应中将数据加载到园区中。因此,从Park的NSManagedObject子类内部,我需要查询具有特定inspectionIDassigmentID的所有相关检查(只有一个满足该条件)并创建它(如果不存在),我使用了以下功能:

- (Inspection *)getInspectionWithId:(NSInteger)inspectionId assignmentId:(NSInteger)assignmentId {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    //Crash in the following line
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY (self.inspections.inspectionID == %d AND self.inspections.assignmentID == %d)", inspectionId, assignmentId];
    [fetchRequest setPredicate:predicate];
    NSError *error = nil;
    NSArray *array = [[[Model sharedModel] managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    if (array == nil || array.count < 1) {
        NSEntityDescription *inspectionEntity = [NSEntityDescription entityForName:@"Inspection" inManagedObjectContext:[[Model sharedModel] managedObjectContext]];
        return (Inspection *)[[NSManagedObject alloc] initWithEntity:inspectionEntity insertIntoManagedObjectContext:[[Model sharedModel] managedObjectContext]];
    }
    else {
        return (Inspection *)array[0];
    }
}

但该应用程序崩溃时出现以下异常。我不确定谓词中的条件是否适用于此。有人可以帮忙吗?

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:'无法解析格式   字符串“ANY(self.inspections.inspectionID ==%d AND   self.inspections.assignmentID ==%d)“'

0 个答案:

没有答案
相关问题