Objective c 核心数据多线程使用
亲爱的社区。 当我有一个稳定的应用程序版本,而我没有开始将代码更改为多线程版本时。与以前的版本有什么区别: in(作废)ApplicationIDFinishLaunching:(NSNotification*)通知 我不循环将代码添加到队列:Objective c 核心数据多线程使用,objective-c,multithreading,core-data,Objective C,Multithreading,Core Data,亲爱的社区。 当我有一个稳定的应用程序版本,而我没有开始将代码更改为多线程版本时。与以前的版本有什么区别: in(作废)ApplicationIDFinishLaunching:(NSNotification*)通知 我不循环将代码添加到队列: NSOperationQueue *opQueueImportUpdateFirstTimeData = [[[NSOperationQueue alloc]init]autorelease]; int i = 0; for (NSString
NSOperationQueue *opQueueImportUpdateFirstTimeData = [[[NSOperationQueue alloc]init]autorelease];
int i = 0;
for (NSString *carrier in currentCarriers)
{
AppController *operation = [[AppController alloc] initAndUpdateCarrier:carrier identifier:i];
[opQueueImportUpdateFirstTimeData addOperation:operation];
i++;
}
外部类有:
- (id)initAndUpdateCarrier:(NSString *)forCarrier
identifier:(NSUInteger)iQuene;
{
[super init];
[self setIdentifierQuene:iQuene];
[self setStartForCarrier:forCarrier];
[self setPercentDone:0.0];
这一点非常重要:
[self setDatabase:[[MySQLIXC alloc] init]];
在u多线程处理过程中不能分配其他类,我不知道为什么,但这是整个队列中的take malloc_错误
[self setAppDelegate:[[NSApplication sharedApplication] delegate]];
[self setManagedObjectContext:[[NSManagedObjectContext alloc] init]];
return self;
}
在外部课程中,我有:
-(void) main;
{
[self makeUpdatesForCarrier:startForCarrier andTypeOfOperation:@"rates" forDirection:@"incoming"];// mySqlConnector:database];
这只是在本地moc上运行的一些功能。
当我启动应用程序时,界面并没有把它放在后台,只有在所有队列完成后才启动可视化
在我尝试在我的外部类中alloc]init]mysqlix类之前,它给了我很多malloc_error_break异常,就像有人试图为我冻结内存一样。
Tnx
以下是商务部声明:
在h中:
in.m:
@综合语境
设置持久存储协调器:
[[self managedObjectContext] setUndoManager:nil];
[[self managedObjectContext] setPersistentStoreCoordinator:[appDelegate persistentStoreCoordinator]];
将的更改与主主主运行中心合并:
- (void)mergeChanges:(NSNotification *)notification;
{
AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];
// Merge changes into the main context on the main thread
[mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
在我的代码的一个地方,我使用了一个主moc(仅用于读取信息,我知道moc不是线程安全的):
首先,CoreData不是线程安全的。我强烈建议,如果您不太了解核心数据的详细信息,请将应用程序保持为单线程,或者至少确保始终从单线程访问存储(可能最好使用主线程) 也就是说,这不会导致malloc_错误(malloc_error_breaks AFAIK)。您将看到核心数据合并错误异常和类似问题
您能否进一步展示设置moc的代码?仅分配和初始化moc是不够的,您必须设置它的NSPersistentStoreCoordinator亲爱的Roger,问题已更新。我发现的主要问题是,我不能在代码中创建外部类的实例,而只是在init中。在我进行更改之后,所有malloc错误都被清除了。但在这种情况下,你可能会提出更多建议,例如,我如何在线程之间保留一些数据,如数组。NSArray有点线程安全,所以它应该可以正常工作——正常的附带条件是,你需要锁定任何共享访问。NSArry没有爆炸并不意味着你的设计是线程安全的。除非您真的需要线程支持,否则我会避免它。如果你真的需要它,我建议一个新问题:-)
- (void)mergeChanges:(NSNotification *)notification;
{
AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];
// Merge changes into the main context on the main thread
[mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = [appDelegate managedObjectContext];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"DestinationsListForSale"
inManagedObjectContext:mainContext]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lastUsedProfit > 0"];
[request setPredicate:predicate];