【发布时间】:2010-12-09 13:49:21
【问题描述】:
亲爱的社区。 当我有一个稳定版本的应用程序时,我不会开始将代码更改为多线程版本。与以前的版本有什么区别: 在 (void)applicationDidFinishLaunching:(NSNotification *)aNotification 我做循环添加到队列我的代码:
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]];
你不能在你的多线程进程中分配其他类,我不知道为什么,但这是在整个队列中使用 malloc_error
[self setAppDelegate:[[NSApplication sharedApplication] delegate]];
[self setManagedObjectContext:[[NSManagedObjectContext alloc] init]];
return self;
}
在外部课程中我有:
-(void) main;
{
[self makeUpdatesForCarrier:startForCarrier andTypeOfOperation:@"rates" forDirection:@"incoming"];// mySqlConnector:database];
这只是在本地 moc 上工作的一些功能。 当我启动应用程序时,界面没有将其置于后台,只有在所有队列完成后才开始可视化。
在我尝试在我的外部类中分配]init] MySQLIXC 类之前,但它给了我很多 malloc_error_break 异常,就像有人试图为我冻结内存一样。 肿瘤坏死因子。
这是 moc 声明: 在 .h 中:
@property(retain) NSManagedObjectContext *managedObjectContext;
在 .m 中: @synthesize managedObjectContext;
设置持久存储协调器:
[[self managedObjectContext] setUndoManager:nil];
[[self managedObjectContext] setPersistentStoreCoordinator:[appDelegate persistentStoreCoordinator]];
与主 moc 合并更改:
- (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 不是线程安全的):
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];
【问题讨论】:
标签: objective-c multithreading core-data