Displaying a window during NSDocumentSubclass initialization

42 views Asked by At

As file loading can be long (15-20s for large text files), I would like to display a waiting window with an undetermined NSProgressIndicator, to help the user be patient. I created a nib and a window controller for that. The problem is displaying this window on screen during recent file loading at application startup. In makeWindowControllers of NSDocumentSubclass, I put the following code:

...    
dispatch_async(dispatch_get_main_queue(), ^(){
        self->progressController = [[ProgressViewWindowController alloc] initWithWindowNibName:@"ProgressViewWindowController"];
        [self->progressController.progressIndicator setUsesThreadedAnimation:YES];
        [self->progressController.progressIndicator startAnimation:self];
        [self->progressController showWindow:self];
    });
...

And in the initForURL:(NSURL *)urlOrNil withContentsOfURL:... I put the following code at the end:

self.content = [[NSMutableString alloc] initWithContentsOfURL:self.fileURL encoding:encoder  error:&error];
dispatch_async(dispatch_get_main_queue(), ^(){
    [self->progressController close];
});

But the window is displayed only after the NSDocumentSubclass has finished loading and its content is displayed on the screen, not before. Thus, the close call is never made (I suppose that it is called before the window has been displayed). If I remove the dispatch_async… block, nothing is displayed at all, neither in time nor delayed.

How can I get this window being displayed when I ask it? Should I replace the synchronous [[NSMutableString alloc] initWithContentsOfURL:self.fileURL encoding:encoder error:&error]; method by an asynchronous one to free time on the main thread? How can I read a string in a file asynchronously? (I tested concurrent file read in returning YES to + (BOOL) canConcurrentlyReadDocumentsOfType:(NSString *)typeName but it doesn't change anything)

1

There are 1 answers

0
Denis On

If the same code is used in the Application delegate instead of the NSDocumentControllerSubclass, the window is correctly displayed.