AndrewH AndrewH - 2 months ago 30
Swift Question

'Unsupported predicate in fetch options: title == "Test"' in osx

I am currently working on an app where I need to display the images stored locally on the phone per album. I have managed to display all album names and now when an album is selected I want to display the images.

func getLocalAlbums() {
var albums: [AlbumModel] = [AlbumModel]()
albumNamesDevice.removeAll()

let options = PHFetchOptions()
let topLvlOptions = PHFetchOptions()
let userAlbums = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: options)
let topLvlCollection = PHCollectionList.fetchTopLevelUserCollectionsWithOptions(topLvlOptions)

let allAlbums = [topLvlCollection, userAlbums]

for i in 0 ..< allAlbums.count {
let result = allAlbums[i]

result.enumerateObjectsUsingBlock { (asset, index, stop) in
if asset is PHAssetCollection {
let ass: PHAssetCollection = asset as! PHAssetCollection
let fetchOptions = PHFetchOptions()

fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)

let newAlbum = AlbumModel(name: ass.localizedTitle!, count: ass.estimatedAssetCount, collection: ass)
albums.append(newAlbum)
}
}
}

for item in albums {
self.albumNamesDevice.append(item.name)
}
}

func getAssetsFromAlbum(albumName: String) -> [PHAsset] {

let options = PHFetchOptions()
// Bug from Apple since 9.1, use workaround
options.predicate = NSPredicate(format: "title = %@", albumName)
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]

let results = PHAsset.fetchAssetsWithMediaType(.Image, options: options)
var assets = [PHAsset]()

results.enumerateObjectsUsingBlock { (obj, index, stop) in

if let asset = obj as? PHAsset {
assets.append(asset)
}
}

return assets
}


the problem is the following line:

options.predicate = NSPredicate(format: "title = %@", albumName)


I keep getting the error:


Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate in fetch options: title == "Test"'


I have read that this was a bug back in 2014, but how can it be that this is still not solved by Apple devs?

Anyone know a work around for this?

Answer

Eventually I found the answer, or at least a workaround

func getAssetsFromAlbum(albumName: String) -> [PHAsset] {

    let options = PHFetchOptions()
    // Bug from Apple since 9.1, use workaround
    //options.predicate = NSPredicate(format: "title = %@", albumName)
    options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]

    let collection: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: nil)

    for k in 0 ..< collection.count {
        let obj:AnyObject! = collection.objectAtIndex(k)
        if obj.title == albumName {
            if let assCollection = obj as? PHAssetCollection {
                let results = PHAsset.fetchAssetsInAssetCollection(assCollection, options: options)
                var assets = [PHAsset]()

                results.enumerateObjectsUsingBlock { (obj, index, stop) in

                    if let asset = obj as? PHAsset {
                        assets.append(asset)
                    }
                }

                return assets
            }
        }
    }
    return [PHAsset]()
}