我从NSManagedObjectContext获取我的数据时出现故障 [英] I got fault when I fetched my data from NSManagedObjectContext

查看:125
本文介绍了我从NSManagedObjectContext获取我的数据时出现故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行我的应用程序,然后抓取我的数据。数据确定。当我第二次跑的时候,我的故事是我的旧价值观。哪里不对?

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

首次运行

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})"
) 

第二个值是错误

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})"
) 


推荐答案

< fault> 并不意味着你的数据被破坏了,这意味着它是动态链接到获取的结果,实际的对象将被加载,当任何尝试访问任何value / property from the object。记住 - 你在.m文件中使用了@dynamic?
这就是为什么当你<$ c $时显示< fault> 当你访问对象的任何属性时,c> NSLog 与数组和故障消失 NSLog(@Test:%@,test.text);

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

这篇关于我从NSManagedObjectContext获取我的数据时出现故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆