簡體   English   中英

解析本地數據存儲查詢

[英]Parse Local Datastore Query

我正在嘗試在將Back4App使用Parse Server作為后端的Android應用程序中啟用緩存的結果。

我啟用了本地數據存儲,但似乎無法從網絡/緩存的結果中查詢。

以下代碼來自我的查詢

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

Logcat會同時顯示Cache和Network,大小均為0。

我嘗試了以下方法:

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

它先執行緩存,然后執行網絡,然后開始運行。 但是我想如果錯誤不為null,則Cache其他Network

似乎您的對象從未被固定過,因此您的本地查詢總是成功返回0個對象,並且它繼續不固定任何內容。 嘗試類似:

            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