2

So i'm developing an app to let user select a few photos in the system Photo Library then using the Action Extension via the share sheet to delete some of those photos.

I was using extensionContext.inputItems to get the URLs of the selected photos:

let extensionItems = (self.extensionContext!.inputItems as! [NSExtensionItem]).first!
let attachments = extensionItems.attachments! as! [NSItemProvider]

for provider in attachments {
    provider.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil) {
        (imageURL, error) in
        ...
        ...
        ...
    }
}

Then i was using PHAssetChangeRequest.deleteAssets to delete photos by it's URL

PHPhotoLibrary.shared().performChanges({
    let imageAssetToDelete = PHAsset.fetchAssets(withALAssetURLs: imageURLs, options: nil)
    PHAssetChangeRequest.deleteAssets(imageAssetToDelete)
}, completionHandler: {success, error in
    if success {
        print("success")
    } else {
        print(error!)
    }
})

The "success" was printed to the console but the photo deletion doesn't requested (that delete photo alert like this doesn't shown)

So i inspected the variable imageAssetToDelete but it prints:

<PHFetchResult: 0x28188b200> count=0

the PHFetchResult has no items.

Which part went wrong?

Alice Chan
  • 2,814
  • 2
  • 16
  • 16
  • What is the action extension for? If you are in your own app, just delete the photos. If you are appearing in an activity view in someone else's app, you _can't_ delete the photos. – matt Jul 19 '18 at 00:25

1 Answers1

0

I've been struggling for days trying to find out what happened, but in my case the thing appeared to be extremely simple. In fact, changeBlock is called asynchronously (what is clearly stated in documentation) while in my code the array with assets is emptied right after (actually before) request. Code:

PHPhotoLibrary.shared().performChanges({
    // called when array is already empty
    PHAssetChangeRequest.deleteAssets(self.assetsToDelete as NSArray)
})
// called first
assetsToDelete.removeAll()

So the solution in my case was to simply move .removeAll() in completionHandler:

PHPhotoLibrary.shared().performChanges({
    PHAssetChangeRequest.deleteAssets(self.assetsToDelete as NSArray)
}, completionHandler: { c, _ in
    if c {
         self.assetsToDelete.removeAll()
    }
})
glassomoss
  • 736
  • 6
  • 7