当我从 NSManagedObjectContext 获取数据时出现错误

发布于 2024-12-28 09:09:26 字数 2166 浏览 4 评论 0原文

我运行我的应用程序,然后获取我的数据。数据没问题。当我第二次跑步时,我对自己的旧价值观产生了错误。怎么了?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2025-01-04 09:09:26

并不意味着您的数据已损坏。这意味着它与获取的结果动态链接,并且当尝试从对象访问任何值/属性时,将加载实际对象。还记得吗 - 你在 .m 文件中使用过 @dynamic 吗?
这就是为什么当您使用数组进行 NSLog 时它会显示 ,而当您访问对象的任何属性 NSLog(@"Test: %@ " 时错误就会消失,测试.文本);

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

满栀 2025-01-04 09:09:26

我修复了一个错误。

我刚刚从 改为

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

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

故障就消失了

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文