UIViewController没有取消初始化(mvvm +协调员)

时间:2018-04-30 09:06:36

标签: swift mvvm rx-swift swinject coordinator-pattern

由于这个问题我很头疼。为什么我的SplashscreenViewController没有取消初始化?你们看到下面发布的代码中有任何潜在的保留周期吗? 我试图检查Malloc堆栈,但无法找到任何有用的信息。

final class SplashscreenViewController: UIViewController {
    var viewModel: SplashscreenViewModelType!

    private let animationStartScale: CGFloat = 0.75
    private let animationEndScale: CGFloat = 1.0
    private let animationDuration: TimeInterval = 0.4

    private let splashscreenDuration: TimeInterval = 1.5

    @IBOutlet private weak var logoImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        animateLogo(scale: animationStartScale)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        DispatchQueue.main.asyncAfter(deadline: .now() + splashscreenDuration, execute: { [weak self] in
            self?.viewModel.onFinish.onNext(())
        })
    }

    private func animateLogo(scale: CGFloat) {
        UIView.animate(withDuration: animationDuration, animations: {
            self.transformImage(scale: scale)
        }, completion: { _ in
            if scale == self.animationStartScale {
                self.animateLogo(scale: self.animationEndScale)
            } else {
                self.animateLogo(scale: self.animationStartScale)
            }
        })
    }

    private func transformImage(scale: CGFloat) {
         logoImageView.transform = CGAffineTransform(scaleX: scale, y: scale)
    }
}

拥有SplaschreenViewController实例的类是它的协调器。以下代码:

final class SplashscreenCoordinator: BaseCoordinator {
    private let window: UIWindow

    init(window: UIWindow) {
        self.window = window
    }

    override func start() -> Observable<Void> {
        let vc: SplashscreenViewController = SwinjectStoryboard.instantiateInitialViewController()
        window.rootViewController = vc
        window.makeKeyAndVisible()

        let onFinish = vc.viewModel.onFinish
            .asObservable()
            .flatMap { [unowned self] _ -> Observable<Void> in
                guard self.window.rootViewController == vc else {
                    return Observable<Void>.empty()
                }
                if UserPreferencesManager.isLoggedIn() {
                    return self.startAppForLoggedInUser()
                } else {
                    return self.startAppForAnonymousUser()
                }
            }

        return onFinish
    }

    private func startAppForLoggedInUser() -> Observable<Void> {
        let tabBar = TabBarCoordinator(window: window)
        return coordinate(to: tabBar)
    }

    private func startAppForAnonymousUser() -> Observable<Void> {
        let startCoordinator = StartCoordinator(window: window)
        return coordinate(to: startCoordinator)
    }
}

BaseCoordinator的一些代码:

...
private func store(coordinator: BaseCoordinator) {
    childCoordinators[coordinator.identifier] = coordinator
}

private func free(coordinator: BaseCoordinator) {
    childCoordinators[coordinator.identifier] = nil
}

func coordinate(to coordinator: BaseCoordinator) -> Observable<Void> {
    store(coordinator: coordinator)
    return coordinator.start()
        .do(onNext: { [weak self] _ in
            self?.free(coordinator: coordinator)
        })
}
...

提前感谢您的回答!

1 个答案:

答案 0 :(得分:1)

我可以看到SplashscreenViewController如何防止释放的两种方式如下:

  1. 如果window的{​​{1}}未被解除分配。请记住,应用程序会保留关键窗口。
  2. rootViewController函数中,start()闭包保留Observable变量。此vc可能会在其他地方保留,无论是明确还是Observable