Skip to content

Commit feb67b4

Browse files
committed
improve use of KVO
1 parent 87e8f8e commit feb67b4

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

bk2ch15p660EmbeddedAVKit/EmbeddedAVKit/ViewController.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ViewController: UIViewController {
5959
}
6060
*/
6161

62-
av.addObserver(self, forKeyPath: "readyForDisplay", options: nil, context: nil)
62+
av.addObserver(self, forKeyPath: "readyForDisplay", options: .New, context: nil)
6363

6464
return; // just proving you can swap out the player
6565
delay(3) {
@@ -71,17 +71,19 @@ class ViewController: UIViewController {
7171

7272
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<()>) {
7373
if keyPath == "readyForDisplay" {
74-
dispatch_async(dispatch_get_main_queue(), {
75-
self.finishConstructingInterface()
76-
})
74+
if let obj = object as? AVPlayerViewController {
75+
if let ok = change[NSKeyValueChangeNewKey] as? Bool {
76+
if ok {
77+
dispatch_async(dispatch_get_main_queue(), {
78+
self.finishConstructingInterface(obj)
79+
})
80+
}
81+
}
82+
}
7783
}
7884
}
7985

80-
func finishConstructingInterface () {
81-
let vc = self.childViewControllers[0] as AVPlayerViewController
82-
if !vc.readyForDisplay {
83-
return
84-
}
86+
func finishConstructingInterface (vc:AVPlayerViewController) {
8587
println("finishing")
8688
vc.removeObserver(self, forKeyPath:"readyForDisplay")
8789
vc.view.hidden = false

0 commit comments

Comments
 (0)