From 0705986b5acb7040481e71429a5a6f9d8b408c26 Mon Sep 17 00:00:00 2001 From: Tate Johnson Date: Wed, 22 Jul 2015 19:59:45 +1000 Subject: [PATCH] Client#fetch should also use a weak reference. --- Source/Client.swift | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Source/Client.swift b/Source/Client.swift index 49b3e10..35dfbb2 100644 --- a/Source/Client.swift +++ b/Source/Client.swift @@ -23,22 +23,24 @@ public class Client { } public func fetch(completionHandler: ((error: NSError?) -> Void)? = nil) { - session.lights(selector: "all") { [unowned self] (request, response, lights, error) in - if error != nil { - completionHandler?(error: error) - return - } + session.lights(selector: "all") { [weak self] (request, response, lights, error) in + if let strongSelf = self { + if error != nil { + completionHandler?(error: error) + return + } - let oldLights = self.lights - let newLights = lights - if oldLights != newLights { - self.lights = newLights - for observer in self.observers { - observer.lightsDidUpdateHandler(lights: lights) + let oldLights = strongSelf.lights + let newLights = lights + if oldLights != newLights { + strongSelf.lights = newLights + for observer in strongSelf.observers { + observer.lightsDidUpdateHandler(lights: lights) + } } - } - completionHandler?(error: nil) + completionHandler?(error: nil) + } } }