NSInternalInconsistencyException: Asset (67) has too many attributes to treat as a single bitfield

Whenever I attempt to save an image to the Photo Library I get the following crash:


*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Asset (67) has too many at
tributes to treat as a single bitfield'


However, it doesn't happen immediately. It happens in a background thread after the image has already been saved.


[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetChangeRequest creationRequestForAssetFromImage:image];
    } completionHandler:^(BOOL success, NSError *error) {
        if (error) {
            NSLog(@"Error saving photo to camera roll: %@", error);
        } else {
            NSLog(@"Saved photo to camera roll");
        }
    }];


So I get a log of "Saved photo to camera roll" and then the app crashes. The exception always occurs in a background queue called "com.apple.photos.persistentHistoryChangeDistributor.managedObjectContext". And this happens 100% of the time I attempt to save an image but I can't reproduce this with another project.


Thoughts on why this might be occuring?

Replies

I'm getting a similar error but it's occurring entirely on a background thread. It seems to somehow be triggered after a call to

PHFetchResult* photosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:options];


The strange thing is that it's also reporting an error on Asset(67) for me too. Or maybe 67 is just an internal error code? Either way, I have no idea how to debug this or fix it. None of my code is in the call stack for the thread that this crash occurs on.

I'm still having this issue. Any update from your end?

Unfortunately, no, although I've narrowed it down somewhat. I've removed all the extra code and have simply this:


- (UICollectionViewCell *) collectionView:(UICollectionView *)inCollectionView cellForItemAtIndexPath:(NSIndexPath *)inIndexPath
{
  PHImageManager* manager = [PHImageManager defaultManager];
  SLPhotoCell* photo_cell = [inCollectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:inIndexPath];
  PHAsset* asset = [self.messages objectAtIndex:inIndexPath.row];

  [manager requestImageForAsset:asset targetSize:photo_cell.bounds.size contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info)
  {
  [photo_cell.thumbnailView setImage:result];
  }];

  return photo_cell;
}

This is the complete callstack of the crashed thread:


*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Asset (67) has too many attributes to treat as a single bitfield'
*** First throw call stack:
(
  0   CoreFoundation                      0x000000010bc7a29b __exceptionPreprocess + 331
  1   libobjc.A.dylib                     0x000000010b216735 objc_exception_throw + 48
  2   CoreFoundation                      0x000000010bc7a022 +[NSException raise:format:arguments:] + 98
  3   Foundation                          0x000000010a45db47 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194
  4   PhotoLibraryServices                0x0000000120ad8286 __56+[PLManagedObjectContext __prepareEntityPropertyLookups]_block_invoke_2 + 989
  5   CoreFoundation                      0x000000010bd14544 __NSDictionaryEnumerate + 532
  6   PhotoLibraryServices                0x0000000120ad7e43 __56+[PLManagedObjectContext __prepareEntityPropertyLookups]_block_invoke + 302
  7   AssetsLibraryServices               0x000000012136f799 __pl_dispatch_once_block_invoke + 25
  8   libdispatch.dylib                   0x000000010f06c63e _dispatch_client_callout + 8
  9   libdispatch.dylib                   0x000000010f06e100 _dispatch_once_callout + 66
  10  AssetsLibraryServices               0x000000012136f77e pl_dispatch_once + 75
  11  PhotoLibraryServices                0x0000000120ad7d0f +[PLManagedObjectContext __prepareEntityPropertyLookups] + 76
  12  PhotoLibraryServices                0x0000000120ad8733 +[PLManagedObjectContext _indexesByAttributeNamesByEntityNames] + 17
  13  PhotoLibraryServices                0x0000000120ad89ed +[PLManagedObjectContext indexValueForAttributeNames:entity:] + 37
  14  PhotoLibraryServices                0x0000000120ad8fb1 +[PLManagedObjectContext recordChangedKeys:forObjectID:inAttributesByOID:relationshipsByOID:] + 169
  15  PhotoLibraryServices                0x0000000120a8119a -[PLLocalChangeEventBuilder _recordUpdateChange:] + 479
  16  PhotoLibraryServices                0x0000000120a80ec4 -[PLLocalChangeEventBuilder recordChange:] + 332
  17  PhotoLibraryServices                0x0000000120a80b1c -[PLLocalChangeEventBuilder recordTransaction:] + 514
  18  CoreData                            0x0000000115ec72d1 developerSubmittedBlockToNSManagedObjectContextPerform + 161
  19  libdispatch.dylib                   0x000000010f06c63e _dispatch_client_callout + 8
  20  libdispatch.dylib                   0x000000010f07ab31 _dispatch_sync_invoke_and_complete_recurse + 136
  21  libdispatch.dylib                   0x000000010f07a476 _dispatch_sync_f_slow + 305
  22  CoreData                            0x0000000115ec71de -[NSManagedObjectContext performBlockAndWait:] + 286
  23  PhotoLibraryServices                0x0000000120a8083f __63-[PLLocalChangeEventBuilder recordAllTransactionsFromIterator:]_block_invoke.38 + 152
  24  PhotoLibraryServices                0x0000000120b85b42 -[PLPersistentHistoryTransactionIterator _enumerateOneTransactionWithBlock:] + 230
  25  PhotoLibraryServices                0x0000000120b85a01 -[PLPersistentHistoryTransactionIterator enumerateRemainingTransactionsWithBlock:] + 121
  26  PhotoLibraryServices                0x0000000120a806f6 -[PLLocalChangeEventBuilder recordAllTransactionsFromIterator:] + 316
  27  PhotoLibraryServices                0x0000000120c76088 __67-[PLPersistentHistoryChangeDistributor localEventFromTransactions:]_block_invoke + 82
  28  PhotoLibraryServices                0x0000000120a8000e __56+[PLLocalChangeEventBuilder localEventWithBuilderBlock:]_block_invoke + 63
  29  AssetsLibraryServices               0x00000001213701b6 pl_result_with_autoreleasepool + 27
  30  PhotoLibraryServices                0x0000000120a7ffa0 +[PLLocalChangeEventBuilder localEventWithBuilderBlock:] + 92
  31  PhotoLibraryServices                0x0000000120c76005 -[PLPersistentHistoryChangeDistributor localEventFromTransactions:] + 115
  32  PhotoLibraryServices                0x0000000120c759d1 -[PLPersistentHistoryChangeDistributor distributeTransactions:] + 89
  33  PhotoLibraryServices                0x0000000120c7522f -[PLPersistentHistoryChangeDistributor distributeNewTransactionsSinceLastToken] + 191
  34  PhotoLibraryServices                0x0000000120c7507c __78-[PLPersistentHistoryChangeDistributor _inq_startObservingRemoteNotifications]_block_invoke.26 + 34
  35  AssetsLibraryServices               0x000000012136fdb6 __pl_notify_register_dispatch_block_invoke + 34
  36  libsystem_notify.dylib              0x000000010f33da10 notify_register_mach_port + 7700
  37  libdispatch.dylib                   0x000000010f07a082 _dispatch_block_async_invoke2 + 83
  38  libdispatch.dylib                   0x000000010f06c63e _dispatch_client_callout + 8
  39  libdispatch.dylib                   0x000000010f073b47 _dispatch_lane_serial_drain + 791
  40  libdispatch.dylib                   0x000000010f0747f4 _dispatch_lane_invoke + 480
  41  libdispatch.dylib                   0x000000010f07e8b8 _dispatch_workloop_worker_thread + 733
  42  libsystem_pthread.dylib             0x000000010f45b61c _pthread_wqthread + 409
  43  libsystem_pthread.dylib             0x000000010f45b415 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

same for me. iOS11 works with no hassle, while iOS12 crashes

I just solved the same problem with you in my project.

In my project, the reason for the error was because I created a category of NSObject and added a property called "userInfo" using the AssociatedObject. When I deleted this category, it no longer crashes. I suspect that this userInfo property conflicts with the internal implementation of some of Apple's classes.

Hope this helps you.

this may actually be the cause indeed, at least by looking at the stacktrace. my problem is that I have several categories which extend classes deriving from NSObject, and any of them could be causing the issue. It's not necessarily caused by a NSObject extension. I have seen that many extensions refer to AssociatedObject in my project, expecially UIImage+AFNetworking