开发者

Stored NSFetchRequest returns error "A fetch request must have an entity"

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 requ开发者_JAVA技巧est 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


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.


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 as nil.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜