简体   繁体   English

将RSS源导入Core Data时防止重复

[英]Prevent duplicates when importing RSS feed to Core Data

Trying to import a RSS feed into Core Data. 尝试将RSS源导入Core Data。 Once they are imported, when trying to update the feed again afterwards, how do I most efficiently prevent duplicates. 导入后,尝试再次更新Feed时,如何最有效地防止重复。 Right now it checks every item against the datastore during the parsing, which is not very efficient. 现在它在解析期间针对数据存储区检查每个项目,这不是非常有效。

I looked into the Top Songs sample from Apple. 我查看了Apple的热门歌曲样本。 It uses a least recently used cache for categories. 它使用最近最少使用的缓存来进行分类。 But when every item is different the cache doesn't help at all. 但是当每个项目都不同时,缓存根本没有帮助。

EDIT: To clarify, I can already identify each item uniquely in the feed with guid. 编辑:为了澄清,我已经可以使用guid在feed中唯一标识每个项目。 The issue is the performance of comparing hundreds of items against the database every time, when most of them are duplicates. 问题是每次将数百个项目与数据库进行比较时的性能,当时大多数项目都是重复项目。

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 ,将谓词设置为查找guid属性并将返回的最大行设置为1。

I would recommend keeping this NSFetchRequest around during your import so that you can reuse it while going through the import. 我建议您在导入期间保留此NSFetchRequest ,以便NSFetchRequest导入时可以重复使用它。 If the NSFetchRequest returns a row you can update that row. 如果NSFetchRequest返回一行,您可以更新该行。 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. 如果操作正确,您会发现性能超出了可接受范围。

Can you modify your core data model ? 你能修改你的核心数据模型吗?

If you can I would add a "Hash" property to each feed entry to uniquely identify it. 如果可以的话,我会在每个Feed条目中添加一个“Hash”属性来唯一标识它。 Then you could efficiently detect wether a specific entry is already in your database or not. 然后,您可以有效地检测数据库中是否已存在特定条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM