2

Currently, I have that piece of code:

import Photos

public class AssetService : NSObject, PHPhotoLibraryChangeObserver {

public override init() {
    super.init()
    PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self)
}

deinit {
    PHPhotoLibrary.sharedPhotoLibrary().unregisterChangeObserver(self)
}

public func photoLibraryDidChange(changeInstance: PHChange) {

    dispatch_async(dispatch_get_main_queue(), {

    })

}  


}

What could I do in the photoLibraryDidChangefunction if I want to check updates in my camera roll. I fetch all images/video at the beginning into a PHFetchResult and now I want to monitor the changes.
I display the images/video in a UIView always in fullscreen and use a UIPageControl to scroll through the content. Would be thankful for any help

mafioso
  • 1,480
  • 3
  • 18
  • 42

1 Answers1

3

From https://developer.apple.com/library/prerelease/content/samplecode/UsingPhotosFramework/Listings/Shared_MasterViewController_swift.html#//apple_ref/doc/uid/TP40014575-Shared_MasterViewController_swift-DontLinkElementID_9

var allPhotos: PHFetchResult<PHAsset>!
var smartAlbums: PHFetchResult<PHAssetCollection>!
var userCollections: PHFetchResult<PHCollection>!

func photoLibraryDidChange(_ changeInstance: PHChange) {
    // Change notifications may be made on a background queue. Re-dispatch to the
    // main queue before acting on the change as we'll be updating the UI.
    DispatchQueue.main.sync {
        // Check each of the three top-level fetches for changes.

        if let changeDetails = changeInstance.changeDetails(for: allPhotos) {
            // Update the cached fetch result. 
            allPhotos = changeDetails.fetchResultAfterChanges 
            // (The table row for this one doesn't need updating, it always says "All Photos".)
        }

        // Update the cached fetch results, and reload the table sections to match.
        if let changeDetails = changeInstance.changeDetails(for: smartAlbums) {
            smartAlbums = changeDetails.fetchResultAfterChanges
            tableView.reloadSections(IndexSet(integer: Section.smartAlbums.rawValue), with: .automatic)
        }
        if let changeDetails = changeInstance.changeDetails(for: userCollections) {
            userCollections = changeDetails.fetchResultAfterChanges
            tableView.reloadSections(IndexSet(integer: Section.userCollections.rawValue), with: .automatic)
        }

    }
}
Federico Zanetello
  • 2,855
  • 1
  • 22
  • 20