Re: Save in Core Data from another thread
Re: Save in Core Data from another thread
- Subject: Re: Save in Core Data from another thread
- From: malcom <email@hidden>
- Date: Tue, 30 Oct 2007 15:01:51 +0100
On Oct 30, 2007, at 4:33 AM, Chris Hanson wrote:
(1) Your -newIstanceManagedObjectContext method misspells "instance"
and should return a non-autoreleased object (since it starts with
"new").
(2) -storeList: is very generic, and mis-named to boot. An NSArray
is not a list; you would be better off calling it something like -
saveMessages:.
Ok I'll fix it.
There's one other issue that may be at the heart of the whole
matter, and may mean that what you're attempting to do is simply
unsupported:
Objects inserted in one managed object context are not made
available to other managed object contexts by Core Data until they
are saved, even if all of the managed object contexts involved share
a persistent store coordinator. In other words, only objects that
are actually in an underlying persistent store are available to all
managed object contexts via that persistent store.
The part of the code involved in save action inside my method is
pretty simple:
- (ANTArtNode *) _storeArticleDataInThisArchive:(NSString *) _rawData
onlyHeader:(BOOL) _onlyHeader ctx:(NSManagedObjectContext *) ctx {
// ... additional code not related with core data
ANTArtNode *art = [[ANTArtNode alloc] initWithEntity:
[NSEntityDescription
entityForName: @"ARTICLE" inManagedObjectContext: ctx]
insertIntoManagedObjectContext: nil];
[ctx insertObject: art];
// ... additional code not related with core data
return art;
}
while the part of the code involved in calling saveList is:
// ... blablabla
if ([listOfTempArticles count] == MIN_CHUNK_TO_SAVE) {
[[[group databaseArchive] persistentStoreArchive] saveList:
listOfTempArticles];
//[NSThread detachNewThreadSelector: @selector(saveList:) toTarget:
[[group databaseArchive] persistentStoreArchive]
withObject:listOfTempArticles];
}
// ... blablabla
It saves a list each MIN_CHUNK_TO_SAVE new chunck.
Now what I would to do is to perform save/insertObject operation on
these object in a background thread. It's not important that user can
see these new messages instantly but he should have the opportunity to
browse other persistent store (actually while core data try to insert
these object the interface become pretty unresponsive).
I've tried with detachNewThreadSelector + other new managed object as
you can see from commented code but it's the same thing (really, no
the same, it is worse than the before).
However I could perform a save each x seconds with a timer or perform
a save operation each x chunck, but probability there is an error in
my approach with core data and multithreading programming that I don't
know how to solve.
Thanks.
malcom
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden