PHFetchResult get all photos and sort by date inconsistent

klcjr89 picture klcjr89 · Jun 2, 2015 · Viewed 10.1k times · Source

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];
            }
        }
    }
}

Answer

klcjr89 picture klcjr89 · Jun 2, 2015

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];
    }
}