4

I'm working with the Photos framework, specifically I'd like to keep track of the current camera roll status, thus updating it every time assets are added, deleted or modified (mainly when a picture is edited by the user - e.g a filter is added, image is cropped).

My first implementation would look something like the following:

private var lastAssetFetchResult : PHFetchResult<PHAsset>?

func photoLibraryDidChange(_ changeInstance: PHChange) {
    guard let fetchResult = lastAssetFetchResult,
    let details = changeInstance.changeDetails(for: fetchResult) else {return}

    let modified  = details.changedObjects
    let removed   = details.removedObjects
    let added     = details.insertedObjects

    // update fetch result
    lastAssetFetchResult = details.fetchResultAfterChanges

    // do stuff with modified, removed, added
}

However, I soon found out that details.changedObjects would not contain only the assets that have been modified by the user, so I moved to the following implementation:

let modified  = modifiedAssets(changeInstance: changeInstance)

with:

func modifiedAssets(changeInstance: PHChange) -> [PHAsset] {
    var modified    : [PHAsset] = []

    lastAssetFetchResult?.enumerateObjects({ (obj, _, _) in
        if let detail = changeInstance.changeDetails(for: obj) {
            if detail.assetContentChanged {
                if let updatedObj = detail.objectAfterChanges {
                    modified.append(updatedObj)
                }
            }
        }
    })

    return modified
}

So, relying on the PHObjectChangeDetails.assetContentChanged property, which, as documentation states indicates whether the asset’s photo or video content has changed.

This brought the results closer to the ones I was expecting, but still, I'm not entirely understanding its behavior.

On some devices (e.g. iPad Mini 3) I get the expected result (assetContentChanged = true) in all the cases that I tested, whereas on others (e.g. iPhone 6s Plus, iPhone 7) it's hardly ever matching my expectation (assetContentChanged is false even for assets that I cropped or added filters to).

All the devices share the latest iOS 11.2 version.

Am I getting anything wrong? Do you think I could achieve my goal some other way?

Thank you in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Eil88
  • 121
  • 5

0 Answers0