Sunday, January 25, 2015

An Example on How to Use NSProgress

Jaanus Kase (via Ole Begemann):

The best I can tell, there is no way to do this in Swift that does not look insane and ridiculous. See, -[NSWindowController initWithWindowNibName:] is a convenience initializer on NSWindowController, and Swift is much more restrictive about its initialization behavior in this case. You can’t call the parent’s convenience initializer from your own designated initializer.


Now, I was able to fix my demo project because it is a small, tightly controlled thing. I can be pretty sure that no other unwanted stuff is running after I fixed the above (first show window, and only then become the current progress object). But this might not be feasible for bigger projects where more things are happening at the same time and you don’t have tight control over all of them. Like, during the time where you are the current progress object, can you guarantee that nobody in your app is going to read anything from disk?


I suppose another solution is what Ole describes, to forgo the whole “current progress object” business and just pass the parent around between objects, and you can then become a child of that particular one and not just a child of whatever happens to be “current” at that point. This defeats the “loose coupling” aspect, though.

Too bad—it seemed like such a promising API. He says it’s also crashy.

2 Comments RSS · Twitter

My impression is that if you want to block transitively-called code from glomming on to your current NSProgress, you have to make a "sacrificial" instance, and make it current across the duration of the calling out. It's an "opt-out" mechanism, which is, at least the way I read it, kinda how it has to be if you want to retain the ability to include transitive callouts in other cases.

@Ian That makes sense as a general principle, but in this case I would have expected the NSData to create its own progress that was a child of the current progress. It doesn’t seem like it should be able to make the parent progress complete to “1”.

Leave a Comment