如何在Android UI线程中等待异步填充的对象而不阻塞它?

时间:2018-01-11 10:12:49

标签: android multithreading asynchronous android-asynctask

我有一个单身人员来处理实体Profilo(个人资料)的注册和取消。

通过传递标识符并以异步方式在服务器上收集信息来设置此实体。

我的问题是,当我必须返回我的profilo实例时,如果它仍未加载,它将返回null。

public class AccountHandler {
    private static AccountHandler istanza = null;

    Context context;
    private boolean logged;
    private Profilo profilo;


    private AccountHandler(Context context) {
        this.context = context;
        //initialization
        //setting logged properly
            assignField(this.getName());
        }
    }

    public static AccountHandler getAccountHandler(Context context) {
        if (istanza == null) {
            synchronized (AccountHandler.class) {
                if (istanza == null) {
                    istanza = new AccountHandler(context);
                }
            }
        }
        return istanza;
    }

    public void setAccount(String nickname, String accessingCode) {
        logged = true;
        assignField(nickname);
    }

    //other methods

    private void assignField(String nickname) {
        ProfiloClient profiloClient = new ProfiloClient();
        profiloClient.addParam(Profilo.FIELDS[0], nickname);
        profiloClient.get(new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode,
                                  Header[] headers,
                                  JSONArray response) {
                JSONObject objson = null;
                try {
                    objson = (JSONObject) response.getJSONObject(0);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                AccountHandler accountHandler = AccountHandler.getAccountHandler(context);
    // Profilo is created with a JSONObject
    // **setProfilo is called in async**
                **accountHandler.setProfilo(new Profilo(objson));**
            }

        });
    }


    private void setProfilo(Profilo profilo) {
        this.profilo = profilo;
    }



    public Profilo getProfilo() {
        if( logged && profilo == null)
            //How can I wait that profilo is loaded by the JsonHttpResponseHandler before to return it

        return this.profilo;
    }


}

2 个答案:

答案 0 :(得分:0)

您可以使用回调机制,而不是调用getProfiloAccountHandler类在加载配置文件时通知调用方。 e.g。

  public void setAccount(String nickname, String accessingCode, MyCallback cb) {
      assignField(nickname, cb);
  }
  private void assignField(String nickname, MyCallback cb) {
      ....
      accountHandler.setProfilo(new Profilo(objson));
      cb.onSuccess(this.profilo);
  }

MyCallback

中创建内部接口AccountHandler(重命名)
public class AccountHandler {
    public interface MyCallback {
        void onSuccess(Profilo profile);
    }
}

现在,无论何时拨打setAccount,您都会通过回调并在个人资料可用时收到通知,例如

accountHandler.setAccount("Test", "Test", new AccountHandler.MyCallback() {
        void onSuccess(Profilio profile) {
            // do something with the profile
        }
    }

答案 1 :(得分:0)

正如@Murat K.建议的那样,我在我的类中添加了一个接口,该接口将提供一个方法,以便在准备好使用时调用该对象。

   public class AccountHandler {

    public interface Callback {
            void profiloReady(Profilo profilo);
    }
}

此方法在GetProfilo中的一个Handler中调用,该函数对getProfilo进行递归调用,直到profilo准备好被使用,然后它调用回调方法,该类作为getProfilo的参数传递。

public void getProfilo(final Callback Callback) {
    if( logged && (profilo == null || !profilo.isReady() ) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getProfilo(Callback);
            }
        }, 500);
    }else
        Callback.profiloReady(profilo);
}

getProfilo调用示例

public class ProfiloCall implements AccountHandler.MyCallback {

        @Override
        public void profiloReady(Profilo profilo) {
            //Use profilo as needed
            //EXECUTED ONLY WHEN PROFILO IS READY
        }


        public void callerMethod() {
            //useful code
            accountHandler.getProfilo(this);
            //other useful code

        }
    }