How to use ManagedObjectContext with threads

2019-06-01 05:06发布

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条回答
Viruses.
2楼-- · 2019-06-01 05:16

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

查看更多
登录 后发表回答