gpt4 book ai didi

objective-c - 如何在线程中使用ManagedObjectContext

转载 作者:行者123 更新时间:2023-12-03 13:14:50 26 4
gpt4 key购买 nike

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

我有一个带有Core Data的普通NavigationController iPhone应用程序。在AppDelegate中,将创建NSManagedObjectContext并将其传递给RootViewController。在 View 中,可以直接从主线程中查找事物以填充表,这似乎很好用。

该应用程序类似于RSS类型的阅读器,因此,一旦该应用程序启动,我就会触发一个线程来获取新数据并更新 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];
}

问题是这行不通。在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中(如其他帖子所暗示的那样),但这给出了相同的结果

最佳答案

受管对象上下文不是线程安全的。 Apple's guidelines指示每个线程必须具有单独的NSManagedObjectContext实例。

关于objective-c - 如何在线程中使用ManagedObjectContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4527314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com