Photokit preserve user album list order

848 views Asked by At

Is there any way to preserve the list order a user has arranged all of their albums in the Photos app? I've been trying to fix this all day, and I can't seem to figure out what I'm missing to preserve the order for displaying in my app.

I would like to preserve the order because a user can rearrange their albums in the Photos app, so I want my UI to match the list order.

Code I'm using to set up the list:

PHFetchResult *smartAlbumFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:
PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

PHFetchResult *albumFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:
PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

NSArray *collectionFetches = @[smartAlbumFetchResult, albumFetchResult];

NSMutableArray *albumArray = [NSMutableArray new];

for (PHFetchResult *newFetch in collectionFetches)
{
    for (PHAssetCollection *sub in newFetch)
    {
        [albumArray addObject:sub];
         // Other setup omitted
    }
}
2

There are 2 answers

0
asclepix On BEST ANSWER

Sorry, but I don't think it's possible actually (I hope in some upgrades).
In fact, it is possible to use PHFetchOptions to sort asset collections, but PhotoKit supports only a restricted set of keys for the predicate and sortDescriptors properties (see PHFetchOptions) and between these I can't see a key for the the list order a user has arranged in the Photos app.

5
Revils On

Can't you make a property, in the PHAssetCollection class, which takes the order into account? That way you can show them based on the property.

A very simple example, if the class contains a number from 1 - n, based on that number you know which class/photo to show?(Or sort them based on that property and you can just loop it after the sort.)

@Edit The simple example:

In the class PHAssetCollection in the .h file you declare a property.

@property (retain, nonatomic) NSInteger id;

In the class PHAssetCollection in the .m file you can fill him in the initializer method. If you want to use him in the .m file you have to synthesize him. You can off course choose to fill how ever and when ever you want, that depends on your needs. I can imagine that they get a standard id and when a user drags a photo it sets a new id onto it.

You can sort your list based on the following, implement the compare method and call the compare functionality:

//Implement
- (NSComparisonResult)compare:(PHAssetCollection *)otherPhoto{
    return [self.id compare:otherPhoto.id];
}

//Call method
NSMutableArray *sortedArray;
sortedArray = [sub sortedArrayUsingSelector:@selector(compare:)];

There are many ways to accomplish it. Another one more complicated is to keep track of the order in the background and not change the id every time the order gets changed. When the ApplicationWillTerminate or ApplicationDidEnterBackground is invoked, then you change the id of the photos based on the order of photos. That way you will only need to change the id one time and that will save you some performance.