我们可以通过NSFetchRequest
查询已经存储在Core Data外部存储器中的数据。查询是Core Data最强大的功能之一。
Now that data is stored in the Core Data persistent store, you will use an NSFetchRequest
to access that existing data. The fetching of objects from Core Data is one of the most powerful features of this framework.
查询数据对象
Fetching NSManagedObject Instances
在下面这个例子中,我们创建了一个查询特定数据表的NSFetchRequest
对象,并调用 NSManagedObjectContext
的executeFetchRequest:error:
方法去查询数据。
In this example you start by constructing an NSFetchRequest
that describes the data you want returned. This example does not add any requirements to that data other than the type of entity being returned. You then call executeFetchRequest:error:
on the NSManagedObjectContext
and pass in the request along with a pointer to an error.
OBJECTIVE-C
------------
NSManagedObjectContext *moc = …;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if (!results) {
NSLog(@"Error fetching Employee objects: %@\n%@", [error localizedDescription], [error userInfo]);
abort();
}
SWIFT
-----
let moc = …
let employeesFetch = NSFetchRequest(entityName: "Employee")
do {
let fetchedEmployees = try moc.executeFetchRequest(employeesFetch) as! [EmployeeMO]
} catch {
fatalError("Failed to fetch employees: \(error)")
}
executeFetchRequest:error:
方法可能会有两种返回结果。一是一个包含若干个数据对象的 NSArray
对象,一是一个空指针nil
,代表Core Data中发生了某些错误。返回的数组也有可能是空的,代表查询成功,但未查询到符合条件的数据记录。
The executeFetchRequest:error:
method has two possible results. It either returns an NSArray
object with zero or more objects, or it returns nil. If nil is returned, you have received an error from Core Data and need to respond to it. If the array exists, you receive possible results for the request even though the NSArray may be empty. An empty NSArray indicates that there were no records found.
过滤结果
Filtering Results
Core Data支持多样化的查询条件。我们可以将一个 NSPredicate
对象添加到NSFetchRequest
中,用于查询符合特定条件的结果。例如,我们希望查询出名字叫“Trevor”的所有员工:
The real flexibility in fetching objects comes in the complexity of the fetch request. To begin with, you can add an NSPredicate
object to the fetch request to narrow the number of objects being returned. For example, if you only want Employee objects that have a firstName of “Trevor”, you add the predicate directly to NSFetchRequest
:
OBJECTIVE-C
-----------
NSString *firstName = @"Trevor";
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"firstName == %@", firstName]];
SWIFT
-----
let firstName = "Trevor"
fetchRequest.predicate = NSPredicate(format: "firstName == %@", firstName)
你也可以自定义查询结果的返回方式。比如你可以要求Core Data返回 NSDictionary
类型的数据结构,而不是 NSManagedObject
或者他的子类。你甚至可以在NSFetchRequest
中规定返回的NSDictionary
对象只包含Employee中的部分属性。
In addition to narrowing the objects being returned, you can configure how those objects are returned. For example, you can instruct Core Data to return NSDictionary
instances instead of fully formed NSManagedObject
instances. Further, you can configure the NSFetchRequest
so that those NSDictionary
instances only contain a subset of the properties available on the Employee entity.
更多详细内容,请参看NSFetchRequest
的参考文档。
For more information about NSFetchRequest
, see the class documentation.
网友评论