如何使用NSManagedObject的新fetchRequest函数创建获取请求? [英] How do I make a fetch request using NSManagedObject's new fetchRequest function?
问题描述
在iOS 10中,CoreData团队为NSManagedObject添加了一个新的fetchRequest方法。它看起来像这样:
In iOS 10 the CoreData team added a new "fetchRequest" method to NSManagedObject. It looks like this:
public class func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
根据我的理解,我们可以将其替换为:
Which, from what I understand, allows us to replace this:
let request = NSFetchRequest<MyEntity>(entityName: "MyEntity")
这个:
let request = MyEntity.fetchRequest()
然而,当我尝试提出这样一个简单的请求时:
However, when I try to make a simple request like this:
let request = MyEntity.fetchRequest()
do {
results = try request.execute()
} catch let error {
print("failed to fetch coffee object: \(error)")
}
I收到以下错误:
错误域= NSCocoaErrorDomain代码= 134060(null)
UserInfo = {message =无法获取在
范围内没有NSManagedObjectContext}
Error Domain=NSCocoaErrorDomain Code=134060 "(null)" UserInfo={message=Cannot fetch without an NSManagedObjectContext in scope}
因此,显然错误是说我需要将NSManagedObjectContext带入范围。我一直在寻找示例,但似乎可以找到如何使用新API功能执行请求的完整示例。
So, clearly the error is stating that I need to bring an NSManagedObjectContext into scope. I've been looking for examples but can seem to find a full example of how to perform a request using the new API features.
使用最新的Core Data API功能,如何进行简单的提取请求?基本问题是我如何将NSmanagedObjectCotnext带入范围。
Using the latest Core Data API features, how do I make a simple fetch request? The underlying question is how do I bring my NSmanagedObjectCotnext into scope.
我应该注意到我能够使用传统语法成功发出请求。
I should note that I am able to successfully make a request using the traditional syntax.
推荐答案
好的,我想出了两种方法。 第一种方法,与我的示例代码类似,只是简单:
Ok, I figured out two ways of doing this. The first approach, which is similar to my example code is just simply:
var moc: NSManagedObjectContext!
let request = MyEntity.fetchRequest()
var results : [MyEntity]
do {
results = try moc.fetch(request)
} catch { // error stuff}
第二种方法,已显示在WWDC 2016中,在我们的获取请求对象上使用名为execute的函数。
The second approach, which was shown in WWDC 2016 uses a function named "execute" on our fetch request object.
如果您查看有关NSFetchRequest的详细信息,您将看到一条注释,明确指出该操作必须在一个区块中执行。
If you view the details on NSFetchRequest you'll see a comment that clearly states that the operation must be performed with in a block.
// Executes the fetch request using the current managed object context. This method must be called from within a block submitted to a managed object context.
@available(iOS 10.0, *)
public func execute() throws -> [ResultType]
我突然意识到这是执行抓取的方式:
It's dawned on me that this was the way to perform the fetch:
var moc: NSManagedObjectContext!
let request = MyEntity.fetchRequest()
var results : [MyEntity]
moc.perform {
self.results = try! request.execute()
}
对于那些自己推荐的人
如果要滚动自己的代码,可能会发现代码无法编译。自动生成的代码中包含一个名为fetchRequest的新方法,编译器使用该方法传递类型。这是代码的样子。
If you are rolling your own code you may find that your code does not compile. Included in the auto-generated code is a new method called fetchRequest that the compiler uses to pass the type along. Here's what the code looks like.
@nonobjc public class func fetchRequest() -> NSFetchRequest<MyEntity> {
return NSFetchRequest<MyEntity>(entityName: "MyEntity");
}
这篇关于如何使用NSManagedObject的新fetchRequest函数创建获取请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!