Re: Core Data : Correct way to force reading property from sql store?
Re: Core Data : Correct way to force reading property from sql store?
- Subject: Re: Core Data : Correct way to force reading property from sql store?
- From: Jerry Krinock <email@hidden>
- Date: Mon, 16 Jan 2012 11:46:22 -0800
On 2012 Jan 15, at 09:53, Keary Suska wrote:
> I don't know that I have anything useful to say but the fact that you are trying to fault an object inside the object itself seems like a code smell. Or do you mean to instead do:
Sorry, Keary. I oversimplified wrongly. It's actually "self" and "self".. Let me post the working code here, again,
[[self managedObjectContext] processPendingChanges] ;
NSTimeInterval oldStaleness = [[self managedObjectContext] stalenessInterval] ;
[[self managedObjectContext] setStalenessInterval:0.0] ;
[[self managedObjectContext] refreshObject:self
mergeChanges:NO] ;
id foo = [self foo] ;
[[self managedObjectContext] setStalenessInterval:oldStaleness] ;
which, I suppose, is worse than you had thought, from a code smell perspective.
Faulting the object is the only way I could find to refresh its properties from the disk. This code appears in an instance method where I need the current value of foo from the disk in order to perform a task. It is, I think, the most logical place to put this code.
I think you are correct that the -refreshObject:mergeChanges:NO turns the object into a fault. But then the next line, [self foo], un-faults it. Fortunately, this action only occurs with one object, when the user clicks a button, so it's not a performance issue.
I agree this seems like emptying the swimming pool in order to retrieve an iPod that had fallen to the bottom. Apparently Apple thought so too, at least for "multiple objects", because in the documentation of the new 10.7 method that Hunger Hillegas pointed us to, "This provides more convenient way to ensure managed object property values are consistent with the store than by using refreshObject:mergeChanges: for multiple objects in turn."
For single objects, it's not that inconvenient, just a head-scratcher when you read the code. But my actual code has comments :)
On 2012 Jan 15, at 14:15, Steve Steinitz wrote:
> It is indeed that hard, possibly for a good reason, but let's not go into that. … You're on the right track.
OK, because I still need to work in 10.6, I'm going to leave it that way.
Thanks, all!
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden