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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:16:12+00:00 2026-05-18T20:16:12+00:00

This is probably a very straight forward application, but I am new to Objective-C

  • 0

This is probably a very straight forward application, but I am new to Objective-C (coming from Java) and the whole memory management and “EXC_BAD_ACCESS” errors are breaking my heart.

I have a normal NavigationController iPhone App, with Core Data. in the AppDelegate the NSManagedObjectContext is created and passed to the RootViewController. A view things are looked up directly from the main thread to populate the table, and that seems to work fine.

The App is somekind of RSS-type reader, so as soon as the App starts I fire a thread to fetch new data and update the view:

-(void)updateData:(id)sender {
 UIActivityIndicatorView *activityIndicator =
    [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
 [activityIndicator startAnimating];
 UIBarButtonItem *activityItem =
    [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
 [activityIndicator release];
 self.navigationItem.leftBarButtonItem = activityItem;
 [activityItem release];

 // Start thread to update the data
 [NSThread detachNewThreadSelector:@selector(doUpdateData) toTarget:self withObject:nil];
}

-(void)doUpdateData{
 NSLog(@"Update data Thread (in 5 sec.)");
 [NSThread sleepForTimeInterval:5];

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 DataManager *data = [[DataManager alloc] initWithContext:managedObjectContext];
 [data updateData];
 [data release];
 data=nil;

 [self performSelectorOnMainThread:@selector(finishUpdateData) withObject:nil waitUntilDone:NO]; 
 [pool release];
}

-(void)finishUpdateData{
 self.navigationItem.leftBarButtonItem = updateBttn;
 DataManager *data = [[DataManager alloc] initWithContext:managedObjectContext];
 objects = [data getArticles];
 [data release];
 data=nil;
 NSLog(@"Amount of records after update: %d", [objects count]);
 [self.tableView reloadData]; 
}

The problem is that this doesn’t work. In the DataManager, first settings need to be retrieved, and as soon as the NSEntityDescription is created I get the “EXC_BAD_ACCESS”:

- (NSFetchedResultsController *)fetchedResultsController {
    // Set up the fetched results controller if needed.
    if (fetchedResultsController == nil) {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Setting" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];
  [fetchRequest setFetchLimit:1]; 
        .
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];

        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

        [aFetchedResultsController release];
        [fetchRequest release];
        [sortDescriptor release];
        [sortDescriptors release];
    }

 return fetchedResultsController;
} 

I guess the pointer to the ManagedObjectContext is wrong, as a result from running in a different thread and memory-pool. So how do you create such an application if that is the issue), how do I get a reference to the original ManagedObjectContext format he thread?

[EDIT]
I also tried to use

iDomsAppDelegate *appDelegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
DataManager *data = [[DataManager alloc] initWithContext:appDelegate.managedObjectContext];

in doUpdateData (as hinted by other posts), but that gives the same result

  • 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-18T20:16:13+00:00Added an answer on May 18, 2026 at 8:16 pm

    Managed Object Contexts are not thread safe. Apple’s guidelines indicate that you must have a separate instance of NSManagedObjectContext per thread.

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

Sidebar

Related Questions

This is probably a very straight forward application, but I am new to Objective-C
This is probably very basic, but it's giving me a headache, and I'm not
I'm brand new to Django, so the answer to this is probably very simple.
This is probably something very simple but I'm not getting the results I'm expecting.
This is probably a very common question but here it goes. I created an
This is probably a very simple question for some, but it has me stumped.
This is probably a very simple question for some, but it has me stumped.
i tried to google this but can't find a satisfactory answer. it's probably very
I know this is probably very elementary, but I can't seem to figure it
I know this is probably very easy, but I want to add sprites 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.