1

我试图从函数的嵌套类中返回一个变量,如下所示:

public int getPlan(int planID) {
 int planId;
 TableOperationCallback<Plans> callback = new TableOperationCallback<Plans>() {
          public void onCompleted(Plans plan, Exception exception,ServiceFilterResponse response) {
               if(exception == null){
                    int planId = (int) db.addPlan(plan); //Want to return this value
               } else {
                    Log.e(exception.getMessage(), "ERROR");
               }
          } 
     };
  mPlanTable.lookUp(planID, callback);
  return planId;
}

此方法与 Azure Android SDK 链接,并且异步调用 mPlanTable.lookUp()。因此,planId有时使用上面的代码返回 null。

返回实际值的最佳方法是什么?

4

1 回答 1

1

如果lookUp 是异步的,那么您永远不会返回它的结果。您需要重构代码,以便使用返回值的任何操作都在回调中完成,而不是在那里。

于 2013-04-14T23:30:41.670 回答