将RSS源导入Core Data时,防止重复 [英] Prevent duplicates when importing RSS feed to Core Data

查看:137
本文介绍了将RSS源导入Core Data时,防止重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试将RSS Feed导入Core Data。导入之后,当尝试再次更新Feed时,如何最有效地防止重复。现在,它在解析过程中会检查每个项目与数据存储区,这不是很有效率。

Trying to import a RSS feed into Core Data. Once they are imported, when trying to update the feed again afterwards, how do I most efficiently prevent duplicates. Right now it checks every item against the datastore during the parsing, which is not very efficient.

我查看了苹果的Top Songs示例。它使用最近最少使用的缓存作为类别。但是当每个项目不同时,缓存根本不起作用。

I looked into the Top Songs sample from Apple. It uses a least recently used cache for categories. But when every item is different the cache doesn't help at all.

编辑:
为了说明,我已经可以在Feed中唯一标识guid。问题是每次将数百个项目与数据库进行比较,而大多数项目是重复的。

To clarify, I can already identify each item uniquely in the feed with guid. The issue is the performance of comparing hundreds of items against the database every time, when most of them are duplicates.

推荐答案

正在导入新行时,您可以针对现有行运行查询,以查看其是否已就位。为此,您需要为您的实体创建一个 NSFetchRequest ,设置谓词以查找guid属性,并将最大行数设置为1.

When you are importing a new row you can run a query against the existing rows to see if it is already in place. To do this you create a NSFetchRequest against your entity, set the predicate to look for the guid property and set the max rows returned to 1.

我建议在导入过程中保持 NSFetchRequest ,以便在导入过程中重用它。如果 NSFetchRequest 返回一行,您可以更新该行。

I would recommend keeping this NSFetchRequest around during your import so that you can reuse it while going through the import. If the NSFetchRequest returns a row you can update that row. If it does not return a row then you can insert a new row.

如果没有返回一行,那么您可以插入一个新行。

When done correctly you will find the performance more than acceptable.

这篇关于将RSS源导入Core Data时,防止重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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