开发者

Update and delete entities in two different process

I'm working on an ipad application that use coredata. It download information on a database that is on the web, and record them in coredata. The application is based on a split view. My problem was to make the update of the data in background.

Here is how I've done :

- I've create an NSOperation, that does the download and the update of the data.

- This NSOperation use a different NSManagedObjectContext than the context of the appDelegate, return by this function, that is in the appDelegate :

(NSManagedObjectContext*)newContextToMainStore {
     NSPersistentStoreCoordinator *coord = nil;
     coord = [self persistentStoreCoordinator];
     NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; 
     [moc setPersistentStoreCoordinator:coord]; 
     return [moc autorelease];
}

- I've had an observer in the NSOperation, that will call this function in the appDelegate when I save the context, to modify the context of the delegate too :

- (void)mergeChangesFromContextSaveNotification:(NSNotification*)notification {
     [[self managedObjectContext]mergeChangesFromContextDidSaveNotification:notification];
}

But I've a problem : when I delete an element in the rootViewController, I really don't know how to manage the changes in the background process, because there is a loop inside that browse all the entities : if I delete one entitie when the background loop is at the same entitie, this is... really bad...

My solution was just to stop the update process when I delete an entitie, and then restart开发者_高级运维 it, simply...

But I've realised that the changes made in the "main" context were not apply in the new context I just create for the update.

So I ask you : Why the changes aren't apply in the new context ? If this the wrong way, how do you do this ? Using mergeChangesFromContext or something else ?

Thanks you in advance.


Sorry, my mistakes :
- First, my entities contained other entities, and because of a bad relationship, the entities that was contained in the parent entitie weren't deleted.
- Second, I was thinking that call -cancelAllOperations will stop the current operation, but it's not, you have to check in the nsoperation if the process is cancelled with [self isCancelled].
That's all !


U don't need to take care if somebody will delete something from interface, bcs NSArrayController is thread safe. But if u make updates and delete in same time, u have to take care about it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜