2011-02-24 67 views
0

我需要幫助!^^目標C核心數據 - 屬性不會被保存持久

我在屬性(名稱,女士prename)的人的兩個名字,並保存它們寫。如果我嘗試在另一個視圖中訪問attrubutes,那麼它們是零。我不明白爲什麼?!?

我這樣做了。我使用getProfile方法獲取profileContext,並使用Dot-Notation訪問屬性,然後保存它。我的NSLog顯示我正確的名字和我的提取。

ownProfile = [[MyProfile alloc] init]; 
profileContext = [ownProfile getProfile]; 
ownProfile = (MyProfile*)[NSEntityDescription insertNewObjectForEntityForName:@"MyProfile" inManagedObjectContext:profileContext]; 
ownProfile.Vorname = @"Max"; 
ownProfile.Nachname = @"Wilson"; 
NSLog(@"%@",ownProfile.Nachname); 



if ([profileContext hasChanges]) { 
    NSLog(@"It has changes!"); 
    [profileContext save:nil]; 
} 

//Fetching 
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"MyProfile" inManagedObjectContext:profileContext]; 
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
[request setEntity:entityDescription]; 

NSArray *array = [profileContext executeFetchRequest:request error:nil]; 
for (int i=0; i < [array count]; i++) { 
    MyProfile *object = [array objectAtIndex:i]; 
    NSLog(@"Name: %@",object.Nachname); 
} 

如果我嘗試訪問另一個ViewController子類中的屬性,它們是零。這是代碼:

- (void)viewDidLoad { 
[super viewDidLoad]; 

ownProfile = [[MyProfile alloc] init]; 
NSManagedObjectContext *profileContext = [ownProfile getProfile]; 
ownProfile = [NSEntityDescription insertNewObjectForEntityForName:@"MyProfile" inManagedObjectContext:profileContext]; 

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"MyProfile" inManagedObjectContext:profileContext]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entityDescription]; 
[request setIncludesPropertyValues:NO]; //only fetch the managedObjectID 

NSArray *array = [profileContext executeFetchRequest:request error:nil]; 
[request release]; 
MyProfile *object = [array objectAtIndex:[array count]-1]; 
NSLog(@"%@",object); 

}

我getProfile方法是在NSManagedObjectClass,看起來像這樣:

-(NSManagedObjectContext*) getProfile { 


NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], 
         NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], 
         NSInferMappingModelAutomaticallyOption, nil]; 


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
NSLog(@"basePath = %@",basePath); 
NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingFormat:@"CoreData.sqlite"]]; 
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]]; 
NSLog(@"PersistentStore = %@",persistentStoreCoordinator); 
NSError *error = nil; 

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
    NSLog(@"error loading persistent store.."); 
    [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
} 

NSManagedObjectContext *profile = [[NSManagedObjectContext alloc] init]; 
[profile setPersistentStoreCoordinator:persistentStoreCoordinator]; 


return profile; 

}

請幫幫我!^^

回答

0

嘿傢伙......我解決了我的問題! :)

我的錯是,我在viewDidLoad中-方法添加的行

ownProfile = [NSEntityDescription insertNewObjectForEntityForName:@"MyProfile" inManagedObjectContext:profileContext]; 

另一個對象在persistentStore和我總是讀取新的對象,其中的屬性是零......當然, ^^