Created
March 15, 2015 07:20
-
-
Save yaakov-h/ad8ec4cb5361d3344aa8 to your computer and use it in GitHub Desktop.
Network Activity Indicator concurrent isolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
public class NetworkOperation { | |
init(application: UIApplication) { | |
self.application = application; | |
} | |
let application : UIApplication | |
public func started() { | |
let numOperationsRunning = OSAtomicIncrement32(&HackyValueHolder.numNetworkingOperations) | |
if numOperationsRunning == 1 { | |
// We were the first operation to start, so we have control of activating the indicator | |
application.networkActivityIndicatorVisible = true | |
} | |
} | |
public func completed() { | |
let numOperationsRemaining = OSAtomicDecrement32(&HackyValueHolder.numNetworkingOperations) | |
if numOperationsRemaining == 0 { | |
application.networkActivityIndicatorVisible = false | |
} | |
} | |
struct HackyValueHolder { | |
static var numNetworkingOperations : Int32 = 0 | |
} | |
} | |
extension UIApplication { | |
public func createNetworkOperation() -> NetworkOperation { | |
return NetworkOperation(application: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment