在 Parse LocalDataStore 中保存数据但无法检索它。查看代码和日志

Saving data in Parse LocalDataStore but can't retrieve it. See Code and Logs

我正在执行服务中的代码。代码按以下方式工作:

    // check the LocalDataStore
    // if the localDataStore is empty then fetch new jokes and update the localDataStore
    // then try again from the first step.
    // otherwise, continue with further logic

private void fetchAndUploadJokesFromLocalDataStore() {

Log.v("uploading", "taking joke from the LocalDataStore");

    ParseQuery<ParseObject> query = ParseQuery.getQuery("JokesDataToBeUploaded");
    query.orderByDescending("objectId");
    query.fromLocalDatastore();
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e == null) {
                if (list.size() > 0) {
                    Log.v("uploading", "found " + list.size() + " jokes in LocalDataStore");
                } else {
                    Log.v("uploading", "localDataStore is empty, fetching new jokes");
                    SharedPreferences configData = getSharedPreferences("configData", 0);
                    uploadJokes(configData.getInt("uploadHowManyJokes", 1));
                    Log.v("Service", "uploadJokes called. uploadHowManyJokes=" + configData.getInt("uploadHowManyJokes", 1));
                }
            } else {

            }
        } ... // more methods follow but the brackets are closed correctly in real code.

}

这里调用了 uploadJokes() 方法。

public void uploadJokes(int numberOfJokes) {

    ParseQuery<ParseObject> query = ParseQuery.getQuery("JokesDataCollection");
    query.whereEqualTo("published", false);
    query.orderByDescending("objectId");
    query.setLimit(numberOfJokes); // limit to at most "n" results
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> feedObjectList,
                         ParseException e) {
            if (e == null) {
                Log.d("Uploading Joke", "Jokes Retrieved: " + feedObjectList.size());
                if (feedObjectList.size() > 0) {
                    updateLocalFeedWithNewData(feedObjectList);
                }
            } else {
                Log.d("Uploading Joke", "Error: " + e.getMessage());
                showNotification("", "");
                stopSelf();
            }
        }
    });
}

在这里找到项目并调用 uploadLocalFeedWithNewData()

private void updateLocalFeedWithNewData(final List<ParseObject> newFeed) {
    // Release any objects previously pinned for this query.
    final ParseObject feed = new ParseObject("JokesDataToBeUploaded");
    ParseObject.unpinAllInBackground("JokesDataToBeUploaded", new DeleteCallback() {
                public void done(ParseException e) {
                    if (e == null) {
                        // Cache the new results.
                        Log.v("pinning", "unpinned all");
                        ParseObject.pinAllInBackground("JokesDataToBeUploaded", newFeed, new SaveCallback() {
                            @Override
                            public void done(ParseException e) {
                                if (e == null) {
                                    Log.v("pinning", "pinned successfully");
   fetchAndUploadJokesFromLocalDataStore();
                                } else {
                                    Log.v("pinning", "pinning FAILED");
                                    // todo try again, either by restarting the service and getting entirely new jokes, or by trying again on the same object list
                                    stopSelf();
                                }
                            }
                        });
                    } else {
                        Log.v("pinning", "UNpinning FAILED");
                        Log.v("ParseException caught", e.getMessage());
                        stopSelf();
                    }
                }
            }
    );
}

最后,保存成功后再次调用本post中第一个方法fetchAndUploadJokesFromLocalDataStore()方法。现在该方法应该从 LocalDataStore 检索项目,但它再次检索零个对象。

这是日志:

10-30 22:00:09.429 4102-4102/com.appsbyusman.jokesmanager V/Service: Started
10-30 22:00:09.459 4102-4102/com.appsbyusman.jokesmanager V/uploading: taking joke from the LocalDataStore
10-30 22:00:09.519 4102-4102/com.appsbyusman.jokesmanager V/uploading: localDataStore is empty, fetching new jokes
10-30 22:00:09.529 4102-4102/com.appsbyusman.jokesmanager V/Service: uploadJokes called. uploadHowManyJokes=1
10-30 22:00:09.539 4102-6493/com.appsbyusman.jokesmanager I/System.out: Thread-12964(ApacheHTTPLog):Reading from variable values from setDefaultValuesToVariables
10-30 22:00:09.539 4102-6493/com.appsbyusman.jokesmanager I/System.out: Thread-12964(ApacheHTTPLog):isSBSettingEnabled false
10-30 22:00:09.539 4102-6493/com.appsbyusman.jokesmanager I/System.out: Thread-12964(ApacheHTTPLog):isShipBuild true
10-30 22:00:09.539 4102-6493/com.appsbyusman.jokesmanager I/System.out: Thread-12964(ApacheHTTPLog):SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
10-30 22:00:10.751 4102-6493/com.appsbyusman.jokesmanager I/System.out: ParseRequest.NETWORK_EXECUTOR-thread-1 calls detatch()
10-30 22:00:10.761 4102-4102/com.appsbyusman.jokesmanager D/Uploading Joke: Jokes Retrieved: 1
10-30 22:00:10.791 4102-4102/com.appsbyusman.jokesmanager V/pinning: unpinned all
10-30 22:00:10.831 4102-4102/com.appsbyusman.jokesmanager V/pinning: pinned successfully
10-30 22:00:20.841 4102-6525/com.appsbyusman.jokesmanager V/uploading: taking joke from the LocalDataStore
10-30 22:00:20.941 4102-4102/com.appsbyusman.jokesmanager V/uploading: localDataStore is empty, fetching new jokes
10-30 22:00:20.941 4102-4102/com.appsbyusman.jokesmanager V/Service: uploadJokes called. uploadHowManyJokes=1
10-30 22:00:21.321 4102-6809/com.appsbyusman.jokesmanager I/System.out: ParseRequest.NETWORK_EXECUTOR-thread-2 calls detatch()
10-30 22:00:21.351 4102-4102/com.appsbyusman.jokesmanager D/Uploading Joke: Jokes Retrieved: 1
10-30 22:00:21.521 4102-4102/com.appsbyusman.jokesmanager V/pinning: unpinned all
10-30 22:00:21.561 4102-4102/com.appsbyusman.jokesmanager V/pinning: pinned successfully
10-30 22:00:31.571 4102-6837/com.appsbyusman.jokesmanager V/uploading: taking joke from the LocalDataStore
10-30 22:00:31.681 4102-4102/com.appsbyusman.jokesmanager V/uploading: localDataStore is empty, fetching new jokes
10-30 22:00:31.681 4102-4102/com.appsbyusman.jokesmanager V/Service: uploadJokes called. uploadHowManyJokes=1
10-30 22:00:31.971 4102-6981/com.appsbyusman.jokesmanager I/System.out: ParseRequest.NETWORK_EXECUTOR-thread-3 calls detatch()
10-30 22:00:31.991 4102-4102/com.appsbyusman.jokesmanager D/Uploading Joke: Jokes Retrieved: 1
10-30 22:00:32.051 4102-4102/com.appsbyusman.jokesmanager V/pinning: unpinned all
10-30 22:00:32.132 4102-4102/com.appsbyusman.jokesmanager V/pinning: pinned successfully

这是一个无限循环,其中 ParseRequest.NETWORK_EXECUTOR-thread-3 调用 detatch() 被一次又一次调用

我找到答案了!

问题是我保存了一个名为 JokesDataToBeUploaded 的 Class JokesDataCollection。新名称只是一个标识符,用于识别 LocalDataStore 中存在的项目(用于删除、更新等)。然后我用来查询本地数据存储的 class 名称是 "JokesDataToBeUploaded",而从互联网上获取并保存的实际 class 名称是 JokesDataCollection。

所以我什至需要调用以下命令来从 LocalDatastore 中获取项目。

ParseQuery.getQuery("JokesDataCollection");                
query.fromLocalDatastore();