WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Error when saving image to Documents Directory

Forums > 100 Days of SwiftUI

I am still working on the app, but I have run into a problem I don't understand. I decided to save the images and information as a combination of a json encoded file and then related UUID identified image files in the document directory. Periodically when saving the image I get the following error:

1) Here's debug I have in my app:

2022-12-30 11:51:38.673804-0500 Challenge77[9990:3528132] 
[claims] 27328842-F382-4DBF-BE78-27B42569217E 
grantAccessClaim reply is an error: Error Domain=NSCocoaErrorDomain Code=4101 
"Couldn’t communicate with a helper application."
UserInfo={NSUnderlyingError=0x600002f29fb0 {Error Domain=PHAssetExportRequestErrorDomain Code=0 
"The operation couldn’tbe completed. (PHAssetExportRequestErrorDomain error 0.)" 
UserInfo={NSLocalizedDescription=The operation couldn’t becompleted. (PHAssetExportRequestErrorDomain error 0.),
NSUnderlyingError=0x600002f2a760 {Error Domain=PAMediaConversionServiceErrorDomain Code=2 
"The operation couldn’t be completed.(PAMediaConversionServiceErrorDomain error 2.)" 
UserInfo={NSLocalizedDescription=The operation couldn’t be completed.
(PAMediaConversionServiceErrorDomain error 2.)}}}}}

Any suggestions on what I am doing wrong?

My code can be found on github at - https://github.com/TheApApp/Challenge77

(formatted error to make it easier to read)


Note, if I hit the home button on the simulator and go back in, I can save the next image no problem. Also, when this error happens, the Add button becomes disabled until I hit the home button. I assume the app is in some kinda of hung state.


It appears that this is a thread issue in my ImagePicker. The warning shows up as

Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

in the following function:

        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            picker.dismiss(animated: true)  // let's get rid of the picker

            guard let provider = results.first?.itemProvider else { return } // let's see if there is anything in the provider if not bail out

            if provider.canLoadObject(ofClass: UIImage.self) {
                provider.loadObject(ofClass: UIImage.self ) { image, _ in
                    self.parent.image = image as? UIImage // WARNING SHOWS HERE

I am not sure how to address this. I tried to wrap this in a Task on MainActor.run but got a message that this is invalid in Swift 6.


On the call for the func wrap it in

Task { @MainActor in
  // call to the picker


DispatchQueue.main.async {
  provider.loadObject(ofClass: UIImage.self ) { image, _ in
      self.parent.image = image as? UIImage


I ran into a similar error, it seems to be a bug in Simulator. Choose the second picture then it successfully saves.



Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.