Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram proceeds before Facebook Graph API responds -- How can I make it wait?
    text
    copied!<p>Here's what's happening:</p> <ul> <li>My program updates when there is a state change</li> <li>The handler for the state change needs to know the user's ID before proceeding; it calls findUserID()</li> <li>findUserID() requests the user's ID from the Facebook Graph API</li> <li>The handler for the state change proceeds before that value if found and therefor behaves incorrectly.</li> <li>A few seconds later, the trace (Log.d) displays that the user's ID has been found</li> </ul> <p>If I pause and then resume my program, the user ID is noticed and then my program behaves correctly; however, this should NOT need to be done. How do I make my program wait for the Facebook Graph API's response before proceeding?</p> <pre><code>protected void findUserID(Session session) { //userId = null; // Set initial // If session is open if (session != null &amp;&amp; session.isOpened()) { // Make an API call to get user data // and define a new callback to handle the response Request request = Request.newMeRequest(session, new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user, Response response) { Session session = Session.getActiveSession(); // If current session matches active session if (session == Session.getActiveSession()) { if (user != null) { userId = user.getId(); // set userId Log.d("internal", "UserId issss: " + userId); } // end if user != null } // end if session == Session.getActiveSession() } // end onCompleted }); // end request Request.executeBatchAsync(request); // Request does not execute unless you call this. Log.d("findUserID", "The value after request: " + userId); } // end session open check } </code></pre> <p>Any/all help is appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload