复杂的CoreData查询

时间:2011-09-08 07:46:07

标签: sorting core-data nspredicate

我是CoreData的新手,想做类似的事情。

我有2个实体

Cave.title            Condition.date
Cave.conditions <-->> Condition.cave

我需要查询所有条件并按日期对其进行排序。 (最新的) 然后我需要得到他们的cave.title但每个洞穴应该只显示一次(最新条件)

实施例

Condition1 (06.09.2011) - Cave1
Condition2 (05.09.2011) - Cave3
Condition3 (05.09.2011) - Cave1
Condition4 (04.09.2011) - Cave5

这应该像这样显示

Cave1 (06.09.2011)
Cave3 (05.09.2011)
Cave5 (04.09.2011)

关于如何完成这项工作的任何想法?

在SQL中,我会这样做

SELECT DISTINCT c.title as title, c.caveID as caveID, cn.countryshort as countryshort, MAX(cc.divedate) as divedate
                FROM caves as c, countries as cn, caveconditions as cc 
                WHERE cn.countryID = c.countryID 
                AND c.caveID = cc.caveID 
                GROUP BY c.title            
                ORDER BY divedate DESC;

输出

2011-09-08 13:39:24.951 CaveConditions[23026:11903] Chaudanne (1287350157)
2011-09-08 13:39:24.952 CaveConditions[23026:11903] Sorgente Bossi (1287333080)
2011-09-08 13:39:24.953 CaveConditions[23026:11903] Elefante Bianco (1287248755)
2011-09-08 13:39:24.953 CaveConditions[23026:11903] Cogol dei Siori - Oliero (1287248678)
2011-09-08 13:39:24.954 CaveConditions[23026:11903] Source du Lison (1287324493)
2011-09-08 13:39:24.955 CaveConditions[23026:11903] Resurgénce de Gouron (1287324296)
2011-09-08 13:39:24.955 CaveConditions[23026:11903] Fontaine du Truffe (1287006107)
2011-09-08 13:39:24.956 CaveConditions[23026:11903] Gouffre de Cabouy (1287005780)
2011-09-08 13:39:24.957 CaveConditions[23026:11903] Emergence du Ressel (1286908470)
2011-09-08 13:39:26.037 CaveConditions[23026:11903] Source de l'Orbe (1287175659)
2011-09-08 13:39:26.120 CaveConditions[23026:11903] Bätterich (1286812411)
2011-09-08 13:39:26.220 CaveConditions[23026:11903] Cogol dei Siori - Oliero (1286787535)
2011-09-08 13:39:26.288 CaveConditions[23026:11903] Fontaine de Saint Georges (1286744641)
2011-09-08 13:39:26.379 CaveConditions[23026:11903] Source du Doubs (1286736293)
2011-09-08 13:39:26.480 CaveConditions[23026:11903] Source Bleue (Montperreux) (1286736150)
2011-09-08 13:39:26.613 CaveConditions[23026:11903] Source Bleue Cusance (1286814108)
2011-09-08 13:39:26.796 CaveConditions[23026:11903] Fontaine de Saint Georges (1286652629)
2011-09-08 13:39:27.096 CaveConditions[23026:11903] Source de l'Orbe (1286735940)
2011-09-08 13:39:27.846 CaveConditions[23026:11903] Gouffre de Cabouy (1286568932)

1 个答案:

答案 0 :(得分:0)

我会获取一次以获取按日期排序的所有条件,然后执行一些逻辑来过滤获取的数据以获取标题。

如果您需要查看一些代码,请告诉我们。

编辑:好的,让我试试..

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
                          initWithKey:@"date" ascending:NO];
[fetch setSortDescriptors:[NSArray arrayWithObject:sort]];

NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"Condition" inManagedObjectContext:self.managedObjectContext];
[fetch setEntity:entity];

[fetch setReturnsDistinctResults:YES];
[fetch setPropertiesToFetch:[NSArray arrayWithObject:@"cave.title"];

NSError *error;
[self.managedObjectContext executeFetchRequest:fetch error:&error];

[fetch release];
[sort release];

我不确定自己没有尝试过,但我认为这应该有效。让我知道它是怎么回事......