Skip to content

Instantly share code, notes, and snippets.

@barbaramartina
Created April 8, 2016 14:56
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbaramartina/94b51cef9782fd5e37133d50c96dc87b to your computer and use it in GitHub Desktop.
Save barbaramartina/94b51cef9782fd5e37133d50c96dc87b to your computer and use it in GitHub Desktop.
How to create an observer of operations and queues in Swift. Which properties you can observe + docs links
//
// An Observer class intended to show how KVO works on operations and queues
//
// Created by Barbara Rodeker on 3/3/16.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//
import Foundation
class SimpleObserver: NSObject {
// You can get more information about the operation observing any of the following paths.
// https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperation_class/#//apple_ref/doc/uid/TP40004591-RH2-SW25
// cancelled / executing / finished / concurrent / asynchronous / ready / name
func observeOperation(op: NSOperation) {
op.addObserver(self, forKeyPath: "executing", options: .New, context: nil)
op.addObserver(self, forKeyPath: "cancelled", options: .New, context: nil)
op.addObserver(self, forKeyPath: "finished", options: .New, context: nil)
op.addObserver(self, forKeyPath: "concurrent", options: .New, context: nil)
op.addObserver(self, forKeyPath: "asynchronous", options: .New, context: nil)
op.addObserver(self, forKeyPath: "ready", options: .New, context: nil)
op.addObserver(self, forKeyPath: "name", options: .New, context: nil)
}
// These are the KVO compliant properties of a queue.
// https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperationQueue_class/#//apple_ref/doc/uid/TP40004592-RH2-SW19 Read: KVO-Compliant Properties
func observeQueue(queue: NSOperationQueue) {
queue.addObserver(self, forKeyPath: "operations", options: .New, context: nil)
queue.addObserver(self, forKeyPath: "operationCount", options: .New, context: nil)
queue.addObserver(self, forKeyPath: "maxConcurrentOperationCount", options: .New, context: nil)
queue.addObserver(self, forKeyPath: "suspended", options: .New, context: nil)
queue.addObserver(self, forKeyPath: "name", options: .New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
let key = keyPath!
switch key {
case "executing", "cancelled", "finished", "concurrent", "asynchronous", "ready", "name":
print(" PRINT SOMETHING ")
case "operations", "operationCount", "maxConcurrentOperationCount", "suspended", "name":
print(" PRINT SOMETHING DIFFERENT")
default:
return
}
}
}
@tewha
Copy link

tewha commented Mar 8, 2018

I'm not sure watching either asynchronous or concurrent makes sense.

@korantengNanaYaw
Copy link

korantengNanaYaw commented Jul 23, 2019

@tewha actually it makes sense as Apple has started in its own docs
check link here

@Sreekanth1995
Copy link

These observers leads to memory leaks in my app. Can you help me how to prevent memory leaks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment