PHFetchResult retrieves all photos and sorts by date inconsistent

I am trying to create a simple photo picker that has two options: Recursions and Favorites. What I am doing is trying to get all the photos using creationDate , but this returns the images in the wrong order in my data source. There are photographs from several years ago at the beginning of the data source, and photographs that are less than a few minutes are scattered everywhere. I think the problem is that I need to first specify the main selection of fetchResult, but I don't think it is possible: Unsupported sort descriptor in fetch options: (creationDate, ascending, compare:

I would appreciate any help. 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]; } } } } 
+6
source share
1 answer

I figured it out myself, 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]; } } 
+5
source

Source: https://habr.com/ru/post/988400/


All Articles