Fix crash when tapping My Profile tab too quickly after app launch

This commit is contained in:
Shadowfacts 2020-10-19 18:41:38 -04:00
parent 744329dca2
commit 8a528936b8
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 31 additions and 12 deletions

View File

@ -14,11 +14,17 @@ class ProfileViewController: UIPageViewController {
weak var mastodonController: MastodonController! weak var mastodonController: MastodonController!
// todo: does this still need to be settable? // This property is optional because MyProfileViewController may not have the user's account ID
var accountID: String! { // 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 { didSet {
updateAccountUI()
pageControllers.forEach { $0.accountID = accountID } pageControllers.forEach { $0.accountID = accountID }
loadAccount()
} }
} }
@ -50,8 +56,10 @@ class ProfileViewController: UIPageViewController {
} }
deinit { deinit {
if let accountID = accountID {
mastodonController.persistentContainer.account(for: accountID)?.decrementReferenceCount() mastodonController.persistentContainer.account(for: accountID)?.decrementReferenceCount()
} }
}
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -84,8 +92,12 @@ class ProfileViewController: UIPageViewController {
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] (_) in self?.updateAccountUI() } .sink { [weak self] (_) in self?.updateAccountUI() }
loadAccount()
}
private func loadAccount() {
guard let accountID = accountID else { return }
if mastodonController.persistentContainer.account(for: accountID) != nil { if mastodonController.persistentContainer.account(for: accountID) != nil {
headerView.updateUI(for: accountID)
updateAccountUI() updateAccountUI()
} else { } else {
let req = Client.getAccount(id: accountID) let req = Client.getAccount(id: accountID)
@ -95,10 +107,6 @@ class ProfileViewController: UIPageViewController {
self.mastodonController.persistentContainer.addOrUpdate(account: account, incrementReferenceCount: true) { (account) in self.mastodonController.persistentContainer.addOrUpdate(account: account, incrementReferenceCount: true) { (account) in
DispatchQueue.main.async { DispatchQueue.main.async {
self.updateAccountUI() 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() { 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 navigationItem.title = account.displayNameWithoutCustomEmoji
pageControllers.forEach {
$0.updateUI(account: account)
}
} }
private func selectPage(at index: Int, animated: Bool, completion: ((Bool) -> Void)? = nil) { private func selectPage(at index: Int, animated: Bool, completion: ((Bool) -> Void)? = nil) {
@ -177,13 +194,15 @@ class ProfileViewController: UIPageViewController {
// MARK: Interaction // MARK: Interaction
@objc private func composeMentioning() { @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) compose(mentioningAcct: account.acct)
} }
} }
private func composeDirectMentioning() { 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) let draft = mastodonController.createDraft(mentioningAcct: account.acct)
draft.visibility = .direct draft.visibility = .direct
compose(editing: draft) compose(editing: draft)