iOS: Error Domain=NSCocoaErrorDomain Code=132001 '-save: on the context aborted' 在后台使用数据库更新 coredata/push 通知
iOS: Error Domain=NSCocoaErrorDomain Code=132001 '-save: on the context aborted' using update of the database coredata/push notification in background
当我尝试通过使用该命令在后台使用带有推送通知的 coreData 更新我的数据库时:
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 更新我的数据库?
你必须使用dispatch_async
dispatch_async(dispatch_get_main_queue(), ^{
当我尝试通过使用该命令在后台使用带有推送通知的 coreData 更新我的数据库时:
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 更新我的数据库?
你必须使用dispatch_async
dispatch_async(dispatch_get_main_queue(), ^{