English 中文(简体)
按谓词对NSFetchRequest排序
原标题:Sort NSFetchRequest by a predicate

我有一个SQLite支持的核心数据存储,希望使用NSFetchRequest获取托管对象的列表。我希望上述列表按布尔值排序,该值可以在数据库级别轻松计算。我知道这一点是因为使用NSPredcate可以制定相同的条件,如下所示:

[NSPredicate predicateWithFormat:@"uid = %@", currentUID]

遗憾的是,似乎没有办法使用NSSortDescriptor来制定这样的条件。我该怎么做最好?我要拿两张单子吗,一张有

[NSPredicate predicateWithFormat:@"uid = %@", currentUID]

还有一个

[NSPredicate predicateWithFormat:@"uid != %@", currentUID]

and combine them later on? Can I then still elegantly use a NSFetchedResultsController?
Or should I fetch all items and sort them later in code? Or is there anything I’ve missed.

问题回答

只需创建NSSortDescriptor的数组(即使它只包含一个元素),即可根据需要对结果进行排序。

您可以使用setSortDescriptors:来设置它们。

fetch可以同时处理谓词和排序。

你的要求没有道理。

谓词用于选择应在一组结果中返回哪些对象。这是通过根据每个对象评估谓词来确定的,以查看谓词的评估结果是YES还是NO(二进制值,或两个可能值之一)。如果评估结果为YES,则包含该对象。如果计算结果为<code>NO</code>,则排除该对象。没有中间立场。

相比之下,排序描述符的计算结果为小于等于大于。换句话说,它是一个三元值(它可以是3事物之一)。没有办法用谓词来表达三元值(因为谓词是二进制的),所以使用谓词作为排序描述符是完全没有意义的。API不允许这样做,因为逻辑不允许这样。

所以真正的问题是:你想做什么?为什么你认为你的排序描述符中需要一个谓词?





相关问题
Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

How to get a flash url in a webpage using a webframe?

As we know , When we load frame from webpage of safari, we will invoke the delegate methods of webkit informal protocol(WebFrameLoadDelegate): webView:didStartProvisionalLoadForFrame: webView:...

热门标签