private void checkGetAccess(String key) { if (!isDataAvailable(key)) { throw new IllegalStateException( "ParseObject has no data for '" + key + "'. Call fetchIfNeeded() to get the data."); } }
/** * Gets whether the {@code ParseObject} specified key has been fetched. * This means the property can be accessed safely. * * @return {@code true} if the {@code ParseObject} key is new or has been fetched or refreshed. {@code false} * otherwise. */ public boolean isDataAvailable(@NonNull String key) { synchronized (mutex) { // Fallback to estimatedData to include dirty changes. return isDataAvailable() || state.availableKeys().contains(key) || estimatedData.containsKey(key); } }
/** * If this {@code ParseObject} has not been fetched (i.e. {@link #isDataAvailable()} returns {@code false}), * fetches this object with the data from the server in a background thread. This is preferable to * using {@link #fetchIfNeeded()}, unless your code is already running from a background thread. * * @return A {@link bolts.Task} that is resolved when fetch completes. */ public final <T extends ParseObject> Task<T> fetchIfNeededInBackground() { if (isDataAvailable()) { return Task.forResult((T) this); } return ParseUser.getCurrentSessionTokenAsync().onSuccessTask(new Continuation<String, Task<T>>() { @Override public Task<T> then(Task<String> task) { final String sessionToken = task.getResult(); return taskQueue.enqueue(new Continuation<Void, Task<T>>() { @Override public Task<T> then(Task<Void> toAwait) { if (isDataAvailable()) { return Task.forResult((T) ParseObject.this); } return fetchAsync(sessionToken, toAwait); } }); } }); }
@Test public void testCompletenessOfIncludedParseObject() throws JSONException { JSONObject json = new JSONObject(); json.put("__type", "Object"); json.put("className", "GameScore"); json.put("createdAt", "2015-06-22T21:23:41.733Z"); json.put("objectId", "TT1ZskATqS"); json.put("updatedAt", "2015-06-22T22:06:18.104Z"); JSONObject child = new JSONObject(); child.put("__type", "Object"); child.put("className", "GameScore"); child.put("createdAt", "2015-06-22T21:23:41.733Z"); child.put("objectId", "TT1ZskATqR"); child.put("updatedAt", "2015-06-22T22:06:18.104Z"); child.put("bar", "child bar"); JSONArray arr = new JSONArray("[\"foo.bar\"]"); json.put("foo", child); json.put("__selectedKeys", arr); ParseObject parentObject = (ParseObject) ParseDecoder.get().decode(json); assertFalse(parentObject.isDataAvailable()); assertTrue(parentObject.isDataAvailable("foo")); ParseObject childObject = parentObject.getParseObject("foo"); assertFalse(childObject.isDataAvailable()); assertTrue(childObject.isDataAvailable("bar")); }
@Test public void testCompleteness() throws JSONException { JSONObject json = new JSONObject(); json.put("__type", "Object"); json.put("className", "GameScore"); json.put("createdAt", "2015-06-22T21:23:41.733Z"); json.put("objectId", "TT1ZskATqS"); json.put("updatedAt", "2015-06-22T22:06:18.104Z"); json.put("foo", "foo"); json.put("bar", "bar"); ParseObject parseObject = (ParseObject) ParseDecoder.get().decode(json); assertTrue(parseObject.isDataAvailable()); JSONArray arr = new JSONArray("[\"foo\"]"); json.put("__selectedKeys", arr); parseObject = (ParseObject) ParseDecoder.get().decode(json); assertFalse(parseObject.isDataAvailable()); }
@Test public void testPointer() throws JSONException { JSONObject json = new JSONObject(); json.put("__type", "Pointer"); json.put("className", "GameScore"); json.put("objectId", "Ed1nuqPvc"); ParseObject pointerObject = (ParseObject) ParseDecoder.get().decode(json); assertNotNull(pointerObject); assertFalse(pointerObject.isDataAvailable()); }
String className = null; for (T object : objects) { if (onlyIfNeeded && object.isDataAvailable()) { continue;
@Override public Task<Void> then(Task<Void> task) { if (!object.isDataAvailable(KEY_ACL)) { return Task.forResult(null); } final ParseACL acl = object.getACL(false); if (acl == null) { return Task.forResult(null); } ParseUser user = acl.getUnresolvedUser(); if (user == null || !user.isCurrentUser()) { return Task.forResult(null); } return ParseUser.pinCurrentUserIfNeededAsync(user); } });
if (!object.isDataAvailable()) { throw new ParseException(ParseException.INVALID_NESTED_KEY, String.format("Bad key: %s", key));
final String key, final ParseObject object, final ParseSQLiteDatabase db) { if (object.getObjectId() != null && !object.isDataAvailable() && !object.hasChanges() && !object.hasOutstandingOperations()) { return Task.forResult(null);
assertFalse(object.isDirty()); assertEquals("bar", object.get("foo")); assertFalse(object.isDataAvailable("name"));