2013-06-05 28 views
1

我使用core-data和hava 2 embadded relationship to-many。我還有一個日曆,我應該在選定的日期下載所有疾病中的所有藥物。我如何使用子查詢或使用子查詢?如何使用子查詢或如何獲取數據

Disease ->>Medicine->>Date 

截圖是這裏http://savepic.ru/4608231.png

我Couild搜索所有藥品不與疾病結合?

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
[request setEntity:[NSEntityDescription entityForName:@"MyMedicine" inManagedObjectContext:objectContext]]; 
[request setSortDescriptors:[NSArray initWithObject:[[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]]; 
[request setPredicate:[NSPredicate predicateWithFormat:@"(data == %@)", currentData]]; 
+0

你可以給出核心數據模式的截圖嗎?它會消除很多猜測工作。 – Anupdas

+0

我是。日期和時間也是日期的一部分。 – user2273459

回答

0

假設你初始化NSFetchRequest這樣的:

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
[request setEntity:[NSEntityDescription entityForName:@"MyMedicine" inManagedObjectContext:objectContext]]; 
[request setSortDescriptors:[NSArray initWithObject:[[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]]; 

你可以設置你的謂詞是這樣的:

// Dates 
[request setPredicate:[NSPredicate predicateWithFormat:@"date == %@", datesEntity]]; 

// NSDate 
[request setPredicate:[NSPredicate predicateWithFormat:@"date.date == %@", date]]; 

作爲默認所有關係將會牽強,因爲斷層,如果你需要他們獲取在此獲取您可以配置關係的關鍵路徑,在這中獲取取這樣的:

NSArray* relationKeys = [NSArray arrayWithObject:@"disease"]; 
[request setRelationshipKeyPathsForPrefetching:relationKeys]; 

使用這種方法,所有相關Disease實體將與所有一起被取出匹配Dates實體。

+0

謝謝。 [請求...]會返回我是或否的結果,以及如何獲取藥物清單? – user2273459

+0

不知道我是否正確,但只是執行我上面描述的請求。 –

+0

我這樣做。謝謝 – user2273459

相關問題