Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:53:05+00:00 2026-05-28T01:53:05+00:00

I need to store a large number of json feeds into Core Data. There

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:53:05+00:00Added an answer on May 28, 2026 at 1:53 am

    What you’re doing is going to be very expensive. You have to remember that each time you execute an NSFetchRequest you’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 resultType to NSDictionaryResultType and set propertiesToFetch to 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 this

    - (NSArray *)keysMissingInStoreFromKeys:(NSArray *)someKeys;
    {
        NSFetchRequest *r = [NSFetchRequest fetchRequestWithEntityName:@"Image"];
        [r setResultType:NSDictionaryResultType];
        [r setPropertiesToFetch:[NSArray arrayWithObject:@"uniqueID"];
        NSPredicate *p = [NSPredicate predicateWithFormat:@"uniqueID IN %@", someKeys];
        [r setPredicate:p];
        NSError *error = nil;
        NSArray *result = [moc executeFetchRequest:r error:&error];
        NSAssert(result != nil, @"Fetch failed: %@", error);
        NSMutableArray *missingKeys = [someKeys mutableCopy];
        for (NSDictionary *values in result) {
            id uniqueID = [values objectForKey:@"uniqueID"];
            [missingKeys removeObject:uniqueID];
        }
        return missingKeys;
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a database that can store a large number of BLOBS. The BLOBs
I need to store large amounts of metering data in a database. A record
I have a large amount of data I need to store, and be able
I need to store a large table (several millions or rows) that contains a
I need to store a growing large number of objects in a collection. While
I have a large number of objects I need to store in memory for
For a web application I'm developing, I need to store a large number of
I need to store a very large number (tens of millions) of 512-bit SHA-2
I need to store a large number of Long values in a SortedSet implementation
I need to store a very large number of files to a MySQL database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.