Re: Any way for a managed object to get at its document object?
Re: Any way for a managed object to get at its document object?
- Subject: Re: Any way for a managed object to get at its document object?
- From: Jim Thomason <email@hidden>
- Date: Thu, 7 Oct 2010 17:37:15 -0500
On Thu, Oct 7, 2010 at 5:14 PM, Rick Mann <email@hidden> wrote:
> I have an NSPersistentDocument-based app. I need to implement a "setNeedsFoo" method on the managed object that causes the receiver to be placed into an NSMutableSet on the document, for later "foo" processing. As part of being added to the set, it sets a timer in the document so that this set of objects gets processed.
>
> I don't see any obvious way of getting at the document. Any suggestions? Thanks!
I've used this little snippet in my code for years w/o issue. It's
always felt like an extreme hack, but I couldn't tell you a better way
to do it.
I created a superclass for all of my managed objects, which is just a
subclass of NSManagedObject. That class gets one extra method:
-(MyDocument*) document {
return [[NSApp delegate] documentForManagedObjectContext:[self
managedObjectContext]];
}
That's just a wrapper to a method defined on the delegate. Then in my
app delegate:
-(id) documentForManagedObjectContext:(NSManagedObjectContext*) context {
for (id doc in [[NSDocumentController sharedDocumentController] documents]) {
if ([doc managedObjectContext] == context) {
return doc;
}
}
return nil;
}
Obviously, that's Leopard only due to the fast enumeration, but you
can refactor it back to an NSEnumerator easy enough if you need Tiger
support.
-Jim.....
_______________________________________________
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