2011-09-08 4 views
0

저는 CoreData를 처음 사용하여 이와 같은 작업을 수행하고자합니다. 복잡한 CoreData 쿼리

나는 2 엔티티 나는 모든 조건을 조회하고 날짜를 정렬 할 필요가
Cave.title   Condition.date 
Cave.conditions <-->> Condition.cave 

있습니다.

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) 

모든 같이 표시해야합니다

예 (최신순) 그때 나는 그들의 cave.title를 얻을 필요가 있지만, 각 동굴은 한 번만 (최신 상태)를 표시해야합니다 이 일을 어떻게 처리 할 수 ​​있을지에 대한 아이디어? 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) 

답변

0
나는 날짜별로 정렬 조건을 모두 가져온 다음에 가져온 데이터를 필터링하는 일부 로직을 한 번 가져 오는 것

제목을 얻으십시오.

몇 가지 코드가 필요한 경우 알려주십시오.

편집 : OK 나 자신을 그것을 시도하지 않은 확실하지 않다

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

.. 내가하려고하자,하지만 난이 일을한다고 생각합니다. 그것이 어떻게되는지 알려주세요.

+0

나는 이것이 바로 직접 풀릴 수 있다고 확신합니다. 이전 게시물을 편집하고 SQL DB에있는 경우 실행할 SQL 코드를 추가했습니다. – Chris

+0

위의 내 대답을 편집했습니다. – Octoshape

+0

작동하지 않는 경우 setPropertiesToFetch를 다음과 같이 시도하십시오. [NSArray arrayWithObject : @ "nameOfRelationship.title"]; ('nameOfRelationship'은 조건에서 동굴로의 관계 이름이고 다른 이름은 아닙니다.) – Octoshape