• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
At what point during loading can NSPersistentDocument fetch?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

At what point during loading can NSPersistentDocument fetch?


  • Subject: At what point during loading can NSPersistentDocument fetch?
  • From: Jerry Krinock <email@hidden>
  • Date: Mon, 16 Feb 2009 17:49:44 -0800

My NSPersistentDocument subclass contains a singular managed object which I'd like to make an observer immediately after the document is loaded. So I invoked my little fetcher method to get it during -init, after invoking super, but was surprised to find that the fetch returned empty at this point.

I found the same behavior in Apple's DepartmentAndEmployees [1] sample project by adding these two methods to the MyDocument implementation:

- (void)logDepartment {
    // -department "fetches" the singular 'department' [2]
    NSLog(@"   'Department': %p", [self department]) ;
}

- (id)init {
    self = [super init] ;

    NSLog(@"Fetch during init:") ;
    [self logDepartment] ;


NSLog(@"Fetched after delay") ; [self performSelector:@selector(logDepartment) withObject:nil afterDelay:0] ; return self ; }

Here's the console log I get upon opening an existing document file:

17:06:02.259 Fetch during init:
17:06:02.268    'Department': 0x0
17:06:02.269 Fetched after delay
17:06:02.388    'Department': 0x1dc730

Besides the fact that it's a kludge, the performSelector:withObject:afterDelay occurs too late for my purposes. So, I've got one fetch that's too early and one that's too late. I looked in the debugger's call stack but don't see any other intermediate method I could legally hook in to.

There are other ways I could set my observer, but I'm quite surprised that executing a fetch in an initialized NSPersistentDocument doesn't return a qualified object which is definitely in there, and understanding this might help me with other issues. Note that executeFetchRequest:error: is happy to return the incorrect answer of an empty array -- it does not set an NSError.

Can someone please explain at what point, during loading, an NSPersistentDocument becomes ready to fetch and return correct results?

Thanks as always,

Jerry Krinock


Note: In MyDocument.m, there is an implementation of - initWithType:error: which inserts a 'Department', but that method only runs for new documents and is not involved here. I'm trying to fetch a 'Department' from the store/file.


[1] http://developer.apple.com/samplecode/DepartmentAndEmployees/index.html

[2] The following method is "right out of the box" of MyDocument.m, reproduced here for convenience.

- (NSManagedObject *)department
{
    // Apparently, 'department' is cached as an ivar for efficiency.
    if (department != nil)
    {
        return department;
    }

    // The remainder of this method, executing a fetch, only executes
    // the first time this method is invoked on a document.

    NSManagedObjectContext *moc = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSError *fetchError = nil;
    NSArray *fetchResults;

@try
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Department"
inManagedObjectContext:moc];


[fetchRequest setEntity:entity];
fetchResults = [moc executeFetchRequest:fetchRequest error:&fetchError];
} @finally
{
[fetchRequest release];
}


if ((fetchResults != nil) && ([fetchResults count] == 1) && (fetchError == nil))
{
[self setDepartment:[fetchResults objectAtIndex:0]];
return department;
}


    if (fetchError != nil)
    {
        [self presentError:fetchError];
    }
    else {
        // should present custom error message...
    }
    return nil;
}


_______________________________________________

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


  • Follow-Ups:
    • Re: At what point during loading can NSPersistentDocument fetch?
      • From: Chris Idou <email@hidden>
  • Prev by Date: Re: NSFormatter not getting called
  • Next by Date: Re: At what point during loading can NSPersistentDocument fetch?
  • Previous by thread: Re: NSFormatter not getting called
  • Next by thread: Re: At what point during loading can NSPersistentDocument fetch?
  • Index(es):
    • Date
    • Thread