代码之家  ›  专栏  ›  技术社区  ›  Program-Me-Rev

如何等待方法调用完成后再继续

  •  0
  • Program-Me-Rev  · 技术社区  · 6 年前

    我正在尝试等待方法调用完成,然后再继续,但目前为止我的尝试不起作用。

    public class SaveMyEntitiesToServerResolver {
        public void syncNewMyEntities() {
            if (jsonObject != null) {
                new MyAsyncJSONPostTaskService(mContext, postURL, jsonObject).myPostJSON(new IMyAsyncJSONPostTask() {
                    @Override
                    public void processFinishAsyncJSONPostTask(JSONObject jsonObject) {
                        for (int i = 0; i < jArr.length(); i++) {
                            JSONObject e;
                            try {
                                e = jArr.getJSONObject(i);
                                synchronized (this) {
                                    setMyEntityMetadataId(e.getJSONArray("_myEntityMetadataList"));
                                    this.wait();
                                    // Then do something else
                                }
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                });
            }
        }
    
        synchronized void setMyEntityMetadataId(JSONArray jsonArray) {
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject e;
                try {
                    e = jsonArray.getJSONObject(i);
                    Long localMetadataId = Long.parseLong(e.getString("localMetadataId"));
                    // Save localMetadataId to DB
    
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
    
                if (i == jsonArray.length() - 1) {
                    this.notifyAll();
                }
            }
    
        }
    }
    

    在继续之前,如何等待方法调用完成?

    0 回复  |  直到 6 年前