当对象具有“描述"时,Core Data 无法解决故障.属性? [英] Core Data cannot resolve faults when object has "description" attribute?

查看:20
本文介绍了当对象具有“描述"时,Core Data 无法解决故障.属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"A"
                                          inManagedObjectContext:moc];
[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"somePredicate", someObject];
[fetchRequest setPredicate:predicate];

frc = [[NSFetchedResultsController alloc]
       initWithFetchRequest:fetchRequest
       managedObjectContext:moc
       sectionNameKeyPath:@"recency"
       cacheName:@"frc"];
[fetchRequest release];

frc.delegate = self;

NSError *error;
BOOL success = [frc performFetch:&error];
if (!success) {
    NSLog(@"error: %@", error);
}

for (A *a in [frc fetchedObjects]) {        
    [someMutableArray addObject:a.b];
    [someMutableArray addObject:a];
}

数据模型:

A 和 B 是实体.A 与 B 具有强制一对一关系.B 与 A 具有反向可选对多关系.

A and B are entities. A has mandatory to-one relation to B. B has inverse optional to-many relation to A.

以上为英文:

初始化一个 NSFetchedResultsController 来获取一些数据来为 tableview 提供动力.初始获取后,将数据放在一边进行一些处理.

Initialize a NSFetchedResultsController to grab some data to power a tableview. After initial fetch, set the data aside for some processing.

现在,稍后,我尝试这样做:

Now, later, I try to do this:

id object = [someMutableArray objectAtIndex:someIndex];
NSLog(@"%@", object);

if ([object isMemberOfClass:[B class]]) {
    someVar = object.propertyFromB; // problem
} else if ([object isMemberOfClass:[A class]]) {
    someVar = object.propertyFromA;
}

问题/问题:标有问题"的行崩溃了.(请参阅下面的解决方案,但仍需要解释.)

Question/problem: the line indicated with "problem" crashes. ( See below for resolution, but would still like an explanation.)

上面的 NSLog 调用产生:

The NSLog call above yields:

2010-01-30 14:47:14.433 app[22618:20b] <B: 0xf7f750> (entity: B; id: 0xf7ba70 <x-coredata://B01FEC86-14D6-4973-BFDB-EDE4AFD24FDC/B/p4> ; data: <fault>)
2010-01-30 14:47:14.438 app[22618:20b] <A: 0xf7e360> (entity: A; id: 0xf35820 <x-coredata://B01FEC86-14D6-4973-BFDB-EDE4AFD24FDC/A/p6> ; data: {
    prop1 = value1;
    prop2 = value2;
    ... etc ...
})

即根据有问题的行,如果对象是 A 类型,则它已出现故障并且在内存中可用,但如果是 B,则它是一个故障.

I.e by the problematic line, if the object was of type A, it has been faulted and is available in memory, but if it is B, it's a fault.

我的理解是问题"行应该触发故障并从存储中获取数据,但这并没有发生.我想了解/调试原因.我试过围绕这个插入 willAccessKey/didAccessKey 调用.我还尝试在获取请求上设置 setRelationshipKeyPathsForPrefetching:"b" .都没有用.

My understanding is that the "problem" line should fire the fault and fetch the data from store, but this is not happening. I would like to understand/debug why. I have tried inserting willAccessKey/didAccessKey calls around this. I also tried to set setRelationshipKeyPathsForPrefetching:"b" on the fetch request. Neither worked.

我的假设是,由于我在某种程度上滥用了 NSFetchedRequestController 结果,故障引擎在此过程中变得困惑并且没有在它应该获取的时候获取故障.所以我想一种蛮力的方法是创建一个新的手动获取请求,以在正确的时间获取相关的 B 对象.但是有更好的方法吗?

My hypothesis is that since I'm somewhat abusing the NSFetchedRequestController results, the faulting engine gets confused along the way and doesn't fetch the fault when it's supposed to. So I guess a bruteforce way would be to create a new manual fetch request to fetch the related B object at the right time. But is there a better way?

问题是对象 B 有一个我定义的属性描述",但这与 NSObject 的内置名称冲突.Xcode 总是给我警告,但我忽略了它们,因为我认为描述"内部属性​​/方法仅用于将字符串转储到控制台等,而不是内部处理.

The problem was that object B had a property "description" that I had defined, but that collides with NSObject's built-in name. Xcode always gave me warnings, but I ignored them because I thought "description" internal property/method is only used for dumping strings to console and the like, not internal processing.

在我制作了模型的新版本后,问题就消失了,将描述"重命名为其他名称.所有故障都开始按预期工作.

The problem disappeared after I made a new version of my model, renaming "description" to something else. All the faulting started to work as expected.

不过,我不明白这是怎么回事.Core Data 是否使用对象的描述"方法进行一些内部自省?

I don't understand, though, what is going on. Is Core Data using the objects' "description" method for some internal introspection?

推荐答案

来自 核心数据编程指南

不鼓励覆盖描述——如果此方法在调试操作期间触发错误,结果可能不可预测——以及 initWithEntity:insertIntoManagedObjectContext:.您通常不应覆盖键值编码方法,例如 valueForKey: 和 setValue:forKeyPath:.

You are discouraged from overriding description—if this method fires a fault during a debugging operation, the results may be unpredictable—and initWithEntity:insertIntoManagedObjectContext:. You should typically not override the key-value coding methods such as valueForKey: and setValue:forKeyPath:.

-description 是 NSObject 中的一个方法,它返回对象的字符串表示.在 NSLog(@"%@", object) 行中,-description 用于获取您在控制台中看到的字符串.键值编码将最终使用方法来获取描述属性的属性.这给 Core Data 造成了很多混乱.

-description is a method in NSObject that returns a string representation of your object. In the line NSLog(@"%@", object), -description is used to get the string that you see in the console. Key-value coding will end up using the method to get the property for the description attribute. This causes a whole lot of confusion to Core Data.

编程指南说不鼓励"时很慷慨.他们的真正意思是是的,它会破坏你的东西."

The programming guide is being generous when it says "discouraged". They really mean "Yeah, it's going to break your stuff."

该链接还有一个很好的其他方法列表,如果您覆盖它们,这些方法会破坏您的内容.

That link also has a good list of other methods that will break your stuff if you override them.

这篇关于当对象具有“描述"时,Core Data 无法解决故障.属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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