Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using RestClient within a task? #154

Closed
heath3n opened this issue Sep 20, 2020 · 3 comments
Closed

Using RestClient within a task? #154

heath3n opened this issue Sep 20, 2020 · 3 comments

Comments

@heath3n
Copy link

heath3n commented Sep 20, 2020

I'm using Firebase Auth to get an auth token; once I have it I need to set it as a header so I can make requests to my backend server. However because I'm putting RestClient within a task, I get the error Internal_CreateGameObject can only be called from the main thread.

_auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
    if (!task.IsCompleted) return;

    FirebaseUser user = task.Result;
    user.TokenAsync(false).ContinueWith(tokenTask => {
        if (!tokenTask.IsCompleted) return;
        
        RestClient.DefaultRequestHeaders["Authorization"] = "Bearer " + tokenTask.Result;
        RestClient.Post("https://...."), null);
    });
});

What is the correct approach here?

@jdnichollsc
Copy link
Member

Can you attach a repo to reproduce that issue?

@heath3n
Copy link
Author

heath3n commented Oct 12, 2020

@jdnichollsc Added one here, sorry for the delay: https://github.com/heath3n/RestClientTaskErrorRepro.

There should be a scene Scenes/ExampleScene which contains a single GameObject with the InitializeClient script attached; just run the project in Unity (I'm using 2019.4) and observe you get the described error in the console.

I've since discovered Firebase has an extension method ContinueWithOnMainThread that solves my issue so I'd guess anyone using RestClient and not using Firebase would just need to write a similar method for their own tasks. That doesn't seem like the responsibility of this library though so I'll close this.

@heath3n heath3n closed this as completed Oct 12, 2020
@jdnichollsc
Copy link
Member

Thanks for let me know mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants