存储的 NSFetchRequest 返回错误“获取请求必须具有实体”
我在数据模型中将获取的属性添加到我的实体中。我没有添加它的谓词。我使用 fetchRequestTemplateForName 检索它,但是当执行它时,我收到此错误“executeFetchRequest:错误:获取请求必须有一个实体。”
但我怎么会出现这个错误呢。获取的属性不是我实体的属性吗?
于是我进一步调试问题,发现是因为检索到的 NSFetchRequest == nil
[[self ManagedObjectModel] fetchRequestTemplateForName:@"somename"];
那么我该如何解决它呢?
谢谢!
邱
I added a fetched property to my entity at data model. I did not add its predicate. I retrieve it with fetchRequestTemplateForName, but when execute it I got this error "executeFetchRequest:error: A fetch request must have an entity."
But how come I get this error. Isn't that fetched property a property of my entity ?
So I further debug the issue and find that it was because the retrieved NSFetchRequest == nil
[[self managedObjectModel] fetchRequestTemplateForName:@"somename"];
So how do I fix it ?
Thanks!
Qiu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您回答我的问题。我按照您的建议添加了谓词,但 NSFetchRequest 仍然返回 nil。然后我突然意识到我实际上添加了 Fetched Property 而不是 Fetch Request 到我的实体,这就是 fetchRequestTemplateForName 返回 nil 的原因。因此,我添加了 Fetch Request(在编辑器菜单中有“Add Fetch Request”和“Add Fetched Property”菜单项),这解决了问题!很抱歉造成混乱:$。
但我的问题是,为什么我们需要获取属性,因为我们已经有获取请求?我知道苹果文档说了类似“获取的属性代表弱的单向关系”之类的内容。但我仍然感到困惑,b/c fetch 请求似乎更简单。
Thanks for answering my question. I added predicate as you suggested but NSFetchRequest still returned nil. Then I suddenly realized that I actually added Fetched Property NOT Fetch Request to my entity and that's the reason fetchRequestTemplateForName return nil. So I add Fetch Request (in Editor menu there are "Add Fetch Request" and "Add Fetched Property" menu items) and that fixed the problem! So sorry for the confusion :$.
But my question then is why we need fetched property as we already have fetch request ? I know apple document said something like "Fetched properties represent weak, one-way relationships." But I still feel confused b/c fetch request seems more straightforward.
我假设您在 Xcode 模型编辑器中创建了获取的属性,而不是以编程方式创建的。您需要填写谓词,否则无法获取任何内容。因此,
NSFetchRequest
返回为nil
。获取的属性实际上只是一个方便的快捷方式,用于在代码中一次又一次地写出冗长的获取请求,以获得您不想保留的结果。也许首先看看用代码编写的
NSFetchRequest
是否有效,然后将其传输到模型是一个很好的过程。I assume you created your fetched property in the Xcode model editor and not programmatically. You need to fill out the predicate, otherwise there is nothing to fetch. Therefore, the
NSFetchRequest
comes back asnil
.A fetched property is really just a handy shortcut for writing out long-winded fetch requests in code time and again for results you do not want to persist. Maybe it is a good process to first see if your
NSFetchRequest
written in code works, and then transfer it to the model.