iOS:错误域= NSCocoaErrorDomain代码= 132001"-save:在上下文中止"使用后台更新数据库coredata/push通知 [英] iOS: Error Domain=NSCocoaErrorDomain Code=132001 '-save: on the context aborted' using update of the database coredata/push notification in background

查看:196
本文介绍了iOS:错误域= NSCocoaErrorDomain代码= 132001"-save:在上下文中止"使用后台更新数据库coredata/push通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过使用该命令在后台使用推送通知在coreData中更新数据库时:

When I try to update my DB with coreData with push notification in background by using that command:

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

有错误消息无法更新从未插入的对象. 我精确地指出,当我在应用程序处于活动状态时执行此操作时,它可以很好地工作,但是当我在后台使用它时,它将不起作用( Error Domain = NSCocoaErrorDomain Code = 134030 ).

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
                    }
                }    
            }
        }   
    }
}

消息错误完成:

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更新数据库?

How can I do updates of my database with coredata during a background process?

推荐答案

您必须使用dispatch_async

You have to use dispatch_async

dispatch_async(dispatch_get_main_queue(), ^{

这篇关于iOS:错误域= NSCocoaErrorDomain代码= 132001"-save:在上下文中止"使用后台更新数据库coredata/push通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆