开发者

NSManagedObject Faulted

开发者 https://www.devze.com 2023-04-07 04:25 出处:网络
I have an NSManagedObject that has some of its properties initialized at the start of the program. When I refer to this object later, it appears to be faulted, and the properties are not accessible. I

I have an NSManagedObject that has some of its properties initialized at the start of the program. When I refer to this object later, it appears to be faulted, and the properties are not accessible. I'm not sure what I need to do.

This is related to a new feature added to a program that has been operating smoo开发者_如何学运维thly with core-data in all other ways.

Here is a code snippet where it is initialized as a property value of a singleton. (That singleton is accessible by many parts of my code):

    favoritesCollection = [[SearchTerms alloc] initWithEntity:[NSEntityDescription entityForName:@"SearchTerms" inManagedObjectContext:moc] insertIntoManagedObjectContext:moc];

    favoritesCollection.keywords = @"Favorites List";
    favoritesCollection.isFavoritesCollection = [NSNumber numberWithBool:YES];
    favoritesCollection.dateOfSearch = [NSDate NSCExtendedDateWithNaturalLanguageString:@"4000"];
    favoritesCollection.pinColorIndex = 0;  

    [moc save:&error];

    NSLog(@"(favoritesCollection) = %@", favoritesCollection);
}

return favoritesCollection;

When I look at favoritesCollection with the NSLog, I see this (I added some newlines to make it easier to read):

(favoritesCollection) = 
<SearchTerms: 0x5c28820> 
(entity: SearchTerms; id: 0x5a6df90 
<x-coredata://3936E19F-C0D0-4587-95B6-AA420F75BF78/SearchTerms/p33> ; 
data: {
    dateOfSearch = "4000-09-25 12:00:00 -0800";...*more things after this*

After the return, another NSLog shows that contents are intact.

When I refer to this instance later, I can see this in the debugger:

<SearchTerms: 0x5c28820> 
(entity: SearchTerms; id: 0x5a6df90 
<x-coredata://3936E19F-C0D0-4587-95B6-AA420F75BF78/SearchTerms/p33> ; 
data: <fault>)

and that's all.

So I believe that the object is retained (I explicitly retain it where it is returned). I have zombies on and it doesn't look like a zombie.

I have only one managedObjectContext in the program, maintained in the singleton.

So what is happening, and how do I get to the properties that were saved?


There is nothing wrong with your object and I think you might be misinterpreting the meaning of "fault" here.

From Apple's documentation:

"Faulting is a mechanism Core Data employs to reduce your application’s memory usage..."

Once you try and access any of the object's properties it will hit the database for all of the object's properties.

More details here http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFaultingUniquing.html


Faults are CoreData's way of having loose links to other entities. Just access the values via properties or valueGorKey and you will see them populated just in time.


I'm a little late getting back to this, but I found out that some steps in my program were out of order. Instead of deleting the database contents (something I do at startup every time, for now) and then creating and adding this entity, I had created and added the entity and then deleted the database contents.

The pointer to the favoritesCollection entity is held for the lifetime of the program, so I would have expected it be able to see its contents any time after it was created.

From the Core Data Programming Guide

Fault handling is transparent—you do not have to execute a fetch to realize a fault. If at some stage a persistent property of a fault object is accessed, then Core Data automatically retrieves the data for the object and initializes the object (see NSManagedObject Class Reference for a list of methods that do not cause faults to fire). This process is commonly referred to as firing the fault.

Core Data automatically fires faults when necessary (when a persistent property of a fault is accessed).

From what I can tell by reading the programming guide, seeing faults on relationships (links to other entities) is normal when looking at any particular entity. But seeing faults on the persistent property values is not mentioned. I believe that, in general, if the object is in memory, then its properties should not be faulted, but its relationships may be faulted.

The fact that the favoritesCollection entity was fully faulted (properties and relationships) and the fault did not get resolved revealed a problem. In this case it is consistent with the entity no longer existing in the database.

0

精彩评论

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

关注公众号