views:

187

answers:

1

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.

A: 

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.

Barry Wark
One advantage of a History entity is I can store a title to label the set of search results (was not an original requirement). I do like the data idea to sort by and archiving NSPredicate for later retrievals.
baalexander
Yes, this is all within a History entity: name (String), date (Date), predicate (Transformable) attributes.
Barry Wark