use of com.parse.boltsinternal.Task in project Parse-SDK-Android by ParsePlatform.
the class ParseSQLiteDatabase method queryAsync.
/**
* Runs a SELECT query.
*
* @see SQLiteDatabase#query
*/
public Task<Cursor> queryAsync(final String table, final String[] select, final String where, final String[] args) {
synchronized (currentLock) {
Task<Cursor> task = current.onSuccess(task13 -> db.query(table, select, where, args, null, null, null), dbExecutor).onSuccess(task12 -> {
Cursor cursor = task12.getResult();
/* Ensure the cursor window is filled on the dbExecutor thread. We need to do this because
* the cursor cannot be filled from a different thread than it was created on.
*/
cursor.getCount();
return cursor;
}, dbExecutor);
current = task.makeVoid();
return task.continueWithTask(task1 -> {
// We want to jump off the dbExecutor
return task1;
}, Task.BACKGROUND_EXECUTOR);
}
}
use of com.parse.boltsinternal.Task in project Parse-SDK-Android by ParsePlatform.
the class NetworkObjectController method saveAllAsync.
@Override
public List<Task<ParseObject.State>> saveAllAsync(List<ParseObject.State> states, List<ParseOperationSet> operationsList, String sessionToken, List<ParseDecoder> decoders) {
int batchSize = states.size();
List<ParseRESTObjectCommand> commands = new ArrayList<>(batchSize);
ParseEncoder encoder = PointerEncoder.get();
for (int i = 0; i < batchSize; i++) {
ParseObject.State state = states.get(i);
ParseOperationSet operations = operationsList.get(i);
JSONObject objectJSON = coder.encode(state, operations, encoder);
ParseRESTObjectCommand command = ParseRESTObjectCommand.saveObjectCommand(state, objectJSON, sessionToken);
commands.add(command);
}
final List<Task<JSONObject>> batchTasks = ParseRESTObjectBatchCommand.executeBatch(client, commands, sessionToken);
final List<Task<ParseObject.State>> tasks = new ArrayList<>(batchSize);
for (int i = 0; i < batchSize; i++) {
final ParseObject.State state = states.get(i);
final ParseDecoder decoder = decoders.get(i);
tasks.add(batchTasks.get(i).onSuccess(task -> {
JSONObject result = task.getResult();
// Copy and clear to create an new empty instance of the
// same type as `state`
ParseObject.State.Init<?> builder = state.newBuilder().clear();
return coder.decode(builder, result, decoder).isComplete(false).build();
}));
}
return tasks;
}
use of com.parse.boltsinternal.Task in project Parse-SDK-Android by ParsePlatform.
the class ParseUserTest method testSaveEventuallyWhenServerError.
// region testSaveEventuallyWhenServerError
@Test
public void testSaveEventuallyWhenServerError() throws Exception {
Shadows.shadowOf(RuntimeEnvironment.application).grantPermissions(Manifest.permission.ACCESS_NETWORK_STATE);
Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application).applicationId(BuildConfig.LIBRARY_PACKAGE_NAME).server("https://api.parse.com/1").enableLocalDataStore().build();
ParsePlugins plugins = ParseTestUtils.mockParsePlugins(configuration);
JSONObject mockResponse = new JSONObject();
mockResponse.put("objectId", "objectId");
mockResponse.put("email", "email@parse.com");
mockResponse.put("username", "username");
mockResponse.put("sessionToken", "r:sessionToken");
mockResponse.put("createdAt", ParseDateFormat.getInstance().format(new Date(1000)));
mockResponse.put("updatedAt", ParseDateFormat.getInstance().format(new Date(2000)));
ParseHttpClient restClient = ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 200, "OK");
when(plugins.restClient()).thenReturn(restClient);
Parse.initialize(configuration, plugins);
ParseUser user = ParseUser.logIn("username", "password");
assertFalse(user.isDirty());
user.put("field", "data");
assertTrue(user.isDirty());
mockResponse = new JSONObject();
mockResponse.put("updatedAt", ParseDateFormat.getInstance().format(new Date(3000)));
ParseTestUtils.updateMockParseHttpClientWithResponse(restClient, mockResponse, 200, "OK");
final CountDownLatch saveCountDown1 = new CountDownLatch(1);
final Capture<Exception> exceptionCapture = new Capture<>();
user.saveInBackground().continueWith((Continuation<Void, Void>) task -> {
exceptionCapture.set(task.getError());
saveCountDown1.countDown();
return null;
});
assertTrue(saveCountDown1.await(5, TimeUnit.SECONDS));
assertNull(exceptionCapture.get());
assertFalse(user.isDirty());
user.put("field", "other data");
assertTrue(user.isDirty());
mockResponse = new JSONObject();
mockResponse.put("error", "Save is not allowed");
mockResponse.put("code", 141);
ParseTestUtils.updateMockParseHttpClientWithResponse(restClient, mockResponse, 400, "Bad Request");
final CountDownLatch saveEventuallyCountDown = new CountDownLatch(1);
user.saveEventually().continueWith((Continuation<Void, Void>) task -> {
exceptionCapture.set(task.getError());
saveEventuallyCountDown.countDown();
return null;
});
assertTrue(saveEventuallyCountDown.await(5, TimeUnit.SECONDS));
assertTrue(exceptionCapture.get() instanceof ParseException);
assertEquals(ParseException.SCRIPT_ERROR, ((ParseException) exceptionCapture.get()).getCode());
assertEquals("Save is not allowed", exceptionCapture.get().getMessage());
assertTrue(user.isDirty());
// Simulate reboot
Parse.destroy();
Parse.initialize(configuration, plugins);
user = ParseUser.getCurrentUser();
assertTrue(user.isDirty());
assertEquals("other data", user.get("field"));
user.put("field", "another data");
mockResponse = new JSONObject();
mockResponse.put("updatedAt", ParseDateFormat.getInstance().format(new Date(4000)));
ParseTestUtils.updateMockParseHttpClientWithResponse(restClient, mockResponse, 200, "OK");
final CountDownLatch saveCountDown2 = new CountDownLatch(1);
user.saveInBackground().continueWith((Continuation<Void, Void>) task -> {
exceptionCapture.set(task.getError());
saveCountDown2.countDown();
return null;
});
assertTrue(saveCountDown2.await(5, TimeUnit.SECONDS));
assertNull(exceptionCapture.get());
assertFalse(user.isDirty());
}
use of com.parse.boltsinternal.Task in project Parse-SDK-Android by ParsePlatform.
the class ParseObjectTest method testParcelWhileSaving.
@Test
public void testParcelWhileSaving() throws Exception {
mockCurrentUserController();
TaskCompletionSource<ParseObject.State> tcs = mockObjectControllerForSave();
// Create multiple ParseOperationSets
List<Task<Void>> tasks = new ArrayList<>();
ParseObject object = new ParseObject("TestObject");
object.setObjectId("id");
object.put("key", "value");
object.put("number", 5);
tasks.add(object.saveInBackground());
object.put("key", "newValue");
object.increment("number", 6);
tasks.add(object.saveInBackground());
object.increment("number", -1);
tasks.add(object.saveInBackground());
// Ensure Log.w is called...
assertTrue(object.hasOutstandingOperations());
Parcel parcel = Parcel.obtain();
object.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
ParseObject other = ParseObject.CREATOR.createFromParcel(parcel);
assertTrue(other.isDirty("key"));
assertTrue(other.isDirty("number"));
assertEquals(other.getString("key"), "newValue");
assertEquals(other.getNumber("number"), 10);
// By design, when LDS is off, we assume that old operations failed even if
// they are still running on the old instance.
assertFalse(other.hasOutstandingOperations());
// Force finish save operations on the old instance.
tcs.setResult(null);
ParseTaskUtils.wait(Task.whenAll(tasks));
}
use of com.parse.boltsinternal.Task in project Parse-SDK-Android by ParsePlatform.
the class ParseTaskUtilsTest method testWaitForTaskWrapsAggregateExceptionAsParseException.
/**
* Verifies {@link AggregateException} gets wrapped with {@link ParseException} when thrown from
* {@link com.parse.ParseTaskUtils#wait(Task)}.
*/
@Test
public void testWaitForTaskWrapsAggregateExceptionAsParseException() {
final Exception error = new RuntimeException("This task failed.");
final ArrayList<Task<Void>> tasks = new ArrayList<>();
for (int i = 0; i < 20; i++) {
final int number = i;
Task<Void> task = Task.callInBackground(() -> {
Thread.sleep((long) (Math.random() * 100));
if (number == 10 || number == 11) {
throw error;
}
return null;
});
tasks.add(task);
}
try {
ParseTaskUtils.wait(Task.whenAll(tasks));
} catch (ParseException e) {
assertTrue(e.getCause() instanceof AggregateException);
}
}
Aggregations