由于这个问题,我很头疼。为什么我的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)
        })
}
...

预先感谢您的回答!

最佳答案

我可以通过两种方式查看如何防止SplashscreenViewController解除分配:

  • 如果未释放其windowrootViewController。请记住,应用程序保留键窗口。
  • start()函数中,Observable闭包保留了vc变量。此Observable可以保留在其他地方,无论是显式还是在DisposeBag中。
  • 关于swift - UIViewController不会取消初始化(MVVM +协调器),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50097291/

    10-10 17:55