<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Charles,<div class=""><br class=""></div><div class="">I see Rob already pointed this out, but I’ll respond as well since I own NSProgress and I also was involved in the WWDC session he references.</div><div class=""><br class=""></div><div class="">NSProgress actually received a significant upgrade in iOS 9 and OS X 10.11 to address most of your concerns. I’ve never really liked the ‘current’ progress idea either, so I’ve changed Foundation to make using explicit progress objects possible (and encouraged). There are still cases where the idea of having a current progress is useful. For example, when there is no opportunity to grab a progress object, because the method you are calling is a proxy, or a global function, etc.).</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 17, 2016, at 6:14 PM, Charles Srstka via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Introduction:<br class=""><br class="">This is a proposal for a native progress-tracking mechanism in Swift.<br class=""><br class="">Motivation:<br class=""><br class="">As most of us know, Foundation includes NSProgress, a class that makes it fairly simple to implement progress tracking in complex applications. What makes NSProgress nice is the way that it builds a tree of progress objects, so that each method or function only needs to take into account the work that needs to be done in that specific context, and the parent NSProgress object can interpret that progress in the context of its own work to be done. NSProgress also provides a support for common issues like checking whether the user has cancelled an operation. However, there are a few issues with NSProgress:<br class=""><br class="">1. Because NSProgress relies on a thread-local global variable to store the current progress object, it is impossible to know, outside of documentation, whether any particular method supports NSProgress or not, and if the documentation is inadequate, one must resort to trial-and-error to determine whether NSProgress is supported (this has been noted before: <a href="http://oleb.net/blog/2014/03/nsprogress/" class="">http://oleb.net/blog/2014/03/nsprogress/</a>).<br class=""></div></div></blockquote><div><br class=""></div><div><br class=""></div><div>You can now create progress trees explicitly. There are two parts to this:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures" class="">@available</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">OSX</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10.11</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, *)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">/*not inherited*/</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(totalUnitCount unitCount: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int64</span><span style="font-variant-ligatures: no-common-ligatures" class="">, parent: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSProgress</span><span style="font-variant-ligatures: no-common-ligatures" class="">, pendingUnitCount portionOfParentTotalUnitCount: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int64</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">and a new protocol:</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> NSProgressReporting : </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSObjectProtocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">@available</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">OSX</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10.9</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, *)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> progress: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSProgress</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">So you can mark classes that publish progress with the protocol, and clients can create their trees using the new initializer.</span></div></span></div></div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">2. NSProgress relies on a paired register-deregister pattern in order to obtain and release "current" status. One must balance every becomeCurrentWithPendingUnitCount() call with a resignCurrent() call, or Bad Things™ will happen. However, this pattern does not work well with Swift's error handling mechanism:<br class=""><br class="">progress.becomeCurrentWithPendingUnitCount(1)<br class="">try self.doSomeThingThatMightThrow()<br class="">progress.resignCurrent() // If an error occurs, this will never be called!<br class=""></div></div></blockquote><div><br class=""></div><div>Yes, agreed. I would encourage the discrete progress if possible, but I’ve had lots of requests for a block-based version of becomeCurrent. This is another good reason to add one.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">3. It is very easy to accidentally add a child NSProgress object if one does not realize that a particular method supports NSProgress. A couple ways this can happen:<br class=""><br class="">func doSomething() {<br class=""> &nbsp;&nbsp;&nbsp;let progress = NSProgress(totalUnitCount: 10)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;progress.becomeCurrentWithPendingUnitCount(10)<br class=""> &nbsp;&nbsp;&nbsp;doSomethingThatSupportsNSProgress()<br class=""> &nbsp;&nbsp;&nbsp;doSomethingThatWeDontExpectSupportsNSProgressButDoes() // whoops, we just picked up a child accidentally<br class=""> &nbsp;&nbsp;&nbsp;progress.resignCurrent()<br class="">}<br class=""><br class="">func doSomething() {<br class=""> &nbsp;&nbsp;&nbsp;let progress = NSProgress(totalUnitCount: 10)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;progress.becomeCurrentWithPendingUnitCount(10)<br class=""> &nbsp;&nbsp;&nbsp;doSomethingThatSupportsNSProgress()<br class=""> &nbsp;&nbsp;&nbsp;progress.resignCurrent()<br class=""><br class=""> &nbsp;&nbsp;&nbsp;doSomethingThatWeDontExpectSupportsNSProgressButDoes() // whoops, this one just picked up our parent's NSProgress and made it our sibling<br class="">}<br class=""><br class="">This is particularly problematic when you consider the obvious workaround for the problem described in #2:<br class=""><br class="">func doSomething() {<br class=""> &nbsp;&nbsp;&nbsp;let progress = NSProgress(totalUnitCount: 10)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;progress.becomeCurrentWithPendingUnitCount(10)<br class=""> &nbsp;&nbsp;&nbsp;defer { progress.resignCurrent() }<br class=""><br class=""> &nbsp;&nbsp;&nbsp;doSomethingThatSupportsNSProgress()<br class=""> &nbsp;&nbsp;&nbsp;doSomethingThatWeDontExpectSupportsNSProgressButDoes() // whoops<br class="">}<br class=""><br class="">I haven't figured out exactly how to reproduce this reliably yet, but I have noticed that NSProgress objects can sometimes end up with a fractionCompleted larger than 1.0 when this occurs.<br class=""></div></div></blockquote><div><br class=""></div><div>I changed the algorithm for how we add child progress objects, so now we only will add the first one. This wasn’t strictly backwards compatible, but it was far too easy to add more progress objects than you intended.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">4. Because NSProgress uses a thread-local global variable for the current progress, one must be vigilant about jumping through specific hoops when spinning off a task in a new thread, or else the child task's NSProgress object will not be properly connected to its parent.<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>Yup, again another problem solved by using the discrete progress objects.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">5. NSProgress posts KVO notifications on the main thread. </div></div></blockquote><div><br class=""></div>This actually isn’t true. NSProgress posts KVO notifications on the thread in which the change was made.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class="">In addition to complicating the interface and causing undefined behavior if one mistakenly binds it to a UI element without inserting something in between to forward the KVO notifications to the main thread, this also hurts performance in the worker thread, since KVO notifications carry a non-trivial performance penalty, involving not only message sends but also packaging things into an NSDictionary for the "change" parameter. Notifications for multiple properties are fired every time completedUnitCount is changed, which then can cause similar KVO notifications to occur up the family tree, depending on how many KVO observers are installed. This can show up as a non-trivial performance cost in Instruments if the NSProgress object is updated frequently enough.<br class=""><br class=""></div></div></blockquote><div><br class=""></div><div>There is a cost associated with updating progress, it’s true. We can always do more to optimize it (and we continue to do so), but there comes a point at which the updater of the progress object has to add some intelligence of its own. This is why the documentation and release notes encourage callers to avoid doing things like updating the progress for each byte in a file (because that is far too many updates).</div><div><br class=""></div><div>- Tony</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">Proposed Solution:<br class=""><br class="">I propose a system that follows the example of Swift's excellent do/try/catch error-handling system. A "reports" keyword can be added to a function or method's declaration, similar to "throws":<br class=""><br class="">func myFunc() reports -&gt; () {<br class=""> &nbsp;&nbsp;&nbsp;...<br class="">}<br class=""><br class="">By default, the "reports" keyword will introduce a "let" constant named "progress" to the function's scope, much like "catch" implicitly adds "error". The name of this constant could be customized, if desired:<br class=""><br class="">func myFunc() reports(myNiftyProgressObject) {<br class=""> &nbsp;&nbsp;&nbsp;...<br class="">}<br class=""><br class="">The created progress object is automatically added as a child to the progress object which is implicitly passed into the function by the calling function or method.<br class=""><br class="">The interface to the progress object would be similar to NSProgress, with a "prepare" method taking the total unit count, as well as a "cancelled" property and a "cancel()" function helping keep track of whether or not an operation is cancelled. However, the "becomeCurrent" and "resignCurrent" methods are replaced with a "report" keyword which precedes a function or method call, similar to "try", although the "report" keyword includes a parameter taking the number of units corresponding to this operation:<br class=""><br class="">func myFunc() throws, reports {<br class=""> &nbsp;&nbsp;&nbsp;progress.prepare(10)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;report(5) foo()<br class=""><br class=""> &nbsp;&nbsp;&nbsp;if progress.cancelled { throw NSCocoaError.UserCancelledError }<br class=""><br class=""> &nbsp;&nbsp;&nbsp;report(5) try bar() // yes, "report" can be combined with "try"<br class="">}<br class=""><br class="">Spinning off an operation onto another thread or queue is no problem:<br class=""><br class="">func myFunc() reports {<br class=""> &nbsp;&nbsp;&nbsp;progress.prepare(10)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;report(10) foo() // the progress object is passed as a parameter to the function, so no need to specially make it current on this thread first<br class=""> &nbsp;&nbsp;&nbsp;}<br class="">}<br class=""><br class="">Unlike "try", "report" is not required. If we don't care about the progress of a method or function, we can call it without "report" and its progress will be ignored. In this example, the "someRelativelyTrivialOperationThatReports"'s progress object will *not* be added as a child to "progress":<br class=""><br class="">func myFunc() reports {<br class=""> &nbsp;&nbsp;&nbsp;progress.prepare(10)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;someRelativelyTrivialOperationThatReports()<br class=""> &nbsp;&nbsp;&nbsp;report(10) someNonTrivialOperationThatReports()<br class="">}<br class=""><br class="">If a function calls only one reporting function, the "prepare" method can be omitted and the parameter can be left off of "report", and the parent progress object will simply be passed through:<br class=""><br class="">func myFunc() reports {<br class=""> &nbsp;&nbsp;&nbsp;report doAllTheActualWork()<br class="">}<br class=""><br class="">To pass in a progress object other than the default progress object (for example, when creating the object at the very top of the tree), simply pass the progress object in as an optional second parameter to "reports":<br class=""><br class="">func startEverything() {<br class=""> &nbsp;&nbsp;&nbsp;let progress = Progress()<br class=""><br class=""> &nbsp;&nbsp;&nbsp;progress.prepare(10)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;report(10, progress) someFuncThatReports()<br class="">}<br class=""><br class="">In the function that does the actual work, of course the progress object has a "completed" property that tracks the completed units:<br class=""><br class="">func doTheActualWork() reports {<br class=""> &nbsp;&nbsp;&nbsp;let someArrayOfThings = ...<br class=""><br class=""> &nbsp;&nbsp;&nbsp;progress.prepare(someArrayOfThings.count)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;for eachThing in someArrayOfThings {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// do the work<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;progress.completed += 1<br class=""> &nbsp;&nbsp;&nbsp;}<br class="">}<br class=""><br class="">Observing the progress object uses a closure-based system, containing a "willUpdateHandler", a "didUpdateHandler", and a "cancelHandler":<br class=""><br class="">progress.didUpdateHandler = { (completed: Int, total: Int) -&gt; () in<br class=""> &nbsp;&nbsp;&nbsp;print("\(completed) units complete out of \(total)")<br class="">}<br class=""><br class="">progress.cancelHandler = {<br class=""> &nbsp;&nbsp;&nbsp;print("the user cancelled the operation")<br class="">}<br class=""><br class="">The separation between will- and did- handlers is to facilitate interoperability with NSProgressIndicator and other Objective-C objects; one could call willChangeValueForKey() and didChangeValueForKey() in the handlers to let the KVO system know about the changes if needed. If desired, a small wrapper could be added to Foundation to translate the progress object's notifications into KVO notifications.<br class=""><br class="">The progress object also includes a property specifying the dispatch queue on which the handlers will be run. By default this is the main queue, but this can be customized as needed:<br class=""><br class="">progress.dispatchQueue = myDispatchQueue<br class=""><br class="">Of course, any changes to the "completed" property in a child progress object would be bubbled up the family tree, causing any handlers on the parent progress objects to fire, and any changes to the "cancelled" property in a parent progress object would be propagated to its children, similar to how NSProgress currently works.<br class=""><br class="">If there is interest in this, I could flesh out the interface and implementation to the progress object a bit.<br class=""><br class="">What do you all think?<br class=""><br class="">Charles<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></body></html>