Skip to content

Commit 22ae29c

Browse files
committed
Merge pull request #19 from mmatoszko/source
Added hint about NSProgressReporting
2 parents c1f7e49 + d4413d2 commit 22ae29c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: post
3+
title: "NSProgressReporting"
4+
author: matt
5+
date: 2015-08-09 20:00:52 +0200
6+
comments: false
7+
categories:
8+
---
9+
10+
In iOS7 and OS X 10.9 Apple released NSProgess. Its a nice, helpful piece of code that was supposed to make our coding lifes easier.
11+
If you finally [find a proper way to use it](http://oleb.net/blog/2014/03/nsprogress/) it can be very beneficial.
12+
13+
Besides of helpfull UserInfo object keys which give us comprehensive text information about progress of our tasks in proper language, NSProgress was supposed to provide us way of compositioning objects into trees. However, first version of class allowed this in an implicit way which does not look very clear first time you learn it.
14+
15+
I will try to present it to you in a short way:
16+
```swift
17+
let parentProgress = NSProgress()
18+
parentProgress.totalUnitCount = 2
19+
parentProgress.becomeCurrentWithPendingUnitCount(1)
20+
21+
let childProgress = NSProgress(totalUnitCount:1)
22+
parentProgress.resignCurrent()
23+
```
24+
When applying this approach you have to create child progress using totalUnitCount convenience constructor immediately. You also should document that you support implicit composition in a clear way.
25+
26+
OSX 10.11 and iOS 9.0 provides more explicit way for creating tree structure of NSProgress objects. Things are simple now:
27+
```swift
28+
let parentProgress = NSProgress()
29+
let childProgress = NSProgress()
30+
parentProgress.addChild(childProgress, withPendingUnitCount:1)
31+
```
32+
33+
There is also one more thing useful thing in process of forwarding progress through our app architecture. When any of your classes is free to attend in NSProgress family tree, simply implement following protocol:
34+
```swift
35+
protocol NSProgressReporting : NSObjectProtocol {
36+
var progress: NSProgress { get }
37+
}
38+
```
39+
This way we are able to easily track progress of tree structure of tasks and get our overall progress in an easy, object oriented way.

0 commit comments

Comments
 (0)