Exclude by mediaSubtype is broken

Hi all,


It would seem that this is a clear bug in the Photos framework: using PHFetchOptions with a predicate works great for *selecting* particular mediaSubtypes, but as soon as one tries to *exclude* a subtype, nothing is returned. Better explained with code:


// control: entire library
let library = PHAsset.fetchAssetsWithMediaType(.Image, options: PHFetchOptions())
print("Entire library: \(library.count)")


// expected result: PHAssetMediaSubtype.PhotoScreenshot *only*
// actual: works fine
let options1 = PHFetchOptions()
options1.predicate = NSPredicate(format: "(mediaSubtype & %d) != 0", PHAssetMediaSubtype.PhotoScreenshot.rawValue)
let result1 = PHAsset.fetchAssetsWithMediaType(.Image, options: options1)
print("Screenshots: \(result1.count)")


// expected result: all types *except for* PHAssetMediaSubtype.PhotoScreenshot
// actual: returns nothing
let options2 = PHFetchOptions()
options2.predicate = NSPredicate(format: "(mediaSubtype & %d) == 0", PHAssetMediaSubtype.PhotoScreenshot.rawValue)
let result2 = PHAsset.fetchAssetsWithMediaType(.Image, options: options2)
print("Not screenshots: \(result2.count)")


// expected result1 + result2 = library
assert(result1.count + result2.count == library.count)


Output on a fresh simulator with one imported screenshot:


Entire library: 12
Screenshots: 1
Not screenshots: 0
assertion failed: : file /Users/jc/Desktop/LibraryPredicate/LibraryPredicate/ViewController.swift, line 36


Assertion fails on device as well.


This would appear to be a common issue:


https://forums.developer.apple.com/message/110146#110146

http://stackoverflow.com/questions/31528441/how-to-to-filter-by-media-subtype-using-nspredicate-with-phfetchoptions

http://stackoverflow.com/questions/35229279/phfetchoptions-mediasubtype-predicate

http://stackoverflow.com/questions/31939082/nspredicate-to-exclude-slow-motion-videos-from-phfetchresults


I'd love to report this as a bug, but the bug reporter is not responding right now. *sigh*

Hi,


It is a bug, but I found a workaround. For example excluding screenshot subtype:


NSPredicate(format: "NOT ((mediaSubtype & %d) != 0)", PHAssetMediaSubtype.PhotoScreenshot.rawValue)

It'd be great to get the bug number posted here...

It appears to still be an issue in IOS 14.6 and Xcode 12.5.1

This issue is still not solved. When could we get this fixed, it is very confusing. Also curious what is causing this bug in Apple's code.

Exclude by mediaSubtype is broken
 
 
Q