use of bolts.Continuation in project Parse-SDK-Android by ParsePlatform.
the class ParsePinningEventuallyQueue method process.
/**
* Invokes the eventually operation.
*/
private Task<JSONObject> process(final EventuallyPin eventuallyPin, final ParseOperationSet operationSet) {
return waitForConnectionAsync().onSuccessTask(new Continuation<Void, Task<JSONObject>>() {
@Override
public Task<JSONObject> then(Task<Void> task) throws Exception {
final int type = eventuallyPin.getType();
final ParseObject object = eventuallyPin.getObject();
String sessionToken = eventuallyPin.getSessionToken();
Task<JSONObject> executeTask;
if (type == EventuallyPin.TYPE_SAVE) {
executeTask = object.saveAsync(httpClient, operationSet, sessionToken);
} else if (type == EventuallyPin.TYPE_DELETE) {
executeTask = object.deleteAsync(sessionToken).cast();
} else {
// else if (type == EventuallyPin.TYPE_COMMAND) {
ParseRESTCommand command = eventuallyPin.getCommand();
if (command == null) {
executeTask = Task.forResult(null);
notifyTestHelper(TestHelper.COMMAND_OLD_FORMAT_DISCARDED);
} else {
executeTask = command.executeAsync(httpClient);
}
}
return executeTask.continueWithTask(new Continuation<JSONObject, Task<JSONObject>>() {
@Override
public Task<JSONObject> then(final Task<JSONObject> executeTask) throws Exception {
Exception error = executeTask.getError();
if (error != null) {
if (error instanceof ParseException && ((ParseException) error).getCode() == ParseException.CONNECTION_FAILED) {
// We did our retry logic in ParseRequest, so just mark as not connected
// and move on.
setConnected(false);
notifyTestHelper(TestHelper.NETWORK_DOWN);
return process(eventuallyPin, operationSet);
}
}
// since this EventuallyPin is still in eventuallyPinUUIDQueue.
return eventuallyPin.unpinInBackground(EventuallyPin.PIN_NAME).continueWithTask(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(Task<Void> task) throws Exception {
JSONObject result = executeTask.getResult();
if (type == EventuallyPin.TYPE_SAVE) {
return object.handleSaveEventuallyResultAsync(result, operationSet);
} else if (type == EventuallyPin.TYPE_DELETE) {
if (executeTask.isFaulted()) {
return task;
} else {
return object.handleDeleteEventuallyResultAsync();
}
} else {
// else if (type == EventuallyPin.TYPE_COMMAND) {
return task;
}
}
}).continueWithTask(new Continuation<Void, Task<JSONObject>>() {
@Override
public Task<JSONObject> then(Task<Void> task) throws Exception {
return executeTask;
}
});
}
});
}
});
}
use of bolts.Continuation in project Parse-SDK-Android by ParsePlatform.
the class ParseObject method saveEventually.
/**
* Saves this object to the server at some unspecified time in the future, even if Parse is
* currently inaccessible. Use this when you may not have a solid network connection, and don't
* need to know when the save completes. If there is some problem with the object such that it
* can't be saved, it will be silently discarded. Objects saved with this method will be stored
* locally in an on-disk cache until they can be delivered to Parse. They will be sent immediately
* if possible. Otherwise, they will be sent the next time a network connection is available.
* Objects saved this way will persist even after the app is closed, in which case they will be
* sent the next time the app is opened. If more than 10MB of data is waiting to be sent,
* subsequent calls to {@code #saveEventually()} or {@link #deleteEventually()} will cause old
* saves to be silently discarded until the connection can be re-established, and the queued
* objects can be saved.
*
* @return A {@link bolts.Task} that is resolved when the save completes.
*/
public final Task<Void> saveEventually() {
if (!isDirty()) {
Parse.getEventuallyQueue().fakeObjectUpdate();
return Task.forResult(null);
}
final ParseOperationSet operationSet;
final ParseRESTCommand command;
final Task<JSONObject> runEventuallyTask;
synchronized (mutex) {
updateBeforeSave();
try {
validateSaveEventually();
} catch (ParseException e) {
return Task.forError(e);
}
// TODO(klimt): Once we allow multiple saves on an object, this
// should be collecting dirty children from the estimate based on
// whatever data is going to be sent by this saveEventually, which
// won't necessarily be the current estimatedData. We should resolve
// this when the multiple save code is added.
List<ParseObject> unsavedChildren = new ArrayList<>();
collectDirtyChildren(estimatedData, unsavedChildren, null);
String localId = null;
if (getObjectId() == null) {
localId = getOrCreateLocalId();
}
operationSet = startSave();
operationSet.setIsSaveEventually(true);
//TODO (grantland): Convert to async
final String sessionToken = ParseUser.getCurrentSessionToken();
try {
// See [1]
command = currentSaveEventuallyCommand(operationSet, PointerOrLocalIdEncoder.get(), sessionToken);
// TODO: Make this logic make sense once we have deepSaveEventually
command.setLocalId(localId);
// Mark the command with a UUID so that we can match it up later.
command.setOperationSetUUID(operationSet.getUUID());
// Ensure local ids are retained before saveEventually-ing children
command.retainLocalIds();
for (ParseObject object : unsavedChildren) {
object.saveEventually();
}
} catch (ParseException exception) {
throw new IllegalStateException("Unable to saveEventually.", exception);
}
}
// We cannot modify the taskQueue inside synchronized (mutex).
ParseEventuallyQueue cache = Parse.getEventuallyQueue();
runEventuallyTask = cache.enqueueEventuallyAsync(command, ParseObject.this);
enqueueSaveEventuallyOperationAsync(operationSet);
// Release the extra retained local ids.
command.releaseLocalIds();
Task<Void> handleSaveResultTask;
if (Parse.isLocalDatastoreEnabled()) {
// ParsePinningEventuallyQueue calls handleSaveEventuallyResultAsync directly.
handleSaveResultTask = runEventuallyTask.makeVoid();
} else {
handleSaveResultTask = runEventuallyTask.onSuccessTask(new Continuation<JSONObject, Task<Void>>() {
@Override
public Task<Void> then(Task<JSONObject> task) throws Exception {
JSONObject json = task.getResult();
return handleSaveEventuallyResultAsync(json, operationSet);
}
});
}
return handleSaveResultTask;
}
use of bolts.Continuation in project fresco by facebook.
the class ImagePipeline method isInDiskCache.
/**
* Returns whether the image is stored in the disk cache.
*
* @param imageRequest the imageRequest for the image to be looked up.
* @return true if the image was found in the disk cache, false otherwise.
*/
public DataSource<Boolean> isInDiskCache(final ImageRequest imageRequest) {
final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, null);
final SimpleDataSource<Boolean> dataSource = SimpleDataSource.create();
mMainBufferedDiskCache.contains(cacheKey).continueWithTask(new Continuation<Boolean, Task<Boolean>>() {
@Override
public Task<Boolean> then(Task<Boolean> task) throws Exception {
if (!task.isCancelled() && !task.isFaulted() && task.getResult()) {
return Task.forResult(true);
}
return mSmallImageBufferedDiskCache.contains(cacheKey);
}
}).continueWith(new Continuation<Boolean, Void>() {
@Override
public Void then(Task<Boolean> task) throws Exception {
dataSource.setResult(!task.isCancelled() && !task.isFaulted() && task.getResult());
return null;
}
});
return dataSource;
}
use of bolts.Continuation in project Bolts-Java by BoltsFramework.
the class TaskTest method testContinueWhile.
public void testContinueWhile() {
final AtomicInteger count = new AtomicInteger(0);
runTaskTest(new Callable<Task<?>>() {
public Task<?> call() throws Exception {
return Task.forResult(null).continueWhile(new Callable<Boolean>() {
public Boolean call() throws Exception {
return count.get() < 10;
}
}, new Continuation<Void, Task<Void>>() {
public Task<Void> then(Task<Void> task) throws Exception {
count.incrementAndGet();
return null;
}
}).continueWith(new Continuation<Void, Void>() {
public Void then(Task<Void> task) throws Exception {
assertEquals(10, count.get());
return null;
}
});
}
});
}
use of bolts.Continuation in project Bolts-Java by BoltsFramework.
the class TaskTest method testContinueWhileAsync.
public void testContinueWhileAsync() {
final AtomicInteger count = new AtomicInteger(0);
runTaskTest(new Callable<Task<?>>() {
public Task<?> call() throws Exception {
return Task.forResult(null).continueWhile(new Callable<Boolean>() {
public Boolean call() throws Exception {
return count.get() < 10;
}
}, new Continuation<Void, Task<Void>>() {
public Task<Void> then(Task<Void> task) throws Exception {
count.incrementAndGet();
return null;
}
}, Executors.newCachedThreadPool()).continueWith(new Continuation<Void, Void>() {
public Void then(Task<Void> task) throws Exception {
assertEquals(10, count.get());
return null;
}
});
}
});
}
Aggregations