核心數據Noob here。核心數據保存和加載到多個實體
我有一個項目,它將數據保存到單個實體沒有問題。但是,因爲實體有太多的屬性(超過100),它會收到警告,我需要對其進行規範化。好的,所以我創建了第二個實體來存儲更多的數據和To-One關係。
問題是,當我嘗試保存數據時,數據保存並重新加載到第一個實體,但不會保存或從第二個實例重新加載。我必須錯過簡單的東西。
下面是一些代碼:
//ViewDidLoad
- (void)viewDidLoad
{
if (managedObjectContext == nil) {
managedObjectContext = [(CoreDataStuffAppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
}
// [self addRecord];
[super viewDidLoad];
if ([self fetchData]) {
NSLog(@"after self fetchData patientArray count is %i", [parentArray count]);
if ([parentArray count] == 1) {
Parent *parentInfo = (Parent *)[parentArray objectAtIndex:0];
parentItem1.text = parentInfo.Mother;
NSLog(@"fetching on load %i", [parentArray count]);
//Try 1: just going after the relationship route (FAIL: cuz data wont store. stays null)
// childItem1.text = parentInfo.ParentToChild.Kid;
// Try 2: going the route of directly talking to the second entity
childItem1.text = child.Kid;
}
}
// Save Record
- (void)saveRecord{
if (managedObjectContext == nil) {
NSLog(@"there is no context, arrg");
}
NSLog(@"array count is %i", [parentArray count]);
if ([parentArray count] == 1) {
NSManagedObjectContext *context = managedObjectContext;//[parent managedObjectContext];
NSLog(@"context 1 is: %@", context);
Parent *parentInfo = (Parent *)[parentArray objectAtIndex:0];
parentInfo.Mother = parentItem1.text;
NSLog(@"data says: %@", parentInfo.Mother);
NSLog(@"text syas: %@", parentItem1.text);
// Try 1, save it through the child relationship thing. (failed)
// parentInfo.ParentToChild.Kid = childItem1.text;
// NSLog(@"childSave says: D: %@ T: %@", parentInfo.ParentToChild.Kid , childItem1.text);
// Try 2. from Resipeas app
if (!child) {
self.child = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:context];
[parent addChildObject:child];
NSLog(@"I hit the child");
}
child.Kid = childItem1.text;
NSLog(@"1: childSave says: D: %@ T: %@", parentInfo.ParentToChild.Kid , childItem1.text);
NSLog(@"2: childSave says: D: %@ T: %@", child.Kid , childItem1.text);
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"context 2 is: %@", context);
NSLog(@"major fail %@", [error localizedDescription]);
// abort();
}
}
NSLog(@"saving stuff");
}
// FetchData
- (BOOL)fetchData {
NSLog(@"doing the fetch");
BOOL returnResult = FALSE;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
if (managedObjectContext == nil)
{
NSLog(@"ok making a new managed object context");
managedObjectContext = [(CoreDataStuffAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Parent" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:1];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
if (mutableFetchResults == nil) {
NSLog(@"Fetching Error");
} else {
[self setParentArray:mutableFetchResults];
returnResult = TRUE;
NSLog(@"Fetching went well");
}
[mutableFetchResults release];
[fetchRequest release];
return (returnResult);
}
1st。感謝您花時間給出這樣一個完整的答案。我同意,在這種情況下,關於「患者」的一切都是錯誤的方式。但是,當我嘗試創建多個實體,例如將信息分解爲相關一口大小的塊時,我無法將數據保存到除一個實體外的所有實體。然而,你的評論(2)讓我思考。我認爲我只是在「家長」而不是其他人身上做一次,這可能是我的一個難題。 – 2011-03-12 05:29:49
我很高興能夠重新使用我的模型,實際上,我們正努力爭取如何一次將數據提取並保存到多個實體。再次感謝您的幫助。歡迎任何關於如何與多個實體交談並寫回的更多想法。 – 2011-03-12 05:34:55
我不認爲你讚賞關係的概念。關係允許您獲取一個實體的對象,然後使用這些關係來訪問相關實體的對象。例如。你有一個病人實體和一個像這樣的醫生實體病人<<-->醫生。每個患者對象都有一位醫生,但每位醫生可能有很多患者。如果您想了解特定的患者醫生,請取出該特定的患者對象,然後檢查其Patient.doctor關係以找出有關醫生的所有信息。等等。 – TechZen 2011-03-12 20:17:27