I need to store a large number of json feeds into Core Data. There are so many feeds, that I can’t call them all at once, because it takes too long for the app to load. So I call the most important ones when the app launches, and divide the rest of them throughout the app. However, I also need the store these into Core Data.
The first view is just a tableview, with names in them. When you click on a name, you’re sent to the next view, where there are supposed to be some text and a few pictures. So when the second view loads, I send a request to a webservice and store the answer for the service in Core Data.
What happens is that if you click the same tableviewcell twice, the info from the service called will be written to Core Data twice, creating duplicates values. I want to avoid this.
What I want to do is check if my entity already contains one of the unique values. If it does, don’t store in Core Data. If not, store it.
Here’s my method:
-(void)addImage:(NSManagedObjectContext *)context withImageUrl:(NSString *)imageUrl{
NSFetchedResultsController *fetchedResultsController = [self fetchObjectsFromEntity:@"Image" withContext:context andSortKey:@"imageUrl"];
[fetchedResultsController.fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"imageUrl contains[cd] %@" , imageUrl]];
if (//WHAT TO PUT HERE?) {
NSLog(@"Doesn't exist");
Image *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context];
image.imageUrl = imageUrl;
}else{
NSLog(@"Already exists");
}
-(NSFetchedResultsController *)fetchObjectsFromEntity:(NSString *)entityName withContext:(NSManagedObjectContext *)context andSortKey:(NSString *)key{
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
[fetchRequest setEntity:entity];
if (key != nil) {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:key ascending:YES];
NSArray *sortDescs = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescs];
}
NSFetchedResultsController *fetchedResultController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Root"];
if (![fetchedResultController performFetch:&error]) {
NSLog(@"Error: %@" , error);
}
return fetchedResultController;
}
Does anyone have an idea of how I can achieve this? Is the predicate wrong? How do I check if the predicate is true or not?
Any help would be appreciated!
What you’re doing is going to be very expensive. You have to remember that each time you execute an
NSFetchRequestyou’re going to execute SQL (expensive) and do I/O (also expensive). Your goal is to limit to number of times you do that.If you can get a list of (say 10) unique keys (in your case the imageURL) and then execute the fetch request to see which ones of those imageURLs are already in the store, that will be a lot cheaper (a factor of roughly 10 in this case).
Also, since you’re not interested in the actual objects, but just whether they exist, you should set the fetch request’s
resultTypetoNSDictionaryResultTypeand setpropertiesToFetchto an array containing just your keym i.e.[NSArray arrayWithObject:@"imageUrl"].Finally, comparing strings is very expensive. Try not to use
@"imageUrl contains[cd] %@". It required the database to do a lot of work. If you have some numeric key, e.g. a 64 bit number, that will makes things a lot faster, since you can use numeric comparison.Putting all of this together, let’s assume your unique keys attribute is called
uniqueID. You can run a fetch request like thisHope this helps.