开发者

core data multithread using

Dear community. When i has a stable version of my application, while i don't start change code to multithread version. What was a difference between previous version: in (void)applicationDidFinishLaunching:(NSNotification *)aNotification i do loop to add to queue my code:

 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++;
 }

External class have:

- (id)initAndUpdateCarrier:(NSString *)forCarrier 
                identifier:(NSUInteger)iQuene;
{
    [super init];
    [self setIdentifierQuene:iQuene];
    [self setStartForCarrier:forCarrier];
    [self setPercentDone:0.0];

This point is a very important:

    [self setDatabase:[[MySQLIXC alloc] init]];

u can't alloc other classes in process of u multithreading, i don't know why, but this is take malloc_error in whole queues

    [self setAppDelegate:[[NSApplication sharedApplication] delegate]];
    [self setManagedObjectContext:[[NSManagedObjectContext alloc] init]];
    return self;
}

And in external class i have:

 -(void) main;
{

[self makeUpdatesForCarrier:startForCarrier andTypeOfOperation:@"rates" forDirection:@"incoming"];// mySqlConnector:database];

it's a just some functions which working on local moc. When i start application, interface didn't place it in background, and start visualization only after all queues will finish.

Before i try to alloc]init] MySQLIXC class inside my external class, but it gives me a lot of malloc_error_break exceptions, like somebody try to freeze memory for me. Tnx.

Here is moc declaration: in .h:

@property(retain开发者_JAVA百科) NSManagedObjectContext *managedObjectContext;

in .m: @synthesize managedObjectContext;

Set persistent store coordinator:

[[self managedObjectContext] setUndoManager:nil];
[[self managedObjectContext] setPersistentStoreCoordinator:[appDelegate persistentStoreCoordinator]];

Merge changes for with main 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];  

And in one place of my code i'm using a main moc (only for read information, i know that moc not thread safe):

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


First off, CoreData is NOT thread safe. I would strongly advise that if you do not understand core data in a good deal of detail you keep you application singlethreaded or at the very least ensure that you always access the store from a single thread (probably best to use the main thread).

That said, this will not cause malloc_error_breaks AFAIK. You would see core data merge error exceptions and similar problems.

Could you show the code where you set up the moc further - just allocating and initing a moc is not enough - you have to set it's NSPersistentStoreCoordinator

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜