Fix crash when showing profile for uncached account

This commit is contained in:
Shadowfacts 2020-08-15 18:00:18 -04:00
parent 29aed65b99
commit eef9b96a1a
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 27 additions and 4 deletions

View File

@ -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 {

View File

@ -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() {