简体   繁体   English

解析本地数据存储查询

[英]Parse Local Datastore Query

I'm trying to enable a cached result in my Android application which is using Parse Server by Back4App as a back-end. 我正在尝试在将Back4App使用Parse Server作为后端的Android应用程序中启用缓存的结果。

I have local datastore enabled but I cannot seem to query from network / cached results. 我启用了本地数据存储,但似乎无法从网络/缓存的结果中查询。

The following code is from my query 以下代码来自我的查询

        query.fromLocalDatastore().findInBackground()
                .continueWithTask((task) -> {
                    if (task.isFaulted() && task.getError() instanceof ParseException && ((ParseException) task.getError()).getCode() == ParseException.CACHE_MISS) {
                        return query.fromNetwork().findInBackground();
                    }
                    Log.d("Cache", "" + task.getResult().size());
                    return task;
                }, Task.UI_THREAD_EXECUTOR)
                .continueWithTask((task) -> {
                    // Update UI with results ...
                    Log.d("Network", "" + task.getResult().size());
                    ParseObject.pinAllInBackground(task.getResult());
                    return task;
                }, Task.UI_THREAD_EXECUTOR);

The logcat shows both Cache and Network with size of 0. Logcat会同时显示Cache和Network,大小均为0。

I have tried the following: 我尝试了以下方法:

        query.fromLocalDatastore().findInBackground()
                .continueWithTask((task) -> {
                    // Update UI with results from Local Datastore ...
                    ParseException error = (ParseException) task.getError();
                    if (error == null) {
                        Log.d("Cache", "" + task.getResult().size());
                    }
                    // Now query the network:
                    return query.fromNetwork().findInBackground();
                }, Task.UI_THREAD_EXECUTOR)
                .continueWithTask((task) -> {
                    // Update UI with results from Network ...
                    ParseException error = (ParseException) task.getError();
                    if (error == null) {
                        Log.d("Network", "" + task.getResult().size());
                    }
                    return task;
                }, Task.UI_THREAD_EXECUTOR);

which executes cache followed by network and it is working. 它先执行缓存,然后执行网络,然后开始运行。 But I want Cache else Network if error is not null. 但是我想如果错误不为null,则Cache其他Network

It seems that your objects have never been pinned so your local query always succeed returning 0 objects and it continues not pinning anything again. 似乎您的对象从未被固定过,因此您的本地查询总是成功返回0个对象,并且它继续不固定任何内容。 Try something like: 尝试类似:

            query.fromLocalDatastore().findInBackground()
                .continueWithTask((task) -> {
                    ParseException error = (ParseException) task.getError();
                    if (error != null || task.getResult().size() == 0) {
                        return query.fromNetwork().findInBackground();
                    }
                    Log.d("Cache", "" + task.getResult().size());
                    return task;
                }, Task.UI_THREAD_EXECUTOR)
                .continueWithTask((task) -> {
                    // Update UI with results ...
                    Log.d("Network", "" + task.getResult().size());
                    ParseException error = (ParseException) task.getError();
                    if (error == null) {
                        ParseObject.pinAllInBackground(task.getResult());
                    }
                    return task;
                }, Task.UI_THREAD_EXECUTOR);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM