2016-10-23 23 views
0

Android/Java開發相當新穎,並且使用開源的Parseplatform作爲我的後端服務器。我已經創建了一個類來管理一個解析對象,並且此對象根據此代碼將其數據從異步調用更新爲解析服務器。來自異步類的多重嵌套回調

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()); 
        } 
       } 
      }); 
     } 
    } 

所以我創建我的類對象在我的主要活動有以下幾點:

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

我可以不在身邊讓我的頭是怎麼在我的MainActivity我可以得到通知/回調繼續顯示userDevice類剛剛從解析服務器下載的信息。

我試圖創建一個接口,並添加監聽,因爲我已經看到了建議,但是我不能添加解析的完成函數內部的聽衆是什麼。

我的主要活動的定義,請注意我需要OnMapReadyCallback我使用谷歌地圖

public class MapMainActivity extends AppCompatActivity implements OnMapReadyCallback { 

因此,在總結,我想添加一些主要的活動,這樣我可以處理我數據從異步調用中添加到類中時的數據。

回答

1

這樣的事情,我建議使用事件總線。 Here is a link to a popular one I've had success with in the past.

基本上,你會有另一個班級,這將是你的公交車。你的活動將註冊一個特定的事件(你將創建,適當的子類化)。您的異步呼叫會告訴事件公共汽車啓動該事件,然後公共汽車會告訴所有訂戶,包括您的主要活動,事件發生。那是當你打電話給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()); 
       } 
      } 
     }); 
    } 
} 

您的MainActivity:

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*/ 
    }; 
} 
+0

哇那工作,那麼容易。直到今天,我從來沒有聽說過事件總線,它工作得很好。非常感謝! – BrentonGray88

+0

@ BrentonGray88當然! – kpsharp