Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSInternalInconsistencyException accessing _cachedSystemAnimationFence requires the main thread

Tags:

ios

Using Crittercism with some beta testers I am seeing an error appear several times that I've never experienced myself and I am unable to replicate.

Crittercism tells me:NSInternalInconsistencyException accessing _cachedSystemAnimationFence requires the main thread

And the line it is pointing to is:

[picker dismissViewControllerAnimated:YES completion:^{

Doing some reading on StackOverflow it appears that any UI code should be ran on the main thread. Is the error I am experiencing because the dismissViewControllerAnimated has been ran on a background thread?

Curious why this error is relatively random (i.e. I cannot reproduce it) and also how do I fix it.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    __block PHObjectPlaceholder *assetPlaceholder;

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

        PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"]];

        assetPlaceholder = changeRequest.placeholderForCreatedAsset;

    } completionHandler:^(BOOL success, NSError *error) {

        NSArray *photos = [[NSArray alloc] initWithObjects:assetPlaceholder.localIdentifier, nil];
        PHFetchResult *savedPhotos = [PHAsset fetchAssetsWithLocalIdentifiers:photos options:nil];

        [savedPhotos enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {

            NSMutableArray *images = self.event.eventAttachments;
            if (images) {
                [images addObject:asset];
            } else {
                images = [[NSMutableArray alloc]init];
                [images addObject:asset];
            }

            self.event.eventAttachments = images;

            [picker dismissViewControllerAnimated:YES completion:^{

                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:4];
                NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];

                [self.tblChildrenNotes beginUpdates];
                [self.tblChildrenNotes reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
                [self.tblChildrenNotes endUpdates];

            }];

        }];
    }];

}
like image 320
nwkeeley Avatar asked Sep 22 '15 20:09

nwkeeley


3 Answers

For Swift 3

    DispatchQueue.main.async(execute: {
       //UI Related Function
    });

For me this solution fixed when trying to open pdfViewer when in landscape mode.

like image 163
Rishabh Dugar Avatar answered Nov 11 '22 08:11

Rishabh Dugar


If we need to call any UI Related operation inside the block use dispatch. Everything that interacts with the UI must be run on the main thread. You can run other tasks that don't relate to the UI outside the main thread to increase performance.

dispatch_async(dispatch_get_main_queue(), ^{
    // Call UI related operations
});
like image 28
Adarsh G J Avatar answered Nov 11 '22 08:11

Adarsh G J


I'm afraid I don't have a solution but I can offer the details of an app that is crashing the same way. We're using the Cordova platform to build our app. This crash only seems to happen in iOS9 (on an iPhone 6 in our case) and only when the user has Swype keyboard installed. We see the crash when opening/closing the keyboard to type in a text field displayed through Cordova's InAppBrowser plugin, and only sometimes. Deleting the Swype app makes the bug go away. Unfortunately, since we didn't write any of the objc involved in the app, we're having a tough time debugging. Good luck!

like image 4
grantpatterson Avatar answered Nov 11 '22 09:11

grantpatterson