From 265eb08180612936076308a83f98160b54da7c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Roze=CC=81?= Date: Tue, 18 Jul 2017 18:01:20 +0200 Subject: [PATCH 1/2] set view properties a weak property to avoid retain cycle --- StatefulViewController/ViewStateMachine.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/StatefulViewController/ViewStateMachine.swift b/StatefulViewController/ViewStateMachine.swift index cd1dca9..b703f39 100644 --- a/StatefulViewController/ViewStateMachine.swift +++ b/StatefulViewController/ViewStateMachine.swift @@ -52,7 +52,7 @@ public class ViewStateMachine { }() /// The view that should act as the superview for any added views - public let view: UIView + public weak var view: UIView? /// The current display state of views public fileprivate(set) var currentState: ViewStateMachineState = .none @@ -163,6 +163,9 @@ public class ViewStateMachine { fileprivate func showView(forKey state: String, animated: Bool, completion: (() -> ())? = nil) { // Add the container view + guard let view = view else { + return + } containerView.frame = view.bounds view.addSubview(containerView) From d140cf7aeaf3a33782f4c12988e9ba2afa1a9e3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Roz=C3=A9?= Date: Wed, 2 Aug 2017 01:36:20 +0200 Subject: [PATCH 2/2] Don't use queue --- StatefulViewController/ViewStateMachine.swift | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/StatefulViewController/ViewStateMachine.swift b/StatefulViewController/ViewStateMachine.swift index b703f39..cb5f7cb 100644 --- a/StatefulViewController/ViewStateMachine.swift +++ b/StatefulViewController/ViewStateMachine.swift @@ -130,31 +130,18 @@ public class ViewStateMachine { public func transitionToState(_ state: ViewStateMachineState, animated: Bool = true, completion: (() -> ())? = nil) { lastState = state - queue.async { [weak self] in - guard let strongSelf = self else { return } + if state == currentState { + return + } - if state == strongSelf.currentState { - return - } - - // Suspend the queue, it will be resumed in the completion block - strongSelf.queue.suspend() - strongSelf.currentState = state - - let c: () -> () = { - strongSelf.queue.resume() - completion?() - } - - // Switch state and update the view - DispatchQueue.main.sync { - switch state { - case .none: - strongSelf.hideAllViews(animated: animated, completion: c) - case .view(let viewKey): - strongSelf.showView(forKey: viewKey, animated: animated, completion: c) - } - } + // Suspend the queue, it will be resumed in the completion block + currentState = state + + switch state { + case .none: + hideAllViews(animated: animated, completion: completion) + case .view(let viewKey): + showView(forKey: viewKey, animated: animated, completion: completion) } }