Search in sources :

Example 6 with Continuation

use of com.parse.boltsinternal.Continuation in project Parse-SDK-Android by ParsePlatform.

the class OfflineStore method getOrCreateUUIDAsync.

/**
 * Gets the UUID for the given object, if it has one. Otherwise, creates a new UUID for the
 * object and adds a new row to the database for the object with no data.
 */
private Task<String> getOrCreateUUIDAsync(final ParseObject object, ParseSQLiteDatabase db) {
    final String newUUID = UUID.randomUUID().toString();
    final TaskCompletionSource<String> tcs = new TaskCompletionSource<>();
    synchronized (lock) {
        Task<String> uuidTask = objectToUuidMap.get(object);
        if (uuidTask != null) {
            return uuidTask;
        }
        // The object doesn't have a UUID yet, so we're gonna have to make one.
        objectToUuidMap.put(object, tcs.getTask());
        uuidToObjectMap.put(newUUID, object);
        fetchedObjects.put(object, tcs.getTask().onSuccess(task -> object));
    }
    /*
         * We need to put a placeholder row in the database so that later on, the save can just be an
         * update. This could be a pointer to an object that itself never gets saved offline, in which
         * case the consumer will just have to deal with that.
         */
    ContentValues values = new ContentValues();
    values.put(OfflineSQLiteOpenHelper.KEY_UUID, newUUID);
    values.put(OfflineSQLiteOpenHelper.KEY_CLASS_NAME, object.getClassName());
    db.insertOrThrowAsync(OfflineSQLiteOpenHelper.TABLE_OBJECTS, values).continueWith((Continuation<Void, Void>) task -> {
        tcs.setResult(newUUID);
        return null;
    });
    return tcs.getTask();
}
Also used : Context(android.content.Context) Arrays(java.util.Arrays) ConstraintMatcher(com.parse.OfflineQueryLogic.ConstraintMatcher) Pair(android.util.Pair) TextUtils(android.text.TextUtils) HashMap(java.util.HashMap) UUID(java.util.UUID) ArrayList(java.util.ArrayList) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) List(java.util.List) JSONException(org.json.JSONException) Capture(com.parse.boltsinternal.Capture) Continuation(com.parse.boltsinternal.Continuation) JSONObject(org.json.JSONObject) Map(java.util.Map) ContentValues(android.content.ContentValues) LinkedList(java.util.LinkedList) Task(com.parse.boltsinternal.Task) TaskCompletionSource(com.parse.boltsinternal.TaskCompletionSource) WeakHashMap(java.util.WeakHashMap) Cursor(android.database.Cursor) ContentValues(android.content.ContentValues) TaskCompletionSource(com.parse.boltsinternal.TaskCompletionSource)

Aggregations

Continuation (com.parse.boltsinternal.Continuation)6 Task (com.parse.boltsinternal.Task)6 TaskCompletionSource (com.parse.boltsinternal.TaskCompletionSource)4 List (java.util.List)4 Context (android.content.Context)3 Capture (com.parse.boltsinternal.Capture)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JSONObject (org.json.JSONObject)3 ContentValues (android.content.ContentValues)2 Cursor (android.database.Cursor)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 TextUtils (android.text.TextUtils)2 Pair (android.util.Pair)2 NonNull (androidx.annotation.NonNull)2 ConstraintMatcher (com.parse.OfflineQueryLogic.ConstraintMatcher)2 ParseHttpRequest (com.parse.http.ParseHttpRequest)2 ParseHttpResponse (com.parse.http.ParseHttpResponse)2