2011-02-24 54 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和我总是读取新的对象,其中的属性是零......当然, ^^