Re: Overridding -[NSPersistentDocument managedObjectContext]
On 2008 Oct, 27, at 12:32, Andy Lee wrote: I don't see that instance variable. I only see a private _managedObjectContext instance variable in the header, which you wouldn't have access to anyway. Am I missing something? Well, there NSPersistentDocument has -managedObjectContext and - setManagedContext: methods. So, I presume that there is an instance variable in there somewhere. Should he also override -setManagedObjectContext:? That's a whole'nother weird subject. Trying to get rid of super's moc, I tried this: NSPersistentDocument* doc = [[NSPersistentDocument alloc] init] ; NSLog(@"After initing, moc = %@", [[doc managedObjectContext] description]) ; [doc setManagedObjectContext:nil] ; NSLog(@"After nilling, moc = %@", [[doc managedObjectContext] description]) ; Here's what I got: After initing, moc = After nilling, moc = :{ My 2 cents: this seems like a brittle design -- it expects the overrider to know that the superclass never uses the ivar directly except in accessors, and it expects the implementer of the base class to remember never to use the ivar directly... Yes, the more I think about this, the less I like it. I'm going to step back here and try to avoid overriding that moc. In any case, I think the documentation could be clearer and I'll send feedback to that effect. Great! ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Overridding -[NSPersistentDocument managedObjectContext]
On Oct 27, 2008, at 12:40 PM, Andy Lee wrote: On Oct 27, 2008, at 3:25 PM, Ron Lue-Sang wrote: On Oct 27, 2008, at 10:49 AM, Jerry Krinock wrote: So it seems like I need to instead use my own instance variable, myMOC. Something like this should work: - (NSManagedObjectContext*)managedObjectContext { if (![self myMOC]) { NSManagedObjectContext* newMOC ; // // ... code to alloc, initialize and configure newMOC // [self setMyMOC:newMOC] ; [myMOC release] ; } return [self myMOC] ; } But it seems odd that now super's instance variable managedObjectContext will just sit there forever, pointing to nil. Is this the proper intended usage? Yes, that's the intended usage. Use your own storage or call super. Should he also override -setManagedObjectContext:? He may choose to. It may be sufficient for him to create a new context in init and call [self setManagedObjectContext:] and never have to override either accessor. If you're worried about some other context being set here, then yea. You'd need to override this and check/handle when the context isn't one of "yours". My 2 cents: this seems like a brittle design -- it expects the overrider to know that the superclass never uses the ivar directly except in accessors, and it expects the implementer of the base class to remember never to use the ivar directly. In any case, I think the documentation could be clearer and I'll send feedback to that effect. Yea, I see your point. Please file away. Thanks --Andy -- RONZILLA ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Overridding -[NSPersistentDocument managedObjectContext]
On Oct 27, 2008, at 3:25 PM, Ron Lue-Sang wrote: On Oct 27, 2008, at 10:49 AM, Jerry Krinock wrote: So it seems like I need to instead use my own instance variable, myMOC. Something like this should work: - (NSManagedObjectContext*)managedObjectContext { if (![self myMOC]) { NSManagedObjectContext* newMOC ; // // ... code to alloc, initialize and configure newMOC // [self setMyMOC:newMOC] ; [myMOC release] ; } return [self myMOC] ; } But it seems odd that now super's instance variable managedObjectContext will just sit there forever, pointing to nil. Is this the proper intended usage? Yes, that's the intended usage. Use your own storage or call super. Should he also override -setManagedObjectContext:? My 2 cents: this seems like a brittle design -- it expects the overrider to know that the superclass never uses the ivar directly except in accessors, and it expects the implementer of the base class to remember never to use the ivar directly. In any case, I think the documentation could be clearer and I'll send feedback to that effect. --Andy ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Overridding -[NSPersistentDocument managedObjectContext]
On Oct 27, 2008, at 3:12 PM, Jerry Krinock wrote: On 2008 Oct, 27, at 11:44, Andy Lee wrote: - (NSManagedObjectContext*)managedObjectContext { NSManagedObjectContext* moc = /* ... create your own MOC ... */; [self setManagedObjectContext:moc]; return moc; } Or alternatively, if your intent is to use the inherited behavior but do some additional stuff to the MOC: - (NSManagedObjectContext*)managedObjectContext { NSManagedObjectContext* moc = [super managedObjectContext]; // ... Do custom stuff to moc ... return moc; } Thanks, Andy. It looks like I would then be creating a new moc, or doing custom stuff to it, whenever this getter is invoked. No good. Ah, I see my first proposed solution doesn't actually work, because it *always* creates a new moc without checking whether it's done so already. But you can't use [super managedObjectContext] to check this, because it will use the inherited behavior to create its own moc. I'm still not sure whether or not it is appropriate to not use super's managedObjectContext instance variable, as proposed at the bottom of my original post. I don't see that instance variable. I only see a private _managedObjectContext instance variable in the header, which you wouldn't have access to anyway. Am I missing something? It might be worth sending feedback on the documentation for - [NSPersistentDocument managedObjectContext]. I agree it's unclear *how* you're supposed to override it. If they wanted to provide a way to override the creation, I think they should have factored it out into a separate method, perhaps -createManagedObjectContext, that would get called by -managedObjectContext. But they didn't. --Andy ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Overridding -[NSPersistentDocument managedObjectContext]
Hi Jerry, Scroll down… On Oct 27, 2008, at 10:49 AM, Jerry Krinock wrote: Documentation for -[NSPersistentDocument managedObjectContext] states: "If a managed object context for the receiver does not exist, one is created automatically. You override this method to customize the creation of the persistence stack." Well, the first sentence leaves me in a bit of quandary when I try and follow the second. Something like this won't work: - (NSManagedObjectContext*)managedObjectContext { if (![super managedObjectContext]) { // Create a new MOC... // Oops, sorry, this branch will never execute. } return [self managedObjectContext] ; } So it seems like I need to instead use my own instance variable, myMOC. Something like this should work: - (NSManagedObjectContext*)managedObjectContext { if (![self myMOC]) { NSManagedObjectContext* newMOC ; // // ... code to alloc, initialize and configure newMOC // [self setMyMOC:newMOC] ; [myMOC release] ; } return [self myMOC] ; } But it seems odd that now super's instance variable managedObjectContext will just sit there forever, pointing to nil. Is this the proper intended usage? Yes, that's the intended usage. Use your own storage or call super. -- RONZILLA ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Overridding -[NSPersistentDocument managedObjectContext]
On 2008 Oct, 27, at 11:44, Andy Lee wrote: - (NSManagedObjectContext*)managedObjectContext { NSManagedObjectContext* moc = /* ... create your own MOC ... */; [self setManagedObjectContext:moc]; return moc; } Or alternatively, if your intent is to use the inherited behavior but do some additional stuff to the MOC: - (NSManagedObjectContext*)managedObjectContext { NSManagedObjectContext* moc = [super managedObjectContext]; // ... Do custom stuff to moc ... return moc; } Thanks, Andy. It looks like I would then be creating a new moc, or doing custom stuff to it, whenever this getter is invoked. No good. I'm still not sure whether or not it is appropriate to not use super's managedObjectContext instance variable, as proposed at the bottom of my original post. Jerry ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: Overridding -[NSPersistentDocument managedObjectContext]
On Oct 27, 2008, at 1:49 PM, Jerry Krinock wrote: - (NSManagedObjectContext*)managedObjectContext { if (![super managedObjectContext]) { // Create a new MOC... // Oops, sorry, this branch will never execute. } return [self managedObjectContext] ; } Won't this lead to infinite recursion? And why are you calling [super managedObjectContext] if the intent is to create your own MOC? It looks like this should be something like: - (NSManagedObjectContext*)managedObjectContext { NSManagedObjectContext* moc = /* ... create your own MOC ... */; [self setManagedObjectContext:moc]; return moc; } Or alternatively, if your intent is to use the inherited behavior but do some additional stuff to the MOC: - (NSManagedObjectContext*)managedObjectContext { NSManagedObjectContext* moc = [super managedObjectContext]; // ... Do custom stuff to moc ... return moc; } [Disclaimer: I don't know Core Data, I'm just going by a quick look at the docs and a guess at your possible intentions.] --Andy ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]