Wait till get my id and my freinds id facebook
<p>i want to wait till i get my user name and my id, and also to wait until i get the user names and users id of my freinds in facebook. how can i implement it?</p>

<p>i wrote a code after these two Request however sometimes one Request didnt finish and i get null in one of the variables (for example the userName variable)</p>

<p>therefor i want to wait till these two requests finish.</p>

<p>Or maybe there is another better implementation?</p>

<p>thanks alot</p>

<p>this is my code:</p>

<pre><code>    final CountDownLatch isForFinish = new CountDownLatch(1);

 private class SessionStatusCallback implements Session.StatusCallback {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if( session.isOpened() ){

                Request.executeMyFriendsRequestAsync(session, new Request.GraphUserListCallback() {

                    @Override
                    public void onCompleted(List&lt;GraphUser&gt; users, Response response) {


                        for (int i=0;i&lt;users.size();i++){

                            friendsId+= (users.get(i).getId()+",");
                            friendsName+=(users.get(i).getName()+",");

                        }

                        isForFinish.countDown();

                    }
                });


                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {

                        String userName = user.getName();
                        String userId = user.getId();


                        Intent i = new Intent(getApplicationContext(), TabMainActivity.class);
                        String email=null;
                        try {
                            email = (String) user.getInnerJSONObject().getString("email");
                        } catch (JSONException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        if(email!=null){

                            String newemail=new String(email);
                            newemail = email.replace("@", "_");
                            newemail = newemail.replace(".", "_");

                            TelephonyManager mTelephonyMgr;    
                             mTelephonyMgr = (TelephonyManager) getSystemService  
                               (Context.TELEPHONY_SERVICE);     

                             String phoneNumber = mTelephonyMgr.getLine1Number();  

                            String password = "facebook";

                            ParseUser Puser = new ParseUser();
                            Puser.setUsername(userId);
                            Puser.setPassword("facebook");
                            Puser.setEmail(email);
                            Puser.put("Name", userName);

                            try {
                                isForFinish.await();
                            } catch (InterruptedException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            Puser.put("friendsId",friendsId );
                            Puser.put("friendsName",friendsName );

                            try {
                                Puser.signUp();
                                ParseObject saleObj =new ParseObject("sale_"+idOfUser);
                                saleObj.saveInBackground();
                                ParseObject deliverObj =new ParseObject("deliver_"+idOfUser);
                                deliverObj.saveInBackground();
                                ParseObject group =new ParseObject("group_"+idOfUser);
                                group.saveInBackground();
                                ParseObject freind =new ParseObject("freind"+idOfUser);
                                freind.saveInBackground();
                            } catch (ParseException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                            i.putExtra("friendsId", friendsId);
                            i.putExtra("emailOwner", newemail);
                            i.putExtra("phone", phoneNumber);
                            i.putExtra("email",email  );
                            i.putExtra("password",password  );
                            i.putExtra("id",userId  );
                            i.putExtra("name",userName  );

                            startActivity(i);

                        }
                    }
                });
            }
</code></pre>
