I’m developing an application, which parses XML ant put data to CoreData. I want it to start quickly, so I load data from CoreData first, and after load and parse XML in other thread. The problem is that when the app starts first time CoreData is empty and I start to parse:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SlideItem" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil)
{
// Handle the error.
NSLog(@"mutableFetchResults == nil");
}
NSLog(@"mutableFetchResults count = %d", [mutableFetchResults count] );
if ([mutableFetchResults count] == 0 ) // if DB is empty
{
[self loadAndParse]; // here I do it in the main thread
//so my CoreData is filled with data here
}
//but if try to execute my request again like this:
mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
//it is empty again
How can I do it? thanks
You have to merge the changes from the other context in the thread where the xml is parsed. Until you do that, the context on the main thread doesn’t know what was changed.
Use:
or