开发者

Complex CoreData query

开发者 https://www.devze.com 2023-04-03 16:38 出处:网络
I am very new to CoreData and would like to do something like this. I have 2 Entities Cave.titleCondition.date

I am very new to CoreData and would like to do something like this.

I have 2 Entities

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

I need to query all Conditions and sort it by date. (Latest first) Then I need to get their cave.title but each cave should show only once (latest conditions)

Example

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

This should show like this

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

Any idea on how I could get this done?

In SQL I would do it like this

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;

Output

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 Georg开发者_如何学Pythones (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)


I would fetch once to get all the conditions sorted by date and then do some logic to filter the fetched data to get the titles.

If you need to see some code let me know.

Edit: OK let me try..

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];

I'm not sure haven't tried it myself, but I think this should work. Let me know how it goes..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号