1

I am trying to wait until onCompleted() of my callback function of a Request is finished. However the method get() of the RequestAsyncTask always returns before this is the case. Therefore it comes to problems in the further statements of my program. The println output is always displayed after the database query and is not equal null. Here is my code:

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            // show authendicated ui
            Log.i(TAG, "Logged in...");
            try {
                Request.newMeRequest(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            MyApplication.userId = user.getId();
                            System.out.println("User id: " + MyApplication.userId);
                        }

                    }
                }).executeAsync().get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            boolean isNewUser = !MyApplication.dbConnection.isUidRegistered(MyApplication.userId);

This causes my database to look up for null as uid. How can I solve this problem?

1 Answers1

1

You should put this part of your code:

boolean isNewUser = !MyApplication.dbConnection.isUidRegistered(MyApplication.userId);

inside your Request callback:

 @Override
public void onCompleted(GraphUser user, Response response) {
    if (user != null) {
        MyApplication.userId = user.getId();
        System.out.println("User id: " + MyApplication.userId);

        boolean isNewUser = !MyApplication.dbConnection.isUidRegistered(MyApplication.userId);
        // here you should continue your work
    }    
}
Ultimo_m
  • 4,724
  • 4
  • 38
  • 60
  • Ok, that works for this situation. Thank you. So I am right that I should never use result data from a request after the request outside the callback handler? –  Jun 06 '14 at 21:38
  • Yes, because its an asynchronous call and you dont know when the result data will come, so thats the best way to do it.Accept my answer as correct if it works for you :) – Ultimo_m Jun 06 '14 at 21:44
  • Ok, I am just wondering since I call the get() method on the asynchronous request. I have just tested the result of the get() method and it contains the expected values. Although it seems that onCompleted method gets called after the asynchronous task had finished. –  Jun 06 '14 at 21:59
  • You are right. You shouldnt worry for the method get(), because it doesnt gives results about the data that you are interested in. – Ultimo_m Jun 06 '14 at 22:11
  • Thats not right. Till you answered the question I had casted the response to a GraphObject via getGraphObjectAs with GraphUser as class and had got the same result as in the onCompleted method. –  Jun 06 '14 at 22:21
  • All i can say is that this is the method that works best, you can do this even by using LoginButton to call asynchronous request inside on create, but i prefer calling it inside onSessionChanged method, the same way you have done it – Ultimo_m Jun 06 '14 at 22:28
  • I also have similar problem. But I cannot move all my rest work into the CALLBACK METHOD. So is there a way that the rest work block until the callback returns? – tainy Mar 09 '16 at 07:29
  • you can add all your code to a new method and call that method you have created when callback returns – Ultimo_m Mar 09 '16 at 10:29