Skip to content

Instantly share code, notes, and snippets.

@jmnavarro
Created June 24, 2015 09:25
Show Gist options
  • Save jmnavarro/f324e2d1a0f1e15cc7f2 to your computer and use it in GitHub Desktop.
Save jmnavarro/f324e2d1a0f1e15cc7f2 to your computer and use it in GitHub Desktop.
Closes the UIActivityIndicator used by concurrent operations when the last one finishes
/*
* Handles the scenario when you have concurrent operations using
* the same activity indicator. Without care, this leads to stop the
* indicator when the first operation ends:
* Start A
* Start B
* Start C
* Finish B -> without this extensions, the indicator will be stopped here
* Finish C
* Finish A -> with this extensions, the indicator will be stopped here
*/
extension UIActivityIndicatorView {
private var startCount: Int {
get {
// since this is an extension, we cannot use stored properties
// use tag to store the value
return self.tag
}
set {
self.tag = newValue
}
}
public func startAnimatingConcurrent() {
objc_sync_enter(self)
if startCount == 0 {
self.startAnimating()
}
startCount++
objc_sync_exit(self)
}
public func stopAnimatingConcurrent() {
if startCount > 0 {
objc_sync_enter(self)
startCount--
if startCount == 0 {
self.stopAnimating()
}
objc_sync_exit(self)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment