PHFetchResult get all photos and sort by date inconsistent
Asked Answered
F

1

7

I'm trying to build a simply photo picker that has two options for now: Recents and Favorites. What I'm doing is trying to get all the photos by the creationDate however this is giving back images in the wrong order in my data source. There's photos from years ago at the beginning of the data source, and photos that are less than a few minutes old scattered throughout. I think the issue is that I need to tell the main fetchResult the sort order first, however I don't think it's possible: Unsupported sort descriptor in fetch options: (creationDate, ascending, compare:

I'd appreciate any help offered. Code:

@property (nonatomic, strong) NSMutableOrderedSet *recentsDataSource;
@property (nonatomic, strong) NSMutableOrderedSet *favoritesDataSource;

- (void)setup
{
    PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

    for (PHAssetCollection *sub in fetchResult)
    {
        PHFetchOptions *fetchOptions = [[PHFetchOptions alloc]init];

        fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

        PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions];

        for (PHAsset *asset in assetsInCollection)
        {
            [self.recentsDataSource addObject:asset];

            if (asset.isFavorite)
            {
                [self.favoritesDataSource addObject:asset];
            }
        }
    }
}
Feeling answered 2/6, 2015 at 17:32 Comment(0)
F
8

I figured this out on my own, here is my solution:

- (void)setup
{
    self.recentsDataSource = [[NSMutableOrderedSet alloc]init];
    self.favoritesDataSource = [[NSMutableOrderedSet alloc]init];

    PHFetchResult *assetCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

    PHFetchResult *favoriteCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumFavorites options:nil];

    for (PHAssetCollection *sub in assetCollection)
    {
        PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil];

        for (PHAsset *asset in assetsInCollection)
        {
            [self.recentsDataSource addObject:asset];
        }
    }

    if (self.recentsDataSource.count > 0)
    {
        NSArray *array = [self.recentsDataSource sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]];

        self.recentsDataSource = [[NSMutableOrderedSet alloc]initWithArray:array];
    }

    for (PHAssetCollection *sub in favoriteCollection)
    {
        PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil];

        for (PHAsset *asset in assetsInCollection)
        {
            [self.favoritesDataSource addObject:asset];
        }
    }

    if (self.favoritesDataSource.count > 0)
    {
        NSArray *array = [self.favoritesDataSource sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]];

        self.favoritesDataSource = [[NSMutableOrderedSet alloc]initWithArray:array];
    }
}
Feeling answered 2/6, 2015 at 18:23 Comment(1)
PHFetchResult does not conform to protocol Sequence, hence we cannot use it inside for .. in statement.Beniamino

© 2022 - 2024 — McMap. All rights reserved.