问题
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