How to get PHAssets in the order of date added using fetchAssests

Viewed 1021

I am trying to get the PHAssets from Camera roll, iCloud, and Photostream in the same order as they were added by the user. In ios 11, I use

PHFetchOptions *options = [[PHFetchOptions alloc] init];
PHFetchResult *fetchResults = [PHAsset fetchAssetsWithOptions:options];

But this has stopped working in iOS 12. Using this iCloud images and camera roll images come seperately. There are only 2 sort descriptors provided creationDate and modifiedDate.

The second solution that I tried was to fetch the photos from the 'user library' using smart album. 'User library' works differently based on whether the user has turned on the iCloud library. If the iCloud library is on, then User library contains all the images from camera roll and iCloud library. If the iCloud library is turned off then the user library only contains the images from camera roll. This returns the images from camera roll and iCloud library in correct order. The problem is that it cannot fetch the images from Photostream.

In another solution, the photos were fetched from user library if the iCloud library is on, and if iCloud is off then they are fetched using the old method giving us the correct order of photostream and camera roll images.

This solutions also did not work.

Please check the following scenario:

  • The iCloud library is off and Photostream is on. We will use fetchAssetsWithOption with default options. The order will be correct.
  • User turns on the iCloud. Photostream automatically turns off. We will fetch the photos from 'user library'. Giving us all the images from iCloud as well as camera roll sorted with default options. The order will be correct.
  • Now user turns off the iCloud library. All the images from iCloud should get deleted from the device. But, some images from iCloud are left in the camera roll. They are not deleted (may be due to a bug in iCloud). As a result, these undeleted images always appear on the top. This also happens if the user downloads the images from iCloud and does not delete the image when the iCloud is turned off.

So is there any way to do it.

1 Answers

The answer is being updated for iOS 13 where using 'smartAlbumRecentlyAdded' works better than 'smartAlbumUserLibrary', because the behavior changed: Feedback was sent to Apple about the change in behavior in iOS 13. Hopefully a better solution will be at hand.

Because, although smartAlbumRecentlyAdded will return the items by their date added, you will not get ALL the items, just RECENT items.

    var fetchResult:PHFetchResult<PHAssetCollection>

    if #available(iOS 13.0, *) {
        fetchResult = PHAssetCollection.fetchAssetCollections(with:.smartAlbum,subtype:.smartAlbumRecentlyAdded,options: nil)
    }
    else {
        fetchResult = PHAssetCollection.fetchAssetCollections(with:.smartAlbum,subtype:.smartAlbumUserLibrary,options: nil)
    }

    if let assetCollection = fetchResult.firstObject {
        self.allPhotos = PHAsset.fetchAssets(in: assetCollection, options: nil)
    }
Related