From eef9b96a1a627d5b13d5fed10729481ff8f6f9d3 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 15 Aug 2020 18:00:18 -0400 Subject: [PATCH] Fix crash when showing profile for uncached account --- .../ProfileStatusesViewController.swift | 8 ++++--- .../Profile/ProfileViewController.swift | 23 ++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Tusker/Screens/Profile/ProfileStatusesViewController.swift b/Tusker/Screens/Profile/ProfileStatusesViewController.swift index 24d48d4d..9c3d84ca 100644 --- a/Tusker/Screens/Profile/ProfileStatusesViewController.swift +++ b/Tusker/Screens/Profile/ProfileStatusesViewController.swift @@ -25,7 +25,7 @@ class ProfileStatusesViewController: EnhancedTableViewController { private var older: RequestRange? private var newer: RequestRange? - private var loaded = false + var loaded = false init(accountID: String?, kind: Kind, mastodonController: MastodonController) { self.accountID = accountID @@ -61,12 +61,14 @@ class ProfileStatusesViewController: EnhancedTableViewController { if !loaded, let accountID = accountID, let account = mastodonController.persistentContainer.account(for: accountID) { - loaded = true updateUI(account: account) } } - private func updateUI(account: AccountMO) { + func updateUI(account: AccountMO) { + guard !loaded else { return } + loaded = true + if kind == .statuses { getPinnedStatuses { (response) in guard case let .success(statuses, _) = response else { diff --git a/Tusker/Screens/Profile/ProfileViewController.swift b/Tusker/Screens/Profile/ProfileViewController.swift index 545984ae..512863b2 100644 --- a/Tusker/Screens/Profile/ProfileViewController.swift +++ b/Tusker/Screens/Profile/ProfileViewController.swift @@ -48,6 +48,10 @@ class ProfileViewController: UIPageViewController { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } + + deinit { + mastodonController.persistentContainer.account(for: accountID)?.decrementReferenceCount() + } override func viewDidLoad() { super.viewDidLoad() @@ -56,7 +60,6 @@ class ProfileViewController: UIPageViewController { headerView = ProfileHeaderView.create() headerView.delegate = self - headerView.updateUI(for: accountID) selectPage(at: 0, animated: false) @@ -70,6 +73,24 @@ class ProfileViewController: UIPageViewController { .filter { [weak self] in $0 == self?.accountID } .receive(on: DispatchQueue.main) .sink { [weak self] (_) in self?.updateAccountUI() } + + if mastodonController.persistentContainer.account(for: accountID) != nil { + headerView.updateUI(for: accountID) + } else { + let req = Client.getAccount(id: accountID) + mastodonController.run(req) { [weak self] (response) in + guard let self = self else { return } + guard case let .success(account, _) = response else { fatalError() } + self.mastodonController.persistentContainer.addOrUpdate(account: account, incrementReferenceCount: true) { (account) in + DispatchQueue.main.async { + self.headerView.updateUI(for: self.accountID) + self.pageControllers.forEach { + $0.updateUI(account: account) + } + } + } + } + } } private func updateAccountUI() {