readFromURL is the appropriate place to start populating the document.





________________________________
From: Jerry Krinock <[email protected]>
To: Cocoa Developers <[email protected]>
Sent: Tuesday, 17 February, 2009 12:49:44 PM
Subject: At what point during loading can NSPersistentDocument fetch?

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 protected])

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/idou747%40yahoo.com

This email sent to [email protected]



      Make Yahoo!7 your homepage and win a trip to the Quiksilver Pro. Find out 
more
_______________________________________________

Cocoa-dev mailing list ([email protected])

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]

Reply via email to