From 88e4f52b5d72501b8cfd41ad6a475988a9a3dddf Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 10 May 2020 14:41:07 -0400 Subject: [PATCH] Fix crash when adding account Adding a UserData.LocalAccountInfo with a nil username while the PreferencesView is on screen will cause a crash, since it triggers a Combine publish upon which the PreferencesView expects to be able to display the username of all accounts. --- .../Screens/Onboarding/OnboardingViewController.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Tusker/Screens/Onboarding/OnboardingViewController.swift b/Tusker/Screens/Onboarding/OnboardingViewController.swift index b94a9a4cde..fe13b67dd9 100644 --- a/Tusker/Screens/Onboarding/OnboardingViewController.swift +++ b/Tusker/Screens/Onboarding/OnboardingViewController.swift @@ -68,13 +68,16 @@ extension OnboardingViewController: InstanceSelectorTableViewControllerDelegate let authCode = item.value else { return } mastodonController.authorize(authorizationCode: authCode) { (accessToken) in - let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: nil, accessToken: accessToken) - mastodonController.accountInfo = accountInfo + // construct a temporary UserAccountInfo instance for the MastodonController to use to fetch it's own account + let tempAccountInfo = LocalData.UserAccountInfo(id: "temp", instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: nil, accessToken: accessToken) + mastodonController.accountInfo = tempAccountInfo mastodonController.getOwnAccount { (account) in - LocalData.shared.setUsername(for: accountInfo, username: account.username) - DispatchQueue.main.async { + // this needs to happen on the main thread because it publishes a new value for the ObservableObject + let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: account.username, accessToken: accessToken) + mastodonController.accountInfo = accountInfo + self.onboardingDelegate?.didFinishOnboarding(account: accountInfo) } }