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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:49:39+00:00 2026-06-04T05:49:39+00:00

I am having usues with performance when inserting 2000 records in my Core Data

  • 0

I am having usues with performance when inserting 2000 records in my Core Data application..

The data is being downloaded from a server as a big JSON file, and parsed into a JSON dictionary, all this is well, and the time to passe the dictionary is nothing…

The issue is that each insert into my database takes longer and longer ?

During my import, i save the context for each 100 docs to keep the memory down, and the first document takes 0.005434ms to save, the last takes 0.039297ms to save..

I am doing all this import in a seperate thread with a completely new ManagedContext with undomanager set to nil..

This is the loop that runs through all the docs in the dictionary

    NSArray *docs = [docsData objectForKey:@"docs"];
    for(NSDictionary *doc in docs){
        if(counter++ % 100){
            [context save:nil];
        }
        NSDate *start = [NSDate date];
        [Document documentWithDictionary:doc lastModifiedDate:[NSDate date] inLevels:nil inManagedObjectContext:context];
        NSDate *end = [NSDate date];

        NSLog(@"time used pr doc = %f",[end timeIntervalSinceDate:start]);
    }
    [context save:nil];

And here is the code that inserts the doc

     NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"Document"];
    req.predicate = [NSPredicate predicateWithFormat:@"id = %@", [data valueForKey:@"id"]];
    NSArray *matches = [context executeFetchRequest:req error:&error];

    if(matches){    
        if([matches count]){
            document = [matches lastObject];
        }else {
            document = [NSEntityDescription insertNewObjectForEntityForName:@"Document" inManagedObjectContext:context];
        }
   }

Can someone shed any light as to why the inserts takes longer and longer?

In the simulator the used time pr doc is pretty much constant, but on the phone it is not ?

This might be a minor issue, but since i can have anywhere from 2000 to 30000 records in the database, this actually becomes a factor with large imports..

Thanks a lot 🙂

/Jacob

UPDATE —–

After doing ONLY insertions in the database, that is, with no fetch for existing records, these are the times..

WITH Fetch:

1100 docs – 54.6s

2349 docs – 194.9s

1872 docs – 222.1s

WITHOUT Fetch.

1100 docs – 34.4s

2349 docs – 74.19s

1872 docs – 59.1s

So, the conclution is that it is my fetch request with the increasing amount of docs that is taking longer and longer.. but that also makes sense 🙂 don’t know why i did’nt think of that earlier… So now the solution is to check if the sync is the first one, and then import documents without the fetch for any existing document.

/Jacob

  • 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-06-04T05:49:41+00:00Added an answer on June 4, 2026 at 5:49 am

    Based on your code, the issue has nothing to do with inserting anything. Nothing actually gets persisted to the database until you call the save method. I’m assuming the “…code that inserts the doc” is the code in the documentsWithDictionary:lastModifiedDate:inLevels:inManagedObjectContext: method. You are not actually inserting anything here, but creating a new ManagedObject in memory. However, you ARE querying the database every time you do this. As the number of records in the database grows, the query could take slightly longer to find a record of the given id.

    Apple has outlined some good practices for efficiently importing large data sets: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/coredata/Articles/cdImporting.html#//apple_ref/doc/uid/TP40003174-SW1

    In particular you will want to read “Implement Find-or-Create Efficiently”. Following their guidelines you can limit your database reads to one per batch of records, or simply once for the entire data set you’re importing.

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

Sidebar

Related Questions

I'm having a problem compiling an application which uses Core-Plot in debug mode. It
Is there an appreciable performance difference between having one SELECT foo, bar, FROM users
I'm having a bit of trouble with understanding JMS from a performance perspective. We
I'm having trouble deploying .NET application which uses Microsoft Access automation. I've installed the
I recently became involved with a new software project which uses SQL Server 2000
I have an application which reads in gridded coordinate data with values (i.e. lat,
I am having performance issues with MongoDB. Running on: MongoDB 2.0.1 Windows 2008 R2
I am working with a Java prepared statement that gets data from an Oracle
I'm reading Dissecting SQL Server Execution Plans from Grant Fritchey and it's helping me
I am having a C# (.NET 3.5, VS2005 Professional) application that uses unmanaged 32bit

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.