gpt4 book ai didi

android - 使用 MYSQL 查询结果进行 Realm 查询

转载 作者:行者123 更新时间:2023-11-29 17:58:01 26 4
gpt4 key购买 nike

我需要创建一个 Realm 查询(可能是异步的),以使用从 MYSQL 查询到在线数据库获取的 ID 从 Realm 数据库检索卡片的图像:问题是,要创建 Realm 查询,我需要编写 query.equalTo("name", "John"); 但我不知道用户有多少张卡,所以我无法使用那种类型的查询。我该如何解决这个问题?谢谢:)

我对在线数据库的互联网调用:

public void getCards() { //It is used when adapter is created, and it need to contain all cards to display

//Realm realm = Realm.getDefaultInstance();

String urlToGet = "myurl";

OkHttpClient client = new OkHttpClient();

RequestBody formBody = new FormBody.Builder()
.add("user_name", user_name)
.build();

Request request = new Request.Builder()
.url(urlToGet)
.post(formBody)
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}

@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}

responseCardInInventory = response.body().string();

handler.post(new Runnable() {
@Override
public void run() {

Realm realm = Realm.getDefaultInstance();
List<CardInInv> cardListInInv = new ArrayList<>();

Gson gson = new Gson();
CardInInventory civ = gson.fromJson(responseCardInInventory, CardInInventory.class);
cardListInInv = getGsonCards(civ);

RealmQuery<Card> query = realm.where(Card.class);


//TODO EXECUTE REALM QUERY
//TODO HERE I NEED TO USE REALM TO EXECUTE A QUERY USING cardListInInv


}
});
}
});

}

最佳答案

如果没有我的评论中指定的有关您的域的足够信息,我所能指定的就是正确的解决方案应该是什么样子。

public class MyActivity extends AppCompatActivity {

Executor executor = Executors.newSingleThreadedPool(); // bg thread

Realm realm;
RealmResults<Card> results;
RealmChangeListener<RealmResults<Card>> realmChangeListener = new RealmChangeListener<RealmResults<Card>>() {
public void onChange(RealmResults<Card> element) { // updates on Write
adapter.setData(element);
}
}

MyAdapter adapter;


public void onCreate(Bundle bundle) {
super.onCreate(bundle);
realm = Realm.getDefaultInstance();
results = realm.where(Card.class).equalTo("userId", user_name);
results.addChangeListener(realmChangeListener);

adapter = new MyAdapter();

String urlToGet = "myurl";

executor.post(new Runnable() { // TODO: cancellation
@Override
public void run() {

OkHttpClient client = ((CustomApplication)getApplication()).getOkHttpClient();

Gson gson = ((CustomApplication)getApplication()).getGson();

RequestBody formBody = new FormBody.Builder()
.add("user_name", user_name)
.build();

Request request = new Request.Builder()
.url(urlToGet)
.post(formBody)
.build();


Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
responseCardInInventory = response.body().string();

CardInInventory civ = gson.fromJson(responseCardInInventory, CardInInventory.class);
List<CardInInv> cardListInInv = getGsonCards(civ);

realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Card card = new Card();
for(CardInInv cardInInv: cardListInInv) {
// map inv to card
}
realm.insertOrUpdate(card);
}
});
}
});
}
}

@Override
public void onDestroy() {
super.onDestroy();
results.removeAllChangeListeners();
results = null;
realm.close();
}

关于android - 使用 MYSQL 查询结果进行 Realm 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48641856/

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