Skip to content

Instantly share code, notes, and snippets.

@yaakov-h
Created March 15, 2015 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaakov-h/ad8ec4cb5361d3344aa8 to your computer and use it in GitHub Desktop.
Save yaakov-h/ad8ec4cb5361d3344aa8 to your computer and use it in GitHub Desktop.
Network Activity Indicator concurrent isolation
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