As part of development for an application I’ve been writing, I’ve had numerous 
issues with my application’s document windows not being able to be opened more 
than once. I can open the document (with the usual ways, and through 
NSDocumentController’s openDocumentWithContentsOfURL: method, but it only works 
once. After that, future attempts to open the same document fail. No log output 
or error is produced.

Through the completionHandler: parameter of the method and its associated block 
parameter BOOL documentWasAlreadyOpen,I have deduced that the system believes 
that the document window is still open even after it has been closed. Here’s 
the associated code:

NSDocumentController *docControl = [NSDocumentController 
sharedDocumentController];
NSURL *selectedDocument = (NSURL *)[docControl recentDocumentURLs][clickedRow];
NSLog(@"Selected row %ld.", (long)clickedRow);
[docControl openDocumentWithContentsOfURL:selectedDocument display:YES 
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError 
*error) {
        NSLog(@"Was document %@ already open? %@", [document class], 
documentWasAlreadyOpen ? @"Yes" : @"No");
        if (error) {
                NSLog(@"Error: %@", error);
        }
}];

I’ve added some code to my NSDocument subclass to print code when a new 
instance is allocated. Here’s the code:

- (id)init {
    self = [super init];
    if (self) {
        // No initilization needed here.
#if DEBUG
        NSLog(@"Created new instance of Document. %@", self);
#endif
    }
    return self;
}

It also prints when the nib is loaded:

- (void)windowControllerDidLoadNib:(NSWindowController *)controller {
    NSLog(@"Window controller loaded nib. %@", window);
    ...
}

When this code is executed to open a document, the first time the document is 
opened, and it works, here’s the output (I’ve added code to my NSDocument 
subclass to print some debug statements):

2013-12-31 18:37:25.844 Mac Linux USB Loader[2740:303] Selected row 0.
2013-12-31 18:37:25.848 Mac Linux USB Loader[2740:303] Created new instance of 
Document. <Document: 0x600000149060>
2013-12-31 18:37:25.873 Mac Linux USB Loader[2740:303] Window controller loaded 
nib. <NSWindow: 0x6080001fba00>
2013-12-31 18:37:25.938 Mac Linux USB Loader[2740:303] Was document Document 
already open? No

But, after the document window has been closed with the standard close button 
(i.e the red “gumdrop"), and the file is to be opened again, here’s the output:

2013-12-31 18:40:30.631 Mac Linux USB Loader[2740:303] Selected row 0.
2013-12-31 18:40:30.634 Mac Linux USB Loader[2740:303] Was document Document 
already open? Yes

As you can see, no new instance has been created, and no new xib has been 
loaded. Here’s the window attributes of the Document window in the .xib file. 
Nothing hopefully very out of order. “Release when closed” is checked, so the 
Document window definitely still should not be open.



This was originally an Xcode 4; I’ve since upgraded to Xcode 5. Maybe it’s a 
bug in Xcode or the 10.9 SDK? I’m not entirely sure, considering no output is 
printed anywhere on the system that I know about.

I want to emphasize that opening a new document window fails regardless of 
whether it was opened using the code I provided or via the standard OS X 
facilities; however, I’m posting the code because it provides the best 
possibility of debugging.

Has anyone heard of this occurring before? Any help on this issue would be 
greatly appreciated. I can provide any additional info that is needed to point 
me in the right direction.

— SevenBits

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

_______________________________________________

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to