executeFetchRequest在空核心数据上返回nil

时间:2014-08-07 04:26:19

标签: ios core-data

我在Core Data中遇到了一个奇怪的问题。基本上,我有一个空的核心数据,有几个模型,如学生和教师。我试着执行以下代码:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
request.predicate = [NSPredicate predicateWithFormat:@"(username = %@) AND (password = %@)", username, password];

// Determines if there is a match for student
NSError *error;
NSArray *matches = [self.context executeFetchRequest:request error:&error];
if (error || !matches || [matches count] > 1) {
    NSLog(@"Error in retrieving login match for student");
}
else if ([matches count]) {
    student = [matches firstObject];
}

奇怪的是,matches原来是nil,错误信息被打印出来。不应该匹配一个空数组,因为空核心数据只是意味着上下文无法找到满足这些条件的NSManagedObject吗?

1 个答案:

答案 0 :(得分:2)

简短回答=是的,它应该。

根据Apple doc

  

executeFetchRequest的返回值...满足请求指定条件的对象数组   从接收器和相关的持久存储中获取   与接收者的持久性商店协调员。 如果发生错误,   返回nil 如果没有对象符合请求指定的条件,   返回空数组

因此,请检查您的NSError

相关问题