When I try to update my DB with coreData with push notification in background by using that command:
if (![context save:&error]) {
There is an error message Cannot update object that was never inserted. I precise that when I do it with the application active, it works well, but when I use it in background, it doesn't work (Error Domain=NSCocoaErrorDomain Code=134030).
- (void)parseAndAddLovAll:(NSMutableArray*)responseArray toArray:(NSMutableArray*)destinationArray
{
NSError *error;
DB_ListOfValue_manage *elements_to_store = [[DB_ListOfValue_manage alloc] init];
NSManagedObjectContext * context = [elements_to_store managedObjectContext];
for (int index=0; index < [responseArray count]; index++)
{
NSDictionary * responseArray2 = [[NSDictionary alloc] initWithDictionary:responseArray[index]];
NSString * table_to_store = [[NSString alloc] initWithString:[responseArray2 objectForKey:@"table"]];
NSArray * lignes = [[NSArray alloc] initWithObjects:[responseArray2 objectForKey:@"lignes"], nil];
id value;
// Check if LOV table or contact table
if ((([@"Table_contact" compare:table_to_store])!=NSOrderedSame)&&
(([@"Table_event" compare:table_to_store])!=NSOrderedSame))
{
for (NSDictionary * item in lignes[0])
{
value = [item objectForKey:@"codeevent"];
if ([value isEqualToNumber:[NSNumber numberWithInt:EVENT_ID]])
{
elements_to_store = (DB_ListOfValue_manage*)[NSEntityDescription insertNewObjectForEntityForName:table_to_store inManagedObjectContext:context];
elements_to_store.code_event = [value isKindOfClass:[NSNull class]] ? @"" : value;
value = [item objectForKey:@"id"];
elements_to_store.id = [value isKindOfClass:[NSNull class]] ? @"" : value;
if (![context save:&error]) {
#ifdef DEBUG
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
#endif
}
else{
#ifdef DEBUG
NSLog(@"Data saved to DB, table %@ %@ %@", table_to_store, elements_to_store.label1, elements_to_store.label2);
#endif
}
}
}
}
}
}
Message error complete:
Error Domain=NSCocoaErrorDomain Code=132001 "(null)" UserInfo={message=attempt to recursively call -save: on the context aborted, stack trace=(
0 CoreData 0x00000001863b322c <redacted> + 156
1 Formbox_Renault_ePrix_Game 0x000000010022b5e4 -[ListOfValueSync parseAndAddLovAll:toArray:] + 4356
2 Formbox_Renault_ePrix_Game 0x0000000100229140 -[ListOfValueSync getAllListOfValueAll] + 748
3 Formbox_Renault_ePrix_Game 0x00000001001afbc8 __23-[ViewController sync:]_block_invoke.395 + 104
4 libdispatch.dylib 0x000000010153d2cc _dispatch_call_block_and_release + 24
5 libdispatch.dylib 0x000000010153d28c _dispatch_client_callout + 16
6 libdispatch.dylib 0x000000010154bf80 _dispatch_queue_serial_drain + 696
7 libdispatch.dylib 0x00000001015407ec _dispatch_queue_invoke + 332
8 libdispatch.dylib 0x000000010154cf6c _dispatch_root_queue_drain_deferred_wlh + 428
9 libdispatch.dylib 0x0000000101554020 _dispatch_workloop_worker_thread + 652
10 libsystem_pthread.dylib 0x000000018380ef1c _pthread_wqthread + 932
11 libsystem_pthread.dylib 0x000000018380eb6c start_wqthread + 4
)}
(lldb)
How can I do updates of my database with coredata during a background process?
You have to use dispatch_async
dispatch_async(dispatch_get_main_queue(), ^{