From 8a528936b8fd5f3c33d6cf7dba91f52d4a4d1bb8 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 19 Oct 2020 18:41:38 -0400 Subject: [PATCH] Fix crash when tapping My Profile tab too quickly after app launch --- .../Profile/ProfileViewController.swift | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/Tusker/Screens/Profile/ProfileViewController.swift b/Tusker/Screens/Profile/ProfileViewController.swift index 955895c4..58e41b0f 100644 --- a/Tusker/Screens/Profile/ProfileViewController.swift +++ b/Tusker/Screens/Profile/ProfileViewController.swift @@ -14,11 +14,17 @@ class ProfileViewController: UIPageViewController { weak var mastodonController: MastodonController! - // todo: does this still need to be settable? - var accountID: String! { + // This property is optional because MyProfileViewController may not have the user's account ID + // when first constructed. It should never be set to nil. + var accountID: String? { + willSet { + if newValue == nil { + fatalError("Do not set ProfileViewController.accountID to nil") + } + } didSet { - updateAccountUI() pageControllers.forEach { $0.accountID = accountID } + loadAccount() } } @@ -50,7 +56,9 @@ class ProfileViewController: UIPageViewController { } deinit { - mastodonController.persistentContainer.account(for: accountID)?.decrementReferenceCount() + if let accountID = accountID { + mastodonController.persistentContainer.account(for: accountID)?.decrementReferenceCount() + } } override func viewDidLoad() { @@ -84,8 +92,12 @@ class ProfileViewController: UIPageViewController { .receive(on: DispatchQueue.main) .sink { [weak self] (_) in self?.updateAccountUI() } + loadAccount() + } + + private func loadAccount() { + guard let accountID = accountID else { return } if mastodonController.persistentContainer.account(for: accountID) != nil { - headerView.updateUI(for: accountID) updateAccountUI() } else { let req = Client.getAccount(id: accountID) @@ -95,10 +107,6 @@ class ProfileViewController: UIPageViewController { self.mastodonController.persistentContainer.addOrUpdate(account: account, incrementReferenceCount: true) { (account) in DispatchQueue.main.async { self.updateAccountUI() - self.headerView.updateUI(for: self.accountID) - self.pageControllers.forEach { - $0.updateUI(account: account) - } } } } @@ -106,8 +114,17 @@ class ProfileViewController: UIPageViewController { } private func updateAccountUI() { - guard let account = mastodonController.persistentContainer.account(for: accountID) else { return } + guard let accountID = accountID, + let account = mastodonController.persistentContainer.account(for: accountID) else { + return + } + + // Optionally invoke updateUI on headerView because viewDidLoad may not have been called yet + headerView?.updateUI(for: accountID) navigationItem.title = account.displayNameWithoutCustomEmoji + pageControllers.forEach { + $0.updateUI(account: account) + } } private func selectPage(at index: Int, animated: Bool, completion: ((Bool) -> Void)? = nil) { @@ -177,13 +194,15 @@ class ProfileViewController: UIPageViewController { // MARK: Interaction @objc private func composeMentioning() { - if let account = mastodonController.persistentContainer.account(for: accountID) { + if let accountID = accountID, + let account = mastodonController.persistentContainer.account(for: accountID) { compose(mentioningAcct: account.acct) } } private func composeDirectMentioning() { - if let account = mastodonController.persistentContainer.account(for: accountID) { + if let accountID = accountID, + let account = mastodonController.persistentContainer.account(for: accountID) { let draft = mastodonController.createDraft(mentioningAcct: account.acct) draft.visibility = .direct compose(editing: draft)