iosiphonesortingicloudnsmetadataquery

iCloud - NSMetadataQuery not sorting with NSMetadataItemFSCreationDateKey


I am getting list of files in array from iCloud, I want that files in sorting order either with NSMetadataItemFSCreationDateKey or NSMetadataItemFSContentChangeDateKey. But it's not returning as per given NSSortDescriptor.

Here is my code, please suggest me if anything is missing or i need to add anything.

-(void)getiCloudData
{

[appdel.metadataQuery setSearchScopes:[NSArray arrayWithObjects: NSMetadataQueryUbiquitousDocumentsScope,NSMetadataQueryUbiquitousDataScope ,nil]];
[appdel.metadataQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*'", NSMetadataItemFSNameKey]];

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:NSMetadataItemFSCreationDateKey
                             ascending:FALSE] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:
                            sortDescriptor,
                            nil];
[appdel.metadataQuery setSortDescriptors:sortDescriptors];

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(queryDidReceiveNotification:)
                                         name:NSMetadataQueryDidFinishGatheringNotification
                                       object:appdel.metadataQuery];
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(updatequeryDidReceiveNotification:)
                                         name:NSMetadataQueryDidUpdateNotification
                                       object:appdel.metadataQuery];
   [appdel.metadataQuery enableUpdates];
   [appdel.metadataQuery startQuery];
}

Thanks.


Solution

  • I believe NSMetadataItemFSCreationDateKey always returns as nil, see this, so don't even try sorting on that.

    Now, I don't sort by NSMetadataItemFSContentChangeDateKey, though I do query it using valueForAttribute: from the NSMetadataItem when the query results come through and that works fine at that point. So focus your efforts on NSMetadataItemFSContentChangeDateKey.

    If you can't get the query results to come through sorted, one possible approach is to sort the results after you get them through - when you're processing your query results. I've used that when I've had an unusual sorting requirement in the past.