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: Rick Mann <email@hidden>
- Date: Thu, 7 Oct 2010 15:41:08 -0700
Thanks. I ended up using an NSMapTable to map MOCs to documents, conceivably faster when there are many documents open (although that won't be the typical case). Also, it's not actually the document I want, so this saves me going through the document, but it's the same basic approach.
In practice, I may have to call this many times during app execution, so it might become a bottleneck. We'll see.
(I worry that I have a design flaw if I need to do this.)
Thanks!
--
Rick
On Oct 7, 2010, at 15:37:15, Jim Thomason wrote:
> 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