Recent searches with Core Data

北城以北 提交于 2020-01-14 05:43:13

问题


I'm making a Core Data app for the iPhone, but hope the question applies enough to Mac OS X to be answered in that context with no NDA issues.

When a user does a search, I am saving each result into a context using entity Entry. I want these results to be retrievable later under a Recent Searches section. What's the best way to go about this?

One way I'm considering is add another entity named History with an Entry attribute linking to this. First search results would have Entry.history_id = 1, second results would have Entry.history_id = 2, and so forth. Then Recent Searches would select Entry entities with a given history id.


回答1:


If what you want is an ordered list of searches, then you are correct: you will have to manually manage some property that specifies the order. The history_id you give is one possibility. I would probably use a date attribute that is set at insertion of the entity instance. You can then order your fetch by this date property. For storing the actual search, I've often serialized the NSPredicate instance for the query into either a data property (handling the serialization/deserialization myself) or into a transformable (using the default NSUnarchiveFromData transfer) property.



来源:https://stackoverflow.com/questions/860253/recent-searches-with-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!