开发者

What changes in Core Data after a save?

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

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?

Edit

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.

http://emberapp.com/splash6/images/littlesnapper/sizes/m.png

Edit

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

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", @开发者_JAVA技巧"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.

Edit

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

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?


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.

update

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?

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?

update

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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜