来自异步的多个嵌套回调 Class

Multiple Nested Callbacks from Async Class

Android/Java 开发和使用开源 Parseplatform 作为我的后端服务器的新手。我创建了一个 class 来管理一个解析对象,这个对象根据这段代码从对解析服务器的异步调用中更新它的数据。

    public class DeviceObject {
       private String objectID, deviceName, status;
       private ParseGeoPoint location;
       int batLevel;

       public DeviceObject(){
          objectID = null;
          deviceName = null;
          location = null;
          batLevel = 0;
          status = null;
       }    
       public void getDeviceLatestData() {
        if (objectID != null) {
            ParseQuery<ParseObject> query = ParseQuery.getQuery("DeviceData");
            query.whereEqualTo("DeviceObjectID", objectID);
            query.orderByDescending("createdAt");
            query.setLimit(1);
            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> ParseDeviceList, ParseException e) {
                    if (e == null) {
                        if (ParseDeviceList.size() == 0) {
                            Log.d("debg", "Device not found");
                        } else {
                            for (ParseObject ParseDevice : ParseDeviceList) {
                                status = ParseDevice.getString("Status");
                                batLevel = ParseDevice.getInt("BatteryLevel");
                                location = ParseDevice.getParseGeoPoint("Location");
                                Log.d("debg", "Retrieving: " + deviceName);
                                Log.d("debg", "Status: " + status + " Battery: " + Integer.toString(batLevel));
                            }

                            //callback listener to add marker to map

                        }
                    } else {
                        Log.d("debg", "Error: " + e.getMessage());
                    }
                }
            });
        }
    }

所以我在 Main Activity 中创建了我的 class 对象,其中包含以下内容:

DeviceObject userDevice = new DeviceObject();
userDevice.getDeviceLatestData();

我无法理解的是如何在我的 MainActivity 中让 notified/callback 继续显示 userDevice class 刚刚完成解析的信息服务器.

我已经尝试按照我所看到的建议创建一个接口并添加一个监听器,但是我无法在解析的完成函数中添加监听器。

我的主要 activity 的定义是,注意我需要 OnMapReadyCallback 因为我正在使用 Google 地图

public class MapMainActivity extends AppCompatActivity implements OnMapReadyCallback {

所以总而言之,我想向主 activity 添加一些内容,以便在数据从异步调用添加到 class 时我可以处理数据。

对于这样的事情,我建议使用事件总线。 Here is a link to a popular one I've had success with in the past.

基本上,您将涉及另一辆 class,这将是您的巴士。您的 activity 将注册一个特定的事件(您将创建该事件,并根据需要进行子class)。您的异步调用将告诉事件总线触发该事件,然后总线将通知所有订阅者,包括您的主要 activity,该事件已触发。那就是你打电话给 getDeviceLatestData 的时候。以下是您可能会使用的简单代码片段,但请阅读该总线上的文档以完全理解它。

您的活动:

 public static class DataReady Event { /* optional properties */ }

您的设备对象:

 public class DeviceObject {
   private String objectID, deviceName, status;
   private ParseGeoPoint location;
   int batLevel;

   public DeviceObject(){
      objectID = null;
      deviceName = null;
      location = null;
      batLevel = 0;
      status = null;
   }    
   public void getDeviceLatestData() {
    if (objectID != null) {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("DeviceData");
        query.whereEqualTo("DeviceObjectID", objectID);
        query.orderByDescending("createdAt");
        query.setLimit(1);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> ParseDeviceList, ParseException e) {
                if (e == null) {
                    if (ParseDeviceList.size() == 0) {
                        Log.d("debg", "Device not found");
                    } else {
                        for (ParseObject ParseDevice : ParseDeviceList) {
                            status = ParseDevice.getString("Status");
                            batLevel = ParseDevice.getInt("BatteryLevel");
                            location = ParseDevice.getParseGeoPoint("Location");
                            Log.d("debg", "Retrieving: " + deviceName);
                            Log.d("debg", "Status: " + status + " Battery: " + Integer.toString(batLevel));
                        }

                        //callback listener to add marker to map
                        EventBus.getDefault().post(new DataReadyEvent());

                    }
                } else {
                    Log.d("debg", "Error: " + e.getMessage());
                }
            }
        });
    }
}

您的主要活动:

public class MainActivity {

   @Override
   public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) // Seems like you're updating UI, so use the main thread
    public void onDataReady(DataReadyEvent event) {
       /* Do whatever it is you need to do - remember you can add properties to your event and pull them off here if you need to*/
    };
}