BlueskyMed
BlueskyMed

Reputation: 1305

Fetching non-empty Smart Albums

Using Swift4 / ios11, I am trying to fetch a collection of non-empty smart albums from a device:

    let smartAlbumOptions = PHFetchOptions()
    smartAlbumOptions.predicate = NSPredicate(format: "estimatedAssetCount > 0")
    smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: smartAlbumOptions)

This is the paradigm I've seen used in many examples, however, never with specifying .smartAlbum. The problem is that the estimatedAssetCount > 0 predicate always returns zero results:

<PHFetchResult: 0x1c40fce00> count=0 

whereas if the predicate is "estimatedAssetCount >= 0", the fetch returns all smart albums empty and otherwise. Am I missing something is relation to smart albums?

Upvotes: 2

Views: 589

Answers (1)

creeperspeak
creeperspeak

Reputation: 5523

According to Apple's docs this estimatedAssetCount may not be accurate, and in cases where it's not available NSNotFound is returned. I suspect this is what's happening to you. In that case, I'd suggest continuing with Apple's docs suggestion, and just fetch all smart albums, and after they're fetched filter them down based on the (now accurate) count property.

Upvotes: 1

Related Questions