I got fault when I fetched my data from NSManagedObjectContext

点点圈 提交于 2019-12-07 14:48:22

问题


I run my app and then fetched my data. Data is ok. When I run second time I got fault for my old values. What is wrong?

- (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);    
}

first run

2012-01-22 21:48:52.092 Mew[411:707] Array: (
"<Test: 0x183f60> (entity: Test; id: 0x1856b0 <x-coredata://90165BCF-D2DE-4661-9B12-33EF86F0C09F/Test/p1> ; data: {\n    index = 0;\n    text = Text;\n})",
"<Test: 0x184940> (entity: Test; id: 0x1857e0 <x-coredata://90165BCF-D2DE-4661-9B12-33EF86F0C09F/Test/p2> ; data: {\n    index = 1;\n    text = Text;\n})"
) 

second run // first and second values are fault

2012-01-22 21:50:29.892 Mew[429:707] Array: (
"<Test: 0x16c950> (entity: Test; id: 0x16c720 <x-coredata://90165BCF-D2DE-4661-9B12-33EF86F0C09F/Test/p1> ; data: <fault>)",
"<Test: 0x16d130> (entity: Test; id: 0x16c730 <x-coredata://90165BCF-D2DE-4661-9B12-33EF86F0C09F/Test/p2> ; data: <fault>)",
"<Test: 0x1684c0> (entity: Test; id: 0x16bfd0 <x-coredata://90165BCF-D2DE-4661-9B12-33EF86F0C09F/Test/p3> ; data: {\n    index = 0;\n    text = Text;\n})",
"<Test: 0x16ab90> (entity: Test; id: 0x16c100 <x-coredata://90165BCF-D2DE-4661-9B12-33EF86F0C09F/Test/p4> ; data: {\n    index = 1;\n    text = Text;\n})"
) 

回答1:


<fault> does not mean that your data is corrupted. It means that it is dynamically linked with the fetched results and the actual object will be loaded when any attempt is made to access any value/property from the object. remember - you have used @dynamic in .m file? That's why it shows <fault> when you NSLog with array and fault disappears when you access any property of the object NSLog(@"Test: %@ ", test.text);




回答2:


I fixed a bug.

I've just change from

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

to

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

and fault was disappeared



来源:https://stackoverflow.com/questions/8963823/i-got-fault-when-i-fetched-my-data-from-nsmanagedobjectcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!