gpt4 book ai didi

android - AsyncCallback 的优先级

转载 作者:行者123 更新时间:2023-11-29 19:38:32 24 4
gpt4 key购买 nike

我的申请中还有另一个问题。我在 MainActivity 中使用以下代码从我的 ContactAdapter 中获取用户数据:

private List<String> loadContactData() {
ContactAdapter db = new ContactAdapter(getApplicationContext());
// Spinner Drop down elements
List<String> contacts = db.getAllContacts();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, contacts);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d("SPINNER", String.valueOf(contacts));
return contacts;
}

和 ContactAdapter:

public List<String> getAllContacts(){
final List<String> contacts_id = new ArrayList<String>();
final List<String> contacts_name = new ArrayList<String>();
Backendless.Data.of(BackendlessUser.class).find(new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
public void handleResponse(BackendlessCollection<BackendlessUser> users) {
Iterator<BackendlessUser> userIterator = users.getCurrentPage().iterator();
while (userIterator.hasNext()) {
BackendlessUser user = userIterator.next();
String user_mail = user.getEmail();
String user_id = user.getUserId();
contacts_name.add(user_mail);
contacts_id.add(user_id);
}
Log.d("getAllContacts: ", String.valueOf(contacts_name)); }
public void handleFault(BackendlessFault backendlessFault) {
System.out.println("Server reported an error - " + backendlessFault.getMessage());}});
return contacts_name;
}

当然,我的问题是 Backendless 查询速度很慢...所以在我的 Logcat 中,我首先看到一个空的“SPINNER”和第二个“getAllContacts”,其中包含来自服务器的用户数据。结果,来自 loadContactData() 的 return contacts 语句也为空。

如何实现 getAllContacts() 先完成以获取 loadContactData() 中的用户数据?

最佳答案

您的问题是 find 调用是异步的(这很好,意味着 handleResponse() 是在新线程上调用的)。所以当你的 return contacts_name 运行时, contacts_name 仍然是空的,因为你在 handleResponse() 中的代码还没有运行,因为新线程还在等待数据库的响应。

对数据库的调用很慢,因为它可能包含文件甚至网络 IO。永远不要在处理 View 的主线程上执行此操作,否则应用程序将不再响应,因为更新 UI(和绘制微调动画)的主线程正忙于等待数据库。通过使数据库请求异步,您的应用程序可以保持响应,并且 UI 不会“卡住”。

为了简单起见,您可以这样做:

public class MyActivity extends AppCompatActivity {

private List<String> mContactIds = new ArrayList<>();

private Spinner mSpinner;

private ProgressDialog mProgressDialog;

@Overrride
public void onCreate() {

this.setContentView(...);
this.mSpinner = ...;

this.showLoadingDialog();

Backendless.Data.of(BackendlessUser.class).find(new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
public void handleResponse(BackendlessCollection<BackendlessUser> users) {

List<String> mails = new ArrayList<>();
List<String> ids = new ArrayList<>();
Iterator<BackendlessUser> userIterator = users.getCurrentPage().iterator();
while (userIterator.hasNext()) {
BackendlessUser user = userIterator.next();
String mail = user.getEmail();
String id = user.getUserId();
mails.add(user_mail);
ids.add(user_id);
}

Log.d("getAllContacts: ", String.valueOf(contacts_name));
applyData(mails, ids);

}

public void handleFault(BackendlessFault backendlessFault) {
Log.e("error: ", "Something went wrong");

}
});
}

private void applyData(List<String> mails, List<String> ids) {
Log.d("getAllContacts: ", String.valueOf(contacts_name));
this.hideLoadingDialog();
this.mContactIds = ids;
this.mSpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mails);

}

private void showLoadingDialog() {
this.mLoadingDialog = new ProgressDialog(this, null, "Loading contacts...", true);
this.mLoadingDialog.dismiss();

}

private void hideLoadingDialog() {
if(this.mLoadingDialog != null) {
this.mLoadingDialog().dismiss();

}
}

private void showErrorDialog() {
this.hideLoadingDialog();
new AlertDialog.Builder(this)
.setMessage("Something went wrong, try again")
.setPositiveButton("Ok", null)
.show();

}
}

关于android - AsyncCallback 的优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38942005/

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