iOS: Ошибка домена = NSCocoaErrorDomain Code = 132001 '-save: в контексте прервано' с использованием обновления базы данных coredata / push-уведомлений в фоновом режиме - PullRequest
0 голосов
/ 04 июня 2018

Когда я пытаюсь обновить свою БД с помощью coreData с push-уведомлением в фоновом режиме с помощью этой команды:

if (![context save:&error]) {  

Появляется сообщение об ошибке Невозможно обновить объект, который никогда не был вставлен .Я уточняю, что когда я делаю это с активным приложением, оно работает хорошо, но когда я использую его в фоновом режиме, оно не работает ( 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
                    }
                }    
            }
        }   
    }
}

Сообщение об ошибке завершено:

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) 

Как я могу обновлять базу данных coredata во время фонового процесса?

1 Ответ

0 голосов
/ 05 июня 2018

Вы должны использовать dispatch_async

dispatch_async(dispatch_get_main_queue(), ^{
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...