最近使用Core Data进行搜索 [英] Recent searches with Core Data

查看:104
本文介绍了最近使用Core Data进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPhone制作核心数据应用程序,但希望这个问题适用于Mac OS X在这种情况下回答没有NDA问题。

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?

我考虑的一种方法是添加另一个名为History的实体,并将一个Entry属性链接到此实体。第一搜索结果将具有Entry.history_id = 1,第二搜索结果将具有Entry.history_id = 2,等等。然后,最近的搜索将选择具有给定历史ID的条目实体。

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.

推荐答案

如果你想要的是搜索的有序列表,您是正确的:您将必须手动管理一些指定顺序的属性。您提供的 history_id 是一种可能性。我可能使用在插入实体实例时设置的日期属性。然后,您可以按此date属性对您的抓取进行排序。为了存储实际的搜索,我经常将查询的NSPredicate实例序列化为一个数据属性(自己处理序列化/反序列化)或一个可转换的(使用默认的NSUnarchiveFromData传输)属性。

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.

这篇关于最近使用Core Data进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆