简体   繁体   中英

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

I am executing code from a Service. The code works in the following way:

    // 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.

}

Here the method uploadJokes() is called.

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

Here items are found and uploadLocalFeedWithNewData() is called

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

Finally, the method fetchAndUploadJokesFromLocalDataStore(), which is the first method in this post is called again after saving successfully. Now the method should retrieve items from LocalDataStore, but it again retrieves zero objects.

Here is the Log:

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

And this is an infinite loop where ParseRequest.NETWORK_EXECUTOR-thread-3 calls detatch() is called again and again

I have found the answer!

The problem was that I was saving a Class named JokesDataCollection with the name JokesDataToBeUploaded. The new name was just an identifier used to identify the items present in LocalDataStore (for deletion, updating etc). Then the class name I used to query the Local DataStore was "JokesDataToBeUploaded", whereas the actual class fetched from internet and saved was named JokesDataCollection.

So I needed to call the following even to fetch items from LocalDatastore.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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