Refreshing view after api call in OnOpenUrl view modifier

I have this very basic piece of code in one of my Views:

 .onOpenURL(perform: { url in
                avm.handleRedirect(viewContext,url: url)        })

where avm is defined as

    @ObservedObject var avm = AccountsViewModel.makeAccountsViewModel()

avm has this basic property:

 @Published var isLoading = true

That when set, my view listens to and shows a loading spinner. This works in all other situations, except that outlined below.

The handleRedirect function looks as follows:

func handleRedirect(_ context: NSManagedObjectContext, url: URL) {

        debugPrint("Handling redirect")
        let url = URLComponents(string: url.absoluteString)!

        let code = url.queryItems?.first(where: { $0.name == "code" })?.value
        trueLayerClient.getAccessToken(code: code!) { res in

            if res == nil {
                debugPrint("accessTokenResponse was nil")
            }

            self.api.storeTokens(
                str: API.StoreTokensRequest(
                    userID: "xxx",
                    authToken: res!.accessToken,
                    refreshToken: res!.refreshToken,
                    apiKey: "xxx"
                ), finished: { success in
                    if success{
                       debugPrint("was successful")
                        DispatchQueue.main.async{
                        self.isLoading = false
                       }
                    } else {
                        debugPrint("store token failed")
                    }
                })
        }


    }

I'm triggering this onOpenUrl using a universal Link I have setup.

When I hit the link, my app opens from the background and I see the following logs:

Handling Redirect

Was successful

However, the app never goes into a loading state. Furthermore, the UI becomes "blocked" and I have to hard kill the app to be able to press anything.

Once I do reopen the app, the new state (which I fetch via api and store in CoreData) is reflected in the view.

At first I thought my API was responding too quickly, but I put a 5 second sleep in it and I still see the UI get blocked but with the same logs (just further apart)

I would appreciate any help on this.

Refreshing view after api call in OnOpenUrl view modifier
 
 
Q