保存后Core数据有什么变化? [英] What changes in Core Data after a save?

查看:203
本文介绍了保存后Core数据有什么变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于核心数据的mac应用程序,它工作得很好,直到我保存一个文件。当我保存文件时,核心数据似乎发生了变化,因为我的原始提取请求不再提取任何内容。这是抓取请求,在保存前工作,但在保存后返回空数组。

I have a Core Data based mac application that is working perfectly well until I save a file. When I save a file it seems like something changes in core data because my original fetch request no longer fetches anything. This is the fetch request that works before saving but returns an empty array after saving.

NSEntityDescription *outputCellEntityDescription = [NSEntityDescription entityForName:@"OutputCell" 
                                                                   inManagedObjectContext:[[self document] managedObjectContext]];
NSFetchRequest *outputCellRequest = [[[NSFetchRequest alloc] init] autorelease];
[outputCellRequest setEntity:outputCellEntityDescription];
NSPredicate *outputCellPredicate = [NSPredicate predicateWithFormat:@"(cellTitle = %@)", outputCellTitle];
[outputCellRequest setPredicate:outputCellPredicate];
NSError *outputCellError = nil;
NSArray *outputCellArray = [[[self document] managedObjectContext] executeFetchRequest:outputCellRequest 
                                                                                 error:&outputCellError];

我已经使用[[[self document] managedObjectContext] registeredObjects检查过,后保存和没有什么似乎已经改变,对象仍然存在。这可能是相当基本的,但有谁知道我可能会做错了吗?如果没有,任何人都可以给我任何指针,在保存后,Core Data模型可能不同,所以我可能有一些线索为什么获取请求停止工作后保存?

I have checked with [[[self document] managedObjectContext] registeredObjects] to see that the object still exists after the save and nothing seems to have changed and the object still exists. It is probably something fairly basic but does anyone know what I might be doing wrong? If not can anyone give me any pointers to what might be different in the Core Data model after a save so I might have some clues why the fetch request stops working after saving?

编辑

我已经知道,关系似乎在保存后打破。如果我省略为请求设置谓词的行,请求将返回数组中的对象。我已经检查过registrationObjects,它似乎关系是完好无损的,但如果我做一些事情像保存文件,重新打开它,然后检查registeredObjects的关系设置为nil。我已经打开一个保存文件作为xml文件,并且关系看起来在第一次保存文件时是完整的。

I have got as far as working out that it is the relationships that seem to be breaking after a save. If I omit the lines setting a predicate for the request, the request returns objects in the array. I have checked through the registeredObjects and it appears that the relationships are intact, but if I do something like save a file, re-open it and then check the registeredObjects the relationships are set to nil. I've opened a save file as an xml file and the relationships appear to be intact when the file is first saved.

我已经添加了核心数据模型的一部分的屏幕截图,因为关系被破坏了。有谁知道为什么在核心数据中保存文件可能会破坏关系?作为参考,我使用默认实现保存内置到核心数据,所以没有自定义保存代码。

I've added a screen shot of the part of the core data model were the relationships are broken. Does anyone have any idea why saving a file in core data might break the relationships? For reference I'm using the default implementation of save built into core data so there is no custom save code.

编辑

我没有 -awakeFromFetch:这个问题就造成了。

I have no -awakeFromFetch: methods that are triggering when this problem is caused.

对于某些问题对象,我使用Core Recipes模型为KVO子类化 NSManagedObject

I have sub-classed NSManagedObject for some of the problem objects, using the Core Recipes model for KVO:

    +(void)initialize
{
    if (self == [OutputCell class])
    {
        NSArray *nameKeys = [NSArray arrayWithObjects:@"cell", @"sheet", @"table", nil];
        [self setKeys:nameKeys
        triggerChangeNotificationsForDependentKey:@"cellTitle"];

        NSArray *measuresKeys = [NSArray arrayWithObjects:@"fivePercentile", @"maximum", @"mean", @"median",@"minimum",@"ninetyFivePercentile",@"standardDeviation",nil];
        [self setKeys:measuresKeys
        triggerChangeNotificationsForDependentKey:@"analysisResults"];
    }
}

此方法似乎并未触发之后保存,所以它似乎并不是这是导致的问题。我目前正在通过代码中的所有其他方法,以查找是否有任何一个在保存期间或之后被调用。

This method doesn't seem to be firing during or after a save so it doesn't seem to be this that is causing the problem. I'm currently going through all the other methods in the code to find if any of them happen to get called during or after a save.

编辑

继续Marcus的建议,在保存模型之前使抓取请求失败。我的问题现在是当控制台失败时返回的消息:

Following on from Marcus' suggestion below I've managed to make a fetch request fail before the model is saved. My problem now is the message this getting returned in the console when it fails:

HIToolbox: ignoring exception '+entityForName: could not locate an NSManagedObjectModel for entity name 'OutputCell'' that raised inside Carbon event dispatch

控制台消息被记录在此调用之后:

The console message is logged following this call:

NSEntityDescription *outputCellEntityDescription = [NSEntityDescription entityForName:@"OutputCell" 
                                                               inManagedObjectContext:[[self document] managedObjectContext]];

在控制台消息中的OutputCell是否应该有额外的?对象通常有这个额外的,还是它来自某处?如果它来自某处,并导致获取请求失败,任何人都有任何明亮的想法,这可能是来自或我如何跟踪它的来源?

Should the extra ' following the OutputCell in the console message be there? Do objects normally have this extra ', or has it come from somewhere? If it has come from somewhere and is causing the fetch request to fail, does anyone have any bright ideas where this might have come from or how I might track it's source down?

推荐答案

听起来你在某个地方设置nil,导致你回零。我会在调试器中遍历您的保存和获取代码,并在您不需要时将查找设置为nil的对象。

Sounds like you are setting something to nil somewhere and causing you to get nil back. I would walk through your save and fetch code in the debugger and look for objects being set to nil when you do not expect it.

你有任何代码在任何地方,可以操纵关系吗?也许在 -awakeFromFetch:中的某些东西导致关系被破坏?

Do you have any code anywhere that can be manipulating the relationships? Perhaps something in the -awakeFromFetch: that is causing the relationships to get corrupted?

如果他们第一次正确保存,然后失败,那么真正指向代码中的某些东西,从而破坏了这些关系。你是这些对象的子类化 NSManagedObject 吗?如果是,您通过覆盖 -init ... 方法吗?

If they are saving correctly the first time and then failing then that truly points at something in your code corrupting those relationships. Are you subclassing NSManagedObject for these objects? If so are you by chancing overriding the -init... method?

最后一刻,肯定不会有。检查您的抓取请求,这可能都归结为一个字符串中的一个简单的拼写错误...

That last tick should definitely not be there. Check your fetch request, this might all boil down to a simple typo in a string somewhere...

这篇关于保存后Core数据有什么变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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