Korbbit
Korbbit

Reputation: 937

android, facebook SDK 4. Do you need to log in every time? (turns out to be a threading issue)

I'm making an android app involving facebook login and graph requests. I was under the impression for a long time, that you can basically auto-login on app start like so:

accessToken = AccessToken.getCurrentAccessToken();
if(accessToken != null && !accessToken.isExpired())
{
    //make graph requests with access token
}

but I've realised my graph calls aren't completing(onComplete is never called, neither is any exception catcher. simply nothing happens). But when I do this instead, it works:

accessToken = AccessToken.getCurrentAccessToken();
if(accessToken != null && !accessToken.isExpired())
{
    LoginManager.getInstance().logInWithReadPermissions(...)
    //make graph requests with access token on login complete
}

But this way pauses the game for a few seconds and loads something(facebook app I'm guessing, but it never appears on screen). Then continues, and successfully makes graph requests.

So my question is this: Do you need to call LoginManager.logIn everytime the app is started to use graph requests? even if there is still a valid access token? And is it improper to call it unprompted by the user(on app start)?

UPDATE: It works if I use batchRequest.executeAndWait() instead of batchRequest.executeAsync() on my graph requests! but thats only on app start, if I click the log in button during the game i get this exception: android.os.NetworkOnMainThreadException

Upvotes: 0

Views: 58

Answers (1)

Korbbit
Korbbit

Reputation: 937

OK so it was a threading issue. It appears the graph request was being sent(executeAsync()) from a weird thread, probably because im using libgdx and I called it from within the game class blah blah I dont really know, but the way to fix it: wrap the android function like this:

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            doGameFacebook();
        }
    });

Upvotes: 1

Related Questions