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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:58:22+00:00 2026-05-24T23:58:22+00:00

I am facing an issue in updating to core data using batch updating on

  • 0

I am facing an issue in updating to core data using batch updating on a background thread. In the below code I am using main thread to notify user with a progress view and a string which calls a method in appdelegate. But if I am getting bad access error in the NSEntity line in random number of data where I have thousands of objects to update.if I uncomment the NSLOG i have indicated below there is no error, or if i comment the main thread no error, or if I dont update through batch instead if I use bulk update then also no error. IF i comment autorelease pool also the error is appearing. Will some body help me on this please.

Thanks in advance,

Cheers,
Shravan

NSAutoreleasePool *tempPool = [[NSAutoreleasePool alloc] init];
NSUInteger iterator = 1;

for (int i = 0; i < totalNo; i++) {
    NSDictionary *alertResult = [[alertResultList objectAtIndex:i] retain];
    if (alertResult == nil) {
        continue;
    }

    //managedObjectContext = [appDelegate.managedObjectContext retain];

    NSLog(@"Object Count:%u", [[managedObjectContext insertedObjects]count]);

    AlertResult *result = (AlertResult *)[NSEntityDescription 
                                          insertNewObjectForEntityForName:@"AlertResult" 
                                          inManagedObjectContext:managedObjectContext];
    [result setUserName:@"A"];


    iterator++;


    //When count reaches max update count we are saving and draining the pool and resetting the pool
    if (iterator == kUploadCount) {
        if ([self update] == NO) {
            // If unable to update Alert results in the Core Data repository, return 
            // a custom status code.
            statusCode = -1;
        }
        [managedObjectContext reset];
        [tempPool drain];

        tempPool = [[NSAutoreleasePool alloc] init];
        iterator = 0;
    }


    //Adding code to change the display string for the lock view to notify user
    float count1 = (float)(counter/totalAlerts);
    counter = counter + 1.0f;
    NSString *dispStr = [NSString stringWithFormat:@"%f",count1];//[NSString stringWithFormat:@"Loading %d out of %d alerts",(i+1),totalAlerts];
    NSString *dispMess = [NSString stringWithFormat:@"Alerts %d of %d",(i+1),totalNo];
    [self performSelectorOnMainThread:@selector(changeLockScreenMessageWith:) withObject:[NSArray arrayWithObjects:dispStr,dispMess, nil] waitUntilDone:YES];
    //NSLog(@"count"); /* If I uncomment this line code runs fine */

    [alertResult release];
    alertResult = nil;
}

//If count is inbetween the update limit we are updating and we are draining the pool
    if (iterator != 0) {
        if ([self update] == NO) {
            // If unable to update Alert results in the Core Data repository, return 
            // a custom status code.
            statusCode = -1;
        }
        [managedObjectContext reset];
        //[tempPool drain];
    }
//Out side the previous method

- (BOOL)update {

    NSError *error;

    if (![managedObjectContext save:&error]) {
        NSLog(@"%@", [error userInfo]);
        return NO;
    }

    return YES;
}
  • 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-24T23:58:23+00:00Added an answer on May 24, 2026 at 11:58 pm

    The most likely cause of the kind of crash you’re describing is using your managedObjectContext across threads. managedObjectContext is not thread-safe. You must create a new MOC for each thread. I assume managedObjectContext is an ivar; you should never access your ivars directly like this (except in init and dealloc). Always use an accessor to handle memory management for you.

    The reason NSLog makes it crash is because NSLog dramatically changes the timing of this function, and you have a race condition.

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

Sidebar

Related Questions

I am facing strange issue. I have wep-part that reads data from user's profile.
I'm facing a issue with opening mail client using openURL. Here is the code.
I am using Appweb server (mini http server) and facing an issue while opening
I'm working on a project which uses user authentication. I'm facing a issue with
i am doing a database design using EAV. I am facing an issue when
I am using ASP.NET MVC2 with NHibernate, but am facing an issue. All calls
I am facing issue with proper date retrieval using date formatter.I have saved the
I am facing an issue regarding EF code first version while mapping one entity
I am using below code to call webservice using javascript: <head> <title>Untitled Page</title> <script
I am facing an issue in jqgrid's treegrid. For local data the tool barserch

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.