如何将ManagedObjectContext与线程一起使用

时间:2010-12-24 16:28:53

标签: objective-c multithreading core-data nsmanagedobjectcontext

这可能是一个非常直接的应用程序,但我是Objective-C的新手(来自Java),整个内存管理和“EXC_BAD_ACCESS”错误让我心碎。

我有一个普通的NavigationController iPhone App,带有Core Data。在AppDelegate中创建NSManagedObjectContext并将其传递给RootViewController。直接从主线程查找视图以填充表格,这似乎工作正常。

该应用程序是某种RSS类型的阅读器,因此只要应用程序启动,我就会触发一个线程来获取新数据并更新视图:

-(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]; 
}

问题是这不起作用。在DataManager中,需要检索第一个设置,并且一旦创建了NSEntityDescription,我就会得到“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;
} 

我认为指向ManagedObjectContext的指针是错误的,因为在不同的线程和内存池中运行。那么如果这是一个问题,你如何创建这样的应用程序),我如何获得他对线程的原始ManagedObjectContext格式的引用?

[编辑] 我也尝试过使用

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

(如其他帖子所示),但结果相同

1 个答案:

答案 0 :(得分:8)

托管对象上下文不是线程安全的。 Apple's guidelines表示您必须每个线程都有一个单独的NSManagedObjectContext实例。