如何獲取父母的所有子實體?NSFetchRequest父母的所有子女
我有一個由Core Data中的父實體填充的表。當用戶觸摸一個單元格時,我打算用該父表格的所有子元素顯示另一個表格。
請問NSFetchRequest
是怎麼樣的?
編輯:
模式是這樣的:
學生>>日期[一對多,一個學生有多少天]
所以我希望所有日期任何給定的學生(通過觸摸學生表格單元格爲該學生選擇),然後爲該學生填充日期表格。
謝謝!
如何獲取父母的所有子實體?NSFetchRequest父母的所有子女
我有一個由Core Data中的父實體填充的表。當用戶觸摸一個單元格時,我打算用該父表格的所有子元素顯示另一個表格。
請問NSFetchRequest
是怎麼樣的?
編輯:
模式是這樣的:
學生>>日期[一對多,一個學生有多少天]
所以我希望所有日期任何給定的學生(通過觸摸學生表格單元格爲該學生選擇),然後爲該學生填充日期表格。
謝謝!
在你的第一個表的委託,當你觸摸特定的單元格,我將注入特定的父屬性到第二個表控制器。例如:
SecondController secondController = ... // alloc-init
secondController.studentToGrab = ...
其中SecondController
聲明有studentToGrab
性質類似如下:
@property (nonatomic, retain) Student* studentToGrab; // use strong with ARC, if non-ARC remember to release it
和定義合成它。
然後在您的第二個控制器,viewDidLoad
方法中,你可以這樣做:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"YourNameEntityForDate" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"student == %@", studentToGrab];
[fetchRequest setPredicate:predicate];
// you can also use a sortdescriptors to order dates...
NSError *error = nil;
NSArray *resultArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error != nil) {
NSLog(@"Error: %@", [error localizedDescription]);
abort();
}
// use resultArray to populate something...
的一句話當你處理表,你也可以使用NSFetchedResultController
類。用於在表格中顯示數據時具有優勢。
假設實體和類名是Student
和Date
,併爲Date
反向關係 - >Student
被稱爲student
,
Student *aStudent = ...;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName: @"Date" inManagedObjectContext: [aStudent managedObjectContext]]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"student == %@", aStudent]];
不錯的貼子。關鍵是相反的關係。如果沒有反向關係,則使用獲取請求會更困難。 – 2012-05-10 14:24:08
如果你有自定義類,你可以遍歷產生的關係( return [student dates]
)。這將讓你一個無序的NSSet上的iOS4,或者,你可以獲取請求做(注意我用ARC所以沒有發佈/這裏自動釋放):
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Date"
inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
NSMutableArray *predicates = [NSMutableArray arrayWithCapacity:3];
[predicates addObject:[NSPredicate predicateWithFormat:@"student == %@", aStudent]];
// You might add other predicates
[fetchRequest setPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:predicates]];
// and if you want sorted results (why not, get the database to do it for you)
// sort by date to the top
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"dateAdded" ascending:NO]];
}
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *sorted = [moc executeFetchRequest:fetchRequest error:&error];
if (error) {
// Handle the error, do something useful
}
return sorted;
您不需要爲此單獨獲取請求。通過訪問來自學生對象的關係可以獲得從一對多關係中的所有對象(不要稱之爲子實體,這是誤導性的和不正確的) - 例如student.dates
。這給你一個NSSet,你可以對它進行排序,並在需要時將它轉換爲數組。
沒有模型很難理解問題。分享一些細節。 – 2012-02-26 11:42:41
@Flex_Addicted,謝謝請參閱編輯 – MaKo 2012-02-26 11:46:52