NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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 -

(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 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.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.