1

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.

Nikunj
  • 987
  • 11
  • 25

1 Answers1

1

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.

Community
  • 1
  • 1
Rob Glassey
  • 2,237
  • 20
  • 21
  • Well, In my case it's returning value with key "NSMetadataItemFSCreationDateKey" but not in sorted order. And yes I had did the same way as after get result. – Nikunj Dec 02 '13 at 07:13
  • 2
    yup by using NSMetadataItemFSCreationDateKey, on iOS6 it's returning nil array, on iOS7 it's working fine. – Nikunj Dec 09 '13 at 10:01
  • Cool. Thanks for the update - hadn't realised they'd fixed that one on iOS 7. – Rob Glassey Dec 09 '13 at 10:10