public Task<Boolean> isReadOnlyAsync() { synchronized (currentLock) { Task<Boolean> task = current.continueWith(new Continuation<Void, Boolean>() { @Override public Boolean then(Task<Void> task) { return db.isReadOnly(); } }); current = task.makeVoid(); return task; } }
@Override public Task<Void> then(Task<Void> task) { return store.fetchLocallyAsync(ParseObject.this).makeVoid(); } });
public Task<Boolean> isOpenAsync() { synchronized (currentLock) { Task<Boolean> task = current.continueWith(new Continuation<Void, Boolean>() { @Override public Boolean then(Task<Void> task) { return db.isOpen(); } }); current = task.makeVoid(); return task; } }
/** * Executes a DELETE. * * @see SQLiteDatabase#delete */ public Task<Void> deleteAsync(final String table, final String where, final String[] args) { synchronized (currentLock) { Task<Integer> task = current.onSuccess(new Continuation<Void, Integer>() { @Override public Integer then(Task<Void> task) { return db.delete(table, where, args); } }, dbExecutor); current = task.makeVoid(); return task.continueWithTask(new Continuation<Integer, Task<Integer>>() { @Override public Task<Integer> then(Task<Integer> task) { // We want to jump off the dbExecutor return task; } }, Task.BACKGROUND_EXECUTOR).makeVoid(); } }
/** * Executes an INSERT and throws on SQL errors. * * @see SQLiteDatabase#insertOrThrow */ public Task<Void> insertOrThrowAsync(final String table, final ContentValues values) { synchronized (currentLock) { Task<Long> task = current.onSuccess(new Continuation<Void, Long>() { @Override public Long then(Task<Void> task) { return db.insertOrThrow(table, null, values); } }, dbExecutor); current = task.makeVoid(); return task.continueWithTask(new Continuation<Long, Task<Long>>() { @Override public Task<Long> then(Task<Long> task) { // We want to jump off the dbExecutor return task; } }, Task.BACKGROUND_EXECUTOR).makeVoid(); } }
@Override public List<Task<Void>> deleteAllAsync( List<ParseObject.State> states, String sessionToken) { int batchSize = states.size(); List<ParseRESTObjectCommand> commands = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { ParseObject.State state = states.get(i); ParseRESTObjectCommand command = ParseRESTObjectCommand.deleteObjectCommand( state, sessionToken); commands.add(command); } final List<Task<JSONObject>> batchTasks = ParseRESTObjectBatchCommand.executeBatch(client, commands, sessionToken); List<Task<Void>> tasks = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { tasks.add(batchTasks.get(i).makeVoid()); } return tasks; } }
@Override public Task<Void> then(Task<Void> task) throws Exception { // Put the JSON in the database. String className = object.getClassName(); String objectId = object.getObjectId(); int isDeletingEventually = json.getInt(ParseObject.KEY_IS_DELETING_EVENTUALLY); final ContentValues values = new ContentValues(); values.put(OfflineSQLiteOpenHelper.KEY_CLASS_NAME, className); values.put(OfflineSQLiteOpenHelper.KEY_JSON, json.toString()); if (objectId != null) { values.put(OfflineSQLiteOpenHelper.KEY_OBJECT_ID, objectId); } values.put(OfflineSQLiteOpenHelper.KEY_IS_DELETING_EVENTUALLY, isDeletingEventually); String where = OfflineSQLiteOpenHelper.KEY_UUID + " = ?"; String[] args = {uuid}; return db.updateAsync(OfflineSQLiteOpenHelper.TABLE_OBJECTS, values, where, args).makeVoid(); } });
@Override public Task<Void> revokeAsync(String sessionToken) { return ParseRESTSessionCommand.revoke(sessionToken) .executeAsync(client) .makeVoid(); }
public Task<Void> trackAppOpenedInBackground(String pushHash, String sessionToken) { ParseRESTCommand command = ParseRESTAnalyticsCommand.trackAppOpenedCommand(pushHash, sessionToken); Task<JSONObject> eventuallyTask = eventuallyQueue.enqueueEventuallyAsync(command, null); return eventuallyTask.makeVoid(); } }
@Override public Task<Void> requestPasswordResetAsync(String email) { ParseRESTCommand command = ParseRESTUserCommand.resetPasswordResetCommand(email); return command.executeAsync(client).makeVoid(); } }
public Task<Void> trackEventInBackground(final String name, Map<String, String> dimensions, String sessionToken) { ParseRESTCommand command = ParseRESTAnalyticsCommand.trackEventCommand(name, dimensions, sessionToken); Task<JSONObject> eventuallyTask = eventuallyQueue.enqueueEventuallyAsync(command, null); return eventuallyTask.makeVoid(); }
@Override public Task<Void> then(Task<ParsePin> task) { if (task.isFaulted()) { return task.makeVoid(); } ParsePin pin = task.getResult(); return unpinAsync(pin, db); } });
@Override public Task<Void> deleteAsync(ParseObject.State state, String sessionToken) { ParseRESTObjectCommand command = ParseRESTObjectCommand.deleteObjectCommand( state, sessionToken); return command.executeAsync(client).makeVoid(); }
public Task<Void> sendInBackground(ParsePush.State state, String sessionToken) { return buildRESTSendPushCommand(state, sessionToken).executeAsync(restClient).makeVoid(); }
@Override public Task<Void> then(Task<Boolean> task) { boolean success = !task.isFaulted() && task.getResult(); if (!success) { return unlinkFromInBackground(authType); } return task.makeVoid(); } });
/** * Executes an UPDATE. * * @see SQLiteDatabase#update */ public Task<Integer> updateAsync(final String table, final ContentValues values, final String where, final String[] args) { synchronized (currentLock) { Task<Integer> task = current.onSuccess(new Continuation<Void, Integer>() { @Override public Integer then(Task<Void> task) { return db.update(table, values, where, args); } }, dbExecutor); current = task.makeVoid(); return task.continueWithTask(new Continuation<Integer, Task<Integer>>() { @Override public Task<Integer> then(Task<Integer> task) { // We want to jump off the dbExecutor return task; } }, Task.BACKGROUND_EXECUTOR); } }
@Override public Task<List<EventuallyPin>> then(Task<List<EventuallyPin>> task) { final List<EventuallyPin> pins = task.getResult(); List<Task<Void>> tasks = new ArrayList<>(); for (EventuallyPin pin : pins) { ParseObject object = pin.getObject(); if (object != null) { tasks.add(object.fetchFromLocalDatastoreAsync().makeVoid()); } } return Task.whenAll(tasks).continueWithTask(new Continuation<Void, Task<List<EventuallyPin>>>() { @Override public Task<List<EventuallyPin>> then(Task<Void> task) { return Task.forResult(pins); } }); } });
/* package for tests */ Task<Void> cleanUpAuthDataAsync() { ParseAuthenticationManager controller = getAuthenticationManager(); Map<String, Map<String, String>> authData; synchronized (mutex) { authData = getState().authData(); if (authData.size() == 0) { return Task.forResult(null); // Nothing to see or do here... } } List<Task<Void>> tasks = new ArrayList<>(); Iterator<Map.Entry<String, Map<String, String>>> i = authData.entrySet().iterator(); while (i.hasNext()) { Map.Entry<String, Map<String, String>> entry = i.next(); if (entry.getValue() == null) { i.remove(); tasks.add(controller.restoreAuthenticationAsync(entry.getKey(), null).makeVoid()); } } State newState = getState().newBuilder() .authData(authData) .build(); setState(newState); return Task.whenAll(tasks); }
@Test public void testMultipleQueriesWithInflightChanges() throws ParseException { Parse.enableLocalDatastore(null); TestQueryController controller = new TestQueryController(); TaskCompletionSource<Void> tcs = new TaskCompletionSource<>(); controller.await(tcs.getTask()); ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject"); query.setUser(new ParseUser()); ParseCorePlugins.getInstance().registerQueryController(controller); List<Task<Void>> tasks = Arrays.asList( query.fromNetwork().findInBackground().makeVoid(), query.fromLocalDatastore().findInBackground().makeVoid(), query.setLimit(10).findInBackground().makeVoid(), query.whereEqualTo("key", "value").countInBackground().makeVoid()); assertTrue(query.isRunning()); tcs.trySetResult(null); ParseTaskUtils.wait(Task.whenAll(tasks)); assertFalse(query.isRunning()); }
@Test public void testQueryCancellation() throws ParseException { TestQueryController controller = new TestQueryController(); ParseCorePlugins.getInstance().registerQueryController(controller); TaskCompletionSource<Void> tcs = new TaskCompletionSource(); controller.await(tcs.getTask()); ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject"); query.setUser(new ParseUser()); Task<Void> task = query.findInBackground().makeVoid(); query.cancel(); tcs.setResult(null); try { ParseTaskUtils.wait(task); } catch (RuntimeException e) { assertThat(e.getCause(), instanceOf(CancellationException.class)); } // Should succeed task = query.findInBackground().makeVoid(); ParseTaskUtils.wait(task); }