gpt4 book ai didi

java - 来自异步类的多个嵌套回调

转载 作者:行者123 更新时间:2023-12-01 09:20:27 31 4
gpt4 key购买 nike

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

因此,我使用以下内容在主 Activity 中创建类对象:

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

我无法理解的是如何在我的 MainActivity 中收到通知/回调以继续显示 userDevice 类刚刚从解析服务器获取的信息。

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

我的主要 Activity 的定义是,请注意,我需要 OnMapReadyCallback,因为我正在使用 Google map

public class MapMainActivity extends AppCompatActivity implements OnMapReadyCallback {

总而言之,我想在主要 Activity 中添加一些内容,以便当数据从异步调用添加到类中时我可以处理数据。

最佳答案

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

基本上,您将涉及另一个类,这将是您的巴士。您的 Activity 将注册一个特定事件(您将创建该事件,并根据需要进行子类化)。您的异步调用将告诉事件总线触发该事件,然后总线将告诉所有订阅者(包括您的主要 Activity )该事件已触发。此时您将调用 getDeviceLatestData。以下是您可以使用的简单代码 fragment ,但请阅读该总线上的文档以完全理解它。

您的 Activity :

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

您的主要 Activity :

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

关于java - 来自异步类的多个嵌套回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40199549/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com