com.parse.OfflineObjectStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of parse-android Show documentation
Show all versions of parse-android Show documentation
A library that gives you access to the powerful Parse cloud platform from your Android app.
/*
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.parse;
import java.util.Arrays;
import java.util.List;
import bolts.Continuation;
import bolts.Task;
/** package */ class OfflineObjectStore implements ParseObjectStore {
private static ParseObjectSubclassingController getSubclassingController() {
return ParseCorePlugins.getInstance().getSubclassingController();
}
private static Task migrate(
final ParseObjectStore from, final ParseObjectStore to) {
return from.getAsync().onSuccessTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
final T object = task.getResult();
if (object == null) {
return task;
}
return Task.whenAll(Arrays.asList(
from.deleteAsync(),
to.setAsync(object)
)).continueWith(new Continuation() {
@Override
public T then(Task task) throws Exception {
return object;
}
});
}
});
}
private final String className;
private final String pinName;
private final ParseObjectStore legacy;
public OfflineObjectStore(Class clazz, String pinName, ParseObjectStore legacy) {
this(getSubclassingController().getClassName(clazz), pinName, legacy);
}
public OfflineObjectStore(String className, String pinName, ParseObjectStore legacy) {
this.className = className;
this.pinName = pinName;
this.legacy = legacy;
}
@Override
public Task setAsync(final T object) {
return ParseObject.unpinAllInBackground(pinName).continueWithTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
return object.pinInBackground(pinName, false);
}
});
}
@Override
public Task getAsync() {
// We need to set `ignoreACLs` since we can't use ACLs without the current user.
ParseQuery query = ParseQuery.getQuery(className)
.fromPin(pinName)
.ignoreACLs();
return query.findInBackground().onSuccessTask(new Continuation, Task>() {
@Override
public Task then(Task> task) throws Exception {
List results = task.getResult();
if (results != null) {
if (results.size() == 1) {
return Task.forResult(results.get(0));
} else {
return ParseObject.unpinAllInBackground(pinName).cast();
}
}
return Task.forResult(null);
}
}).onSuccessTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
T ldsObject = task.getResult();
if (ldsObject != null) {
return task;
}
return migrate(legacy, OfflineObjectStore.this).cast();
}
});
}
@Override
public Task existsAsync() {
// We need to set `ignoreACLs` since we can't use ACLs without the current user.
ParseQuery query = ParseQuery.getQuery(className)
.fromPin(pinName)
.ignoreACLs();
return query.countInBackground().onSuccessTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
boolean exists = task.getResult() == 1;
if (exists) {
return Task.forResult(true);
}
return legacy.existsAsync();
}
});
}
@Override
public Task deleteAsync() {
final Task ldsTask = ParseObject.unpinAllInBackground(pinName);
return Task.whenAll(Arrays.asList(
legacy.deleteAsync(),
ldsTask
)).continueWithTask(new Continuation>() {
@Override
public Task then(Task task) throws Exception {
// We only really care about the result of unpinning.
return ldsTask;
}
});
}
}