Batch insert error when unique constraint is set on multiple attributes

I'm testing out Core Data batch insert. The performance is amazing!!

Data gets imported in a just a couple hundreds of milliseconds. New rows are inserted and existing rows are updated.

I ran into 1 problem though....

If the unique constraint is set with multiple attributes, batch insert will fail.

Below is the call stack of the failed operation:

Code Block
2020-10-06 00:05:44.900901-0700 TestHostingApp[17605:1541912] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'error in batch insertion: Error Domain=NSCocoaErrorDomain Code=134050 "(null)" UserInfo={NSExceptionOmitCallstacks=true, _NSCoreDataOptimisticLockingFailureConflictsKey=()}'
*** First throw call stack:
(
0   CoreFoundation                      0x00007fff2043a126 exceptionPreprocess + 242
1   libobjc.A.dylib                     0x00007fff20177f78 objc_exception_throw + 48
2   CoreFoundation                      0x00007fff20439f4f +[NSException raise:format:] + 0
3   Foundation                          0x00007fff207881ca -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4   SchedKit2_Tests                     0x0000000104fb85a7 -[NSManagedObjectContext(SchedBatchInsert) batchInsertWithEntityName:objects:] + 391
5   SchedKit2_Tests                     0x0000000104fb3194 92+[BatchImport importEntityWithName:confID:batchSize:context:queue:formatHandler:completion:]_block_invoke_2.73 + 180
6   CoreData                            0x00007fff25109933 developerSubmittedBlockToNSManagedObjectContextPerform + 154
7   libdispatch.dylib                   0x00007fff20106534 _dispatch_client_callout + 8
8   libdispatch.dylib                   0x00007fff2010c425 _dispatch_lane_serial_drain + 715
9   libdispatch.dylib                   0x00007fff2010cfa0 _dispatch_lane_invoke + 403
10  libdispatch.dylib                   0x00007fff20117591 _dispatch_workloop_worker_thread + 782
11  libsystem_pthread.dylib             0x00007fff5dcd8a3d _pthread_wqthread + 290
12  libsystem_pthread.dylib             0x00007fff5dcd7b77 start_wqthread + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException


My entity has this constrain configuration:
  • Constraints: eventID,sessionID

The context where batch insert operation has mergeByPropertyObjectTrumpMergePolicy policy set.

Is this a known limitation? (i.e. I can only set unique constraint on 1 attribute in order to have batch insert work correctly?)

Replies

I've filed a radar FB8780731
Did you prepare an example by any chance?
I've played with NSBatchInsertRequest init(entity:managedObjectHandler:) and it seems working fine.

Hi, I see the similar problem in my simple test project at Github ( at the commit: 23c759d ):

I am trying to save users of a multilanguage app into Core Data by using NSBatchInsertRequest and I am using uid,language as constraints:

and the error (when calling 2nd time) is:

`TopsBatchInsert/TopViewModel.swift:103: Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134050 "(null)" UserInfo={NSExceptionOmitCallstacks=true, _NSCoreDataOptimisticLockingFailureConflictsKey=( )}, ["_NSCoreDataOptimisticLockingFailureConflictsKey": <__NSArray0 0x102b7a9b0>(

) , "NSExceptionOmitCallstacks": 1] 2021-07-13 16:30:02.944932+0200 TopsBatchInsert[86706:2120693] TopsBatchInsert/TopViewModel.swift:103: Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134050 "(null)" UserInfo={NSExceptionOmitCallstacks=true, _NSCoreDataOptimisticLockingFailureConflictsKey=( )}, ["_NSCoreDataOptimisticLockingFailureConflictsKey": <__NSArray0 0x102b7a9b0>(

) , "NSExceptionOmitCallstacks": 1]