TheUnexpected
TheUnexpected

Reputation: 3165

Why do I receive the "ParseObject has no data for this key" error when pinning to localDatastore?

Using the following query I want to get a list of ParseUsers containing only a subset of columns, defined into the queryColumns list. I successfully receive the results, but when I try to pin them to the local datastore I receive the exception:

com.parse.ParseException: java.lang.IllegalStateException: ParseObject has no data for this key. Call fetchIfNeeded() to get the data.

I can't understand why... Should this message appear when "complex" columns like ParseObjects are involved? The data I'm querying is, instead, composed by Strings and Booleans only.

final List<String> queryColumns = Arrays.asList(
    "username",         // String
    "email",            // String
    "avatarCountry",    // String
    "avatarId",         // String
    "showCountry"       // Boolean
    "facebookId"        // String
);

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.selectKeys(queryColumns);
query.whereContainedIn("facebookId", fids);

query.findInBackground(new FindCallback<ParseUser>() {

    @Override
    public void done(List<ParseUser> result, ParseException ex) {

        if (ex == null) {
            // do stuff... then pin the results
            ParseUser.pinAllInBackground(CACHED_PLAYERS_LABEL, result, new SaveCallback() {
                @Override
                public void done(ParseException pinEx) {
                    // EXCEPTION HERE: java.lang.IllegalStateException: ParseObject has no data for this key.  Call fetchIfNeeded() to get the data.
                }
            });
        }
    }
});

Upvotes: 3

Views: 1351

Answers (1)

Oleg Osipenko
Oleg Osipenko

Reputation: 2430

Your exception's already containing an answer: Call fetchIfNeeded() to get the data. So change your code as following:

query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> result, ParseException e) {
            ParseObject.fetchAllIfNeededInBackground(result, new FindCallback<ParseUser>() {
                @Override
                public void done(List<ParseUser> list, ParseException ex) {
                    if (ex == null) {
                        ParseObject.pinAllInBackground(CACHED_PLAYERS_LABEL, result, new SaveCallback() {
                            @Override
                            public void done(ParseException e) {

                            }
                        });
                    }
                }
            });
        }
    });

Upvotes: 1

Related Questions