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 7170853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:17:42+00:00 2026-05-28T15:17:42+00:00

I have a large list of Users in an NSDictionary that has a structure

  • 0

I have a large list of Users in an NSDictionary that has a structure like this:

        97 =             {
            birthday = "";
            gender = Unspecified;
            image =                 {
                status = Prepared;
                type = Image;
            };
            "name_display" = "Facebook User";
            "name_first" = Facebook;
            "name_last" = User;
            type = Contact;
            "user_id" = 97;
        };
        98 =             {
            birthday = "";
            gender = Unspecified;
            image =                 {
                status = Prepared;
                type = Image;
            };
            "name_display" = "Facebook User";
            "name_first" = Facebook;
            "name_last" = User;
            type = Contact;
            "user_id" = 98
         }

I want to input this data into Core Data. First I must check if the user already exists in core data. If so, update that user. Otherwise create a new user. The way I am doing it works, but it is extremely slow. Here’s my code:

NSDictionary *users = [responseData objectForKey:@"users"];
if (users) {
    for (id userKey in [users allKeys]) {
        NSDictionary *contactDictionary = [users objectForKey:userKey];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID == %@", userKey];
        NSUInteger count = [CoreDataHelper countForEntity:@"Contact" withPredicate:predicate andContext:[FSAppDelegate managedObjectContext]];
        if (count > 0) {
            NSMutableArray *existingContactArray = [CoreDataHelper searchObjectsForEntity:@"Contact" withPredicate:predicate andSortKey:nil andSortAscending:NO andContext:[FSAppDelegate managedObjectContext]];
            Contact *existingContact = [existingContactArray objectAtIndex:0];
            [CoreDataHelper updateContactWithDictionary:contactDictionary forContactObject:existingContact];
        }
        else {
            Contact *newContact = (Contact*)[NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:[FSAppDelegate managedObjectContext]];
            [CoreDataHelper updateContactWithDictionary:contactDictionary forContactObject:newContact];
        }
    }

    NSError *error;
    if (![[FSAppDelegate managedObjectContext] save:&error]) {
        // Handle the error.
        NSLog(@"error saving to db - fsrequest class.");
    }
}

And here is my method to update the contact

+(BOOL)updateContactWithDictionary:(NSDictionary*)changedContact forContactObject:(Contact*)contact {
NSString *bday = [changedContact valueForKey:@"birthday"];
NSString *gender = [changedContact valueForKey:@"gender"];
NSString *nameDisplay = [changedContact valueForKey:@"name_display"];
NSString *nameFirst = [changedContact valueForKey:@"name_first"];
NSString *nameLast = [changedContact valueForKey:@"name_last"];
NSString *type = [changedContact valueForKey:@"type"];
NSString *userID = [NSString stringWithFormat:@"%@",[changedContact valueForKey:@"user_id"]];
NSString *imageStatus = [[changedContact objectForKey:@"image"]objectForKey:@"status"];
NSString *imageType = [[changedContact objectForKey:@"image"]objectForKey:@"type"];
NSString *imageURL = [[changedContact objectForKey:@"image"]objectForKey:@"url"];
NSString *imageThumb = [[changedContact objectForKey:@"image"]objectForKey:@"url_thumb"];
NSString *locationName = [[changedContact objectForKey:@"location"]objectForKey:@"name"];

[contact setBirthday:bday];
[contact setGender:gender];
[contact setNameDisplay:nameDisplay];
[contact setNameFirst:nameFirst];
[contact setNameLast:nameLast];
[contact setType:type];
[contact setUserID:userID];
[contact setImageStatus:imageStatus];
[contact setImageType:imageType];
if (imageURL && !((NSNull *)imageURL == [NSNull null])) {
    [contact setImageURL:imageURL];
}
if (imageThumb && !((NSNull *)imageThumb == [NSNull null])) {
    [contact setImageThumbURL:imageThumb];
}
if (locationName && !((NSNull *)locationName == [NSNull null])) {
    [contact setLocationName:locationName];
}
return YES;
}

Can someone give me an example of how I would do this in a much faster way? Some people have mentioned some ideas, but I need to see it to understand. Thanks!

  • 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-28T15:17:43+00:00Added an answer on May 28, 2026 at 3:17 pm

    First of all I’d move save: outside the loop. Replace:

     // save core data
            NSError *error;
            if (![[FSAppDelegate managedObjectContext] save:&error]) {
                // Handle the error.
                NSLog(@"error saving to db - fsrequest class.");
            }
        }
    }
    

    with

        }
        // save core data
        NSError *error;
        if (![[FSAppDelegate managedObjectContext] save:&error]) {
        // Handle the error.
        NSLog(@"error saving to db - fsrequest class.");
      }
    }
    

    Also, do you have some default values for imageURL, imageThumb and locationName defined in Core Data model? If no, why do you check for nulls (twice)?

    Bonus:

    It may be a good idea to eliminate countForEntity:withPredicate:andContext: call, like this:

    NSMutableArray *existingContactArray = [CoreDataHelper searchObjectsForEntity:@"Contact" withPredicate:predicate andSortKey:nil andSortAscending:NO andContext:[FSAppDelegate managedObjectContext]];
    
    if ([existingContactArray count] > 0)
    {
        Contact *existingContact = [existingContactArray objectAtIndex:0];
    
        [CoreDataHelper updateContactWithDictionary:contactDictionary forContactObject:existingContact];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large table with list of scores like this, one score per
I have a webpage that displays a very large list of data. Since this
I have a large list of users that registered through a website without any
I have a large list [[1,.., ..],[2,...,...],[5,...,...],[1,...,...]] I need to remove all elements that
I have a large list of file names that are illegal, I want to
I have a very large list Suppose I do that (yeah, I know the
I have a (derived) Menu control, that displays a rather large list of items
I'd like to give users the ability to search through a large list of
We have to automatically import a large list of users with some data into
Here's an interesting problem, I have a list of users that I list in

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.