当我从NSManagedObjectContext获取数据时,我遇到了错误

我运行我的应用程序,然后获取我的数据。 数据没问题。 当我第二次跑步时,我的旧价值出现了问题。 哪里不对?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:[self managedObjectContext]]; for (int i =0; i<2; i++) { Test *test = [[[Test alloc] initWithEntity:entity insertIntoManagedObjectContext:[self managedObjectContext]] autorelease]; test.text = @"Text"; test.index = [NSNumber numberWithInt:i]; } [self saveContext]; } -(void) showValues { NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:[self managedObjectContext]]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; [request setEntity:entity]; NSError *error; NSArray *array = [[self managedObjectContext] executeFetchRequest:request error:&error]; NSLog(@"Array: %@ ", array); } 

第一次运行

 2012-01-22 21:48:52.092 Mew[411:707] Array: ( " (entity: Test; id: 0x1856b0  ; data: {\n index = 0;\n text = Text;\n})", " (entity: Test; id: 0x1857e0  ; data: {\n index = 1;\n text = Text;\n})" ) 

第二次运行//第一个和第二个值是错误的

 2012-01-22 21:50:29.892 Mew[429:707] Array: ( " (entity: Test; id: 0x16c720  ; data: )", " (entity: Test; id: 0x16c730  ; data: )", " (entity: Test; id: 0x16bfd0  ; data: {\n index = 0;\n text = Text;\n})", " (entity: Test; id: 0x16c100  ; data: {\n index = 1;\n text = Text;\n})" ) 

并不意味着您的数据已损坏。 这意味着它与获取的结果动态链接,并且在尝试访问对象的任何值/属性时将加载实际对象。 记得 – 你在.m文件中使用了@dynamic? 这就是为什么当您访问NSLog with array并且当您访问对象NSLog(@"Test: %@ ", test.text);任何属性时,它显示并且故障消失NSLog(@"Test: %@ ", test.text);

我修了一个错误。

我刚改变

  NSLog(@"Array: %@ ", array); 

 for (Test *test in array) { NSLog(@"Test: %@ ", test.text); } 

故障消失了