Fetch Photos from Photo Library | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7910383-ios-photos-framework/lessons/1

This line of code is actually quite dangerous:
let albums = PHCollectionList.fetchTopLevelUserCollections(with: nil) as? PHFetchResult<PHAssetCollection>

You would think you would have only PHAssetCollection in the result, but it’s wrong: you will get a bunch of PHCollection, that is, either PHAssetCollection or PHCollectionList.
Weirdly enough, the compiler does NOT catch that (because the PHFetchResult kind of erases the generic type within).
And then you will call estimatedAssetsCount on what you think is a PHAssetCollection, and if a user has album folders (a.k.a PHCollectionList) in their library, your code will just crash. Mine did in production …

For my part, the code don’t work, I run the application, the interface loads, I am asked to get access to the library, I agree, and then the photos are not displayed.
Here is the error returned :
[core] “Error returned from daemon: Error Domain=com.apple.accounts Code=7 “(null)””

Capture d’écran 2020-07-02 à 11.03.15

Is that the version of the app from the sample project or is it the version of the app you built? If it’s the app you built, I recommend running the app from the final sample project corresponding to this episode then if that works, figure out where your code may differ.

I downloaded the project, I unzipped it, then opened the project in ‘End’, it build without errors or warnings, and then the app on the simulator or on a real device, doesn’t show any photos.

The error :
[core] “Error returned from daemon: Error Domain=com.apple.accounts Code=7 “(null)””
is displayed when this line of code (74) is called : Capture d’écran 2020-07-02 à 17.33.13

What are your Xcode and iOS versions?

And what’s the photo album configuration on your simulator? For example, are the photo items in various photo albums? Are they stored on the cloud? etc

Xcode 11.5 (11E608c)
iOS 13.5 (simulator and device), I tried also 12.1 in simulator (same pb)
Devices in simulator are brand new simulators, no extra configuration. All photos are in one album.
Real device has various albums and a part is on iCloud.

On simulator, ‘My Albums’ seem to be empty, but there are photos in ‘My Camera Roll’ album if iIclick on ‘See All’.
Capture d’écran 2020-07-03 à 08.59.51

Thanks for your help Lyndsey

@lyndsey Can you please help with this when you get a chance? Thank you - much appreciated! :]

@freddyf Thank you for sharing this - much appreciated!