I’ve been working on an RSS Reader, using core data for caching. Like a lot of people, I wanted to avoid duplicate entries, which led me to this question, and also this one.
But, there was another thing I wanted, I also wanted to give users the ability to delete articles, and avoid adding deleted articles again when refreshing a feed, that is if the deleted article still existed in the feed. So, my solution currently, is, to maintain another entity in my managed object context with unique identifiers (which how I identify each item in the feed) of deleted articles, I just add the identifier of the article that is being deleted to that entity, and check against it.
Now, here is a piece of code that I wrote to accomplish all of the above. This code is run every time a new item in the feed is parsed, during the parsing process.
dispatch_queue_t checkQueue = dispatch_queue_create("com.feedreader.backgroundchecking", NULL);
dispatch_async(checkQueue,^{
NSMutableArray *mutablesortedArticles = [NSMutableArray arrayWithArray:self.feeds.sortedArticles];
if (!mutablesortedArticles) {
// Handle the error.
}
if ([[mutablesortedArticles valueForKey:@"identifier"]
containsObject:article.identifier]) {
NSLog(@"This article already exists");
return;
}else {
NSMutableArray *mutabledeletedArticles = [NSArray arrayWithArray:self.alldeletedArticles];
if (!mutabledeletedArticles) {
// Handle the error.
}
if ([mutabledeletedArticles valueForKey:@"identifier"]
containsObject:article.identifier]) {
NSLog(@"This article has been deleted");
return;
}else {
Article *newArticle = [NSEntityDescription insertNewObjectForEntityForName:@"Article" inManagedObjectContext:self.managedObjectContext];
newArticle.title = article.title;
newArticle.date = article.date;
newArticle.link = article.link;
newArticle.summary = article.summary;
newArticle.image = article.image;
newArticle.identifier = article.identifier;
newArticle.updated = article.updated;
newArticle.content = article.content;
newArticle.feed = self.feed;
dispatch_async(dispatch_get_main_queue(),^{
NSError *error = nil;
[self.managedObjectContext save:&error];
if (error) {
NSLog(@"%@", error);
}
});
}
}
});
Both, self.feeds.sortedArticles and self.alldeletedArticles are fetched from managed object context before parsing starts.
My problem begins when this code is being run, the UI freezes, for 1-2 seconds (I tried it with a feed that had a little more than 500 articles in the managed object context). So, I guess my question is, is there a more efficient way to do what I’m trying to do here, one that hopefully doesn’t freeze the UI?
Perhaps a better way of dealing with deleted articles?
My first suggestion would be to handle deleted articles by added a “itemDeleted” property to the Article entity. Then you have only one list of objects to check when inserting new items.
(Hint: Don’t call that attribute “deleted”.
isDeletedis a built-in property ofNSManagedObject, so that is likely to cause name collisions.)The next suggestion is to save the managed object context only after all items have been imported, and not after each each item (EDIT: See also Caffeine’s answer, which was posted while I was writing this.)
Finally, searching each new item in the list of all articles separately is a pattern that does not scale well. Implementing Find-or-Create Efficiently in the “Core Data Programming Guide” describes a pattern that might be better: