Realm Query使用MYSLQ查询的结果

时间:2018-02-06 11:21:57

标签: android mysql realm

我需要创建一个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


                    }
                });
            }
        });

    }

1 个答案:

答案 0 :(得分:0)

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

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