如何按修改日期对UIDocument的icloud NSMetadataQuery结果进行排序? [英] How can I sort an icloud NSMetadataQuery result of UIDocument's by modification date?

查看:263
本文介绍了如何按修改日期对UIDocument的icloud NSMetadataQuery结果进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,它使用一个简单的UIDocument模型和一个内容字段用于iCloud存储。我检索文档列表以使用NSMetadataQuery填充UITableView。我希望在此表的顶部显示新文档,但默认排序似乎是旧的 - >新的。

I have an iOS application that uses a simple UIDocument model with a single content field for iCloud storage. I retrieve the list of documents to populate a UITableView using an NSMetadataQuery. I want to have new documents appear at the top of this table, but the default sorting appears to be old -> new.

我知道如果我在文档中有自定义日期字段,我可以在该字段上使用NSSortDescriptor对查询进行排序,但我的问题是是否可以按内在排序通过UIDocument iCloud存储创建的文件的修改日期?或者还有另一种首选方法吗?

I know if I had a custom date field in the document I could sort the query with the NSSortDescriptor on that field but my question is is it possible to sort by the intrinsic modification date of the file created through the UIDocument iCloud storage? Or is there another preferred method to do this?

推荐答案

看起来正确的方法是设置排序描述符您创建它时的查询。这些内容:

Looks like the right way to do this would be to set the sort descriptor of your query when you create it. Something along these lines:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObject:
                            NSMetadataQueryUbiquitousDocumentsScope]];

NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K ENDSWITH '.bin'", 
                                   NSMetadataItemFSNameKey];
[query setPredicate:pred];

NSSortDescriptor *sortDescriptor = 
    [[[NSSortDescriptor alloc] initWithKey:NSMetadataItemFSContentChangeDateKey 
                                 ascending:FALSE] autorelease]; //means recent first
NSArray *sortDescriptors = [NSArray arrayWithObjects:
                                sortDescriptor,
                                nil];        
[query setSortDescriptors:sortDescriptors];

[[NSNotificationCenter defaultCenter] 
     addObserver:self 
     selector:@selector(queryDidFinishGathering:) 
     name:NSMetadataQueryDidFinishGatheringNotification 
     object:query];

[query startQuery];

这篇关于如何按修改日期对UIDocument的icloud NSMetadataQuery结果进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆