美文网首页
Core Data编程指南 1-5: 查询数据对象

Core Data编程指南 1-5: 查询数据对象

作者: TalktoEason | 来源:发表于2017-07-18 18:10 被阅读0次

我们可以通过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对象,并调用 NSManagedObjectContextexecuteFetchRequest: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.

相关文章

网友评论

      本文标题:Core Data编程指南 1-5: 查询数据对象

      本文链接:https://www.haomeiwen.com/subject/fxmhkxtx.html