Saving a photo using PHPhotoLibrary issues

Good afternoon, I am creating a camera application as part of my learning projects with Swift and I am having an issue.

When the application runs for the first time, the application finishes before the photo actually is saved in the Photo Library; checking the console i can see that the call of the method that saves the image finishes before the actual code block finishes saving the image.


Here is the code of how I call the method from the ViewController. I am using my own class called PhotoAlbum.


Save button Action:

@IBAction func btnSaveClick(_ sender: AnyObject) {
        print("executing btnSaveClick: save click")
        print("selected album: \(self.SelectedAlbum)")
  
        let newPhotoAlbum = PhotoAlbum(strAlbumName: self.SelectedAlbum)
  
        let validate = newPhotoAlbum.saveAutograph(self.imageView.image!)
  
        print("btnSaveClick: validate value: \(validate)")
  
        self.getSaveResponse(validate)
}


Now here is the code for the saveAutograph method in the PhotoAlbum class


func saveAutograph(_ image: UIImage) ->Bool
{
        let result:Bool = self.createAlbum(image)
        return result
    
}


The next code is the code of the createAlbum method which is the method that calls the saveImage method:


func createAlbum(_ image: UIImage) ->Bool {
     
        var result: Bool = false
     
        print("in CreateAlbum")
     
        if let assetCollection = fetchAssetCollectionForAlbum() {
            self.albumFound = true
            self.assetCollection = assetCollection
            result = self.saveImage(image)
        }
        else {
            PHPhotoLibrary.requestAuthorization{ status in
                switch status
                {
                    case .authorized :
                        print("CreateAlbum, we are authorized")
                        do
                        {
                            try PHPhotoLibrary.shared().performChangesAndWait({
                                let createAlbumRequest : PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: self.albumName)
                                self.assetCollectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection
                                result = true
                            })
                         
                            self.albumFound = result
                            print("execution of the performanChanges in createAlbum")
                            if (result) {
                                print("album was created")
                                let collectionFetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [self.assetCollectionPlaceholder.localIdentifier], options: nil)
                             
                                self.assetCollection = collectionFetchResult.firstObject!
                                print("createAlbum: result value: \(result)")
                                result = self.saveImage(image)  //here is the issue 
                            }
                            else {
                                print("there is a problem")
                            }
                        }
                        catch let error
                        {
                            result = false
                            print("there was a problem: \(error.localizedDescription)")
                        }
                     
                     
                        break
                    default:
                        break
                }
            }
         
        }
        return result
     
    }


Finally here is the code of the saveImage method: the issue is that the method exists before the performChangesAndWait finishs:


func saveImage(_ image: UIImage) ->Bool {
        print("Executing saveImage")
        var result:Bool = false
        if assetCollection == nil {
            print("the album is nil")
            return result
        }
        print("saveImage: we will start saving the image")
        /
        do
        {
            try PHPhotoLibrary.shared().performChangesAndWait({
                let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
                let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
                let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
                let enumeration: NSArray = [assetPlaceHolder!]
                albumChangeRequest!.addAssets(enumeration)
                print("saveImage: image was save without issues")
                result = true
                print("saveImage: result value after saving the image: \(result)")
            })
        }
        catch let error
        {
            result = false
            print("saveImage: there was a problem: \(error.localizedDescription)")
        }
      
      
        print("saveImage: result value before exiting the method: \(result)")
        return result
      
    }



The issue the execution of the method reaches the return result before the PHPhotoLibrary.shared().performChangesAndWait finishes, so result is false and even when the method finishes saving the photo without issues because result == false my code will show a message saying that something went wrong but it actually didn't.


The question is how can I force this to wait until the PHPhotoLibrary.shared().performChangesAndWait it actually finishes??


thanks in advance for the help

Saving a photo using PHPhotoLibrary issues
 
 
Q