Home > Blockchain >  How to handle errors while saving multiple images to the user's device?
How to handle errors while saving multiple images to the user's device?

Time:10-18

My app as the functionality of choosing multiple images from the app main screen, and save the selected images to the user gallery.

As an example (image from google): enter image description here

After the user clicking "save" I am doing the following in order to save the chosen images to the user's gallery.

  1. Running through all of the images and saving each image that on clicked.
    func saveSelectedImagesToDevice() {
        for imageList in imagesListCells {
            for image in imageList.images {
                if image.selectionState == .onClicked {
                    downloadImage(from: image.url)
                }
            }
        }
    }
  1. Downloading each image
   
    func downloadImage(from url: String) {
        guard let url = URL(string: url) else {return}
        getData(from: url) { data, response, error in
            guard let data = data, error == nil else { return }
            guard let image = UIImage(data: data) else {return}
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
        }
    }

   private func getData(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
        URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
    }
    

    @objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if let _ = error {
            self.delegate?.savedImage(proccessMsg: "Error adding images to the gallery, pleast make sure you enabled photos permissions in phone setting")
        }
    }

The thing is, because the saving process is asynchronies, in case error occurs in one of the process of downloading an image, I would like to stop all of the other asynchronies processes that running on the background.

At the moment, in case of error, the error been called for each one of the images.

Any ideas how can I manage it different in order to keep the process asynchronies but to be able to stop all processes in case of error?

CodePudding user response:

You would have to change completely the architecture of the download to make it cancellable. A data task is cancellable, but yours is not because you have not retained any way of referencing it.

CodePudding user response:

Apple suggests to not using the shared instance if you want to create multiple sessions. You could try to achieve this by creating a single session instance and invalidate it as soon as you receive an error. Keep in mind that if you want to re-start the session you need to re instantiate a new one.

e.g.

let session = URLSession(configuration: .default)

func downloadImage(from url: String) {
    guard let url = URL(string: url) else {return}
    
    session.dataTask(with: url) { [weak self] data, response, error in
        guard let self = self else { return }
        if let error = error {
            print("You have an error: ",error.localizedDescription)
            self.session.invalidateAndCancel()
        }else if let data = data,
                 let image = UIImage(data: data) {

            UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
        }
    }.resume()
}
  • Related