What does the error mean: Unable to obtain assetsd XPC proxy

My iOS app allows users to edit and save videos. The edited videos are saved with a call to

PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL:
within a
PHPhotoLibrary
's
performChanges
block.

These calls frequently fail with the error code

41002
(Domain:
com.apple.photos.error
). Its
localizedDescription
is:

Unable to obtain assetsd XPC proxy for getPhotoKitServiceWithReply:. assetsd could have crashed

What does the error mean? I tried to search for the error code, the domain and the keywords from the description, but couldn't find anything. Is there an official reference for the errors in this domain? What error message should I show to the user?

Accepted Reply

An Apple engineer has provided me the following explanation:


The app could not communicate with a helper application (i.e. the photo library). The most common reason that

assetsd
might crash is because the foreground app is using a lot of memory (or allocating memory very quickly), and the system decides to terminate
assetsd
to alleviate some of that memory pressure.


To prevent these errors, you should make sure that your app is not triggering a high memory pressure situation.


This should only be a temporary issue. In other words, if you tried to submit the same request later, it may work (assuming that any high memory pressure situation has been resolved). So, I recommend that you queue up requests that fail and try to submit them later, and inform your user of this in some way, so that they know the save has not gone through yet.


This same issue can also create an error with the code

4099
, domain
NSCocoaErrorDomain
and the description:

Couldn’t communicate with a helper application.

Replies

I am also noticing this same crash

An Apple engineer has provided me the following explanation:


The app could not communicate with a helper application (i.e. the photo library). The most common reason that

assetsd
might crash is because the foreground app is using a lot of memory (or allocating memory very quickly), and the system decides to terminate
assetsd
to alleviate some of that memory pressure.


To prevent these errors, you should make sure that your app is not triggering a high memory pressure situation.


This should only be a temporary issue. In other words, if you tried to submit the same request later, it may work (assuming that any high memory pressure situation has been resolved). So, I recommend that you queue up requests that fail and try to submit them later, and inform your user of this in some way, so that they know the save has not gone through yet.


This same issue can also create an error with the code

4099
, domain
NSCocoaErrorDomain
and the description:

Couldn’t communicate with a helper application.

I found this answer to be untrue for my circumstances even though it seems to match almost exactly the errors I was getting.

I am building a Catalyst version of an ancient Objective-C iOS app and on iOS and iPadOS UIImageWriteToSavedPhotosAlbum works as it always has. For Mac, though, I had to use the Photos framework. I had a feeling it was not my apps using too much memory more than photoslibraryd or assets had crashed because I could retrieve images from the Photos library just fine and because my Catalyst apps run more quietly and with less memory than most apps running on my Mac at any given time.

The following truncated code worked for me on Mac and iOS and iPadOS.

Code Block
#if !TARGET_OS_UIKITFORMAC
    UIImageWriteToSavedPhotosAlbum(saveImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
 #endif
#if TARGET_OS_UIKITFORMAC
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    [PHAssetChangeRequest creationRequestForAssetFromImage:saveImage];
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
             // do some thing you need to do
        }
        else {
            NSLog(@"write error : %@",error);
        }
    }];
 #endif


Leaving this here in case anyone else gets frustrated because this is a top hit for the error code. I hope this helps someone else save a few hours.