bolts.Task类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(222)

本文整理了Java中bolts.Task类的一些代码示例,展示了Task类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Task类的具体详情如下:
包路径:bolts.Task
类名称:Task

Task介绍

[英]Represents the result of an asynchronous operation.
[中]

代码示例

代码示例来源:origin: facebook/facebook-android-sdk

@Suppress
public void testUrlWithNoAppLinkData() {
  String testNoAppLinkUrlString = "https://fb.me/732873156764191_no_app_link";
  Uri testNoAppLinkUrl = Uri.parse(testNoAppLinkUrlString);
  try {
    executeResolverOnBlockerThread(new FacebookAppLinkResolver(), testNoAppLinkUrl);
    getTestBlocker().waitForSignals(1);
    assertNotNull(resolveTask);
    Task<AppLink> singleUrlResolveTask = (Task<AppLink>)resolveTask;
    assertTrue(singleUrlResolveTask.isCompleted() &&
        !singleUrlResolveTask.isCancelled() &&
        !singleUrlResolveTask.isFaulted());
    AppLink appLink = singleUrlResolveTask.getResult();
    assertNull(appLink);
  } catch (Exception e) {
    // Forcing the test to fail with details
    assertNull(e);
  }
}

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public Task<Void> then(Task<String> task) {
    final String uuid = task.getResult();
    if (uuid == null) {
      // The root object was never stored in the offline store, so nothing to unpin.
      return Task.forResult(null);
    }
    return unpinAsync(uuid, db);
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

/* package */ Task<Void> open(final SQLiteOpenHelper helper) {
  synchronized (currentLock) {
    current = current.continueWith(new Continuation<Void, SQLiteDatabase>() {
      @Override
      public SQLiteDatabase then(Task<Void> task) {
        // get*Database() is synchronous and calls through SQLiteOpenHelper#onCreate, onUpdate,
        // etc.
        return (openFlags & SQLiteDatabase.OPEN_READONLY) == SQLiteDatabase.OPEN_READONLY
            ? helper.getReadableDatabase()
            : helper.getWritableDatabase();
      }
    }, dbExecutor).continueWithTask(new Continuation<SQLiteDatabase, Task<Void>>() {
      @Override
      public Task<Void> then(Task<SQLiteDatabase> task) {
        db = task.getResult();
        return task.makeVoid();
      }
    }, Task.BACKGROUND_EXECUTOR); // We want to jump off the dbExecutor
    return current;
  }
}

代码示例来源:origin: facebook/facebook-android-sdk

return Task.forResult(appLinkResults);
final Task<Map<Uri, AppLink>>.TaskCompletionSource taskCompletionSource = Task.create();

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public Task<Boolean> matchesAsync(T object, ParseSQLiteDatabase db) {
    Object objectValue;
    try {
      objectValue = getValue(object, key);
    } catch (ParseException e) {
      return Task.forError(e);
    }
    return Task.forResult(matchesEqualConstraint(queryConstraintValue, objectValue));
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

task.waitForCompletion();
  if (task.isFaulted()) {
    Exception error = task.getError();
    if (error instanceof ParseException) {
      throw (ParseException) error;
  } else if (task.isCancelled()) {
    throw new RuntimeException(new CancellationException());
  return task.getResult();
} catch (InterruptedException e) {
  throw new RuntimeException(e);

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
public <T extends ParseObject> Task<List<T>> findAsync(ParseQuery.State<T> state,
                            ParseUser user, Task<Void> cancellationToken) {
  final AtomicBoolean cancelled = new AtomicBoolean(false);
  cancellationToken.continueWith(new Continuation<Void, Void>() {
    @Override
    public Void then(Task<Void> task) {
      cancelled.set(true);
      return null;
    }
  });
  return await(Task.<Void>forResult(null).continueWithTask(new Continuation<Void, Task<Void>>() {
    @Override
    public Task<Void> then(Task<Void> task) {
      if (cancelled.get()) {
        return Task.cancelled();
      }
      return task;
    }
  })).cast();
}

代码示例来源:origin: parse-community/Parse-SDK-Android

@Test
public void testSubscribeInBackgroundFail() throws Exception {
  ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
  ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error");
  when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception));
  ParseCorePlugins.getInstance().registerPushChannelsController(controller);
  Task<Void> pushTask = ParsePush.subscribeInBackground("test");
  pushTask.waitForCompletion();
  verify(controller, times(1)).subscribeInBackground("test");
  assertTrue(pushTask.isFaulted());
  assertSame(exception, pushTask.getError());
}

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public Task<Void> then(Task<ParsePin> task) {
    if (task.isFaulted()) {
      return task.makeVoid();
    }
    ParsePin pin = task.getResult();
    return unpinAsync(pin, db);
  }
});

代码示例来源:origin: facebook/facebook-android-sdk

@Override
  public AppLink then(Task<Map<Uri, AppLink>> resolveUrisTask) throws Exception {
    return resolveUrisTask.getResult().get(uri);
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public Task<Void> then(Task<Void> ignore) {
    synchronized (tasksLock) {
      // It might be better to return an aggregate error here.
      for (Task<Void> task : tasks) {
        if (task.isFaulted() || task.isCancelled()) {
          return task;
        }
      }
      tasks.clear();
      return Task.forResult(null);
    }
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

@Test
public void testSaveAsyncAlreadyCancelled() throws Exception {
  ParseHttpClient restClient = mock(ParseHttpClient.class);
  ParseFileController controller = new ParseFileController(restClient, null);
  ParseFile.State state = new ParseFile.State.Builder().build();
  Task<Void> cancellationToken = Task.cancelled();
  Task<ParseFile.State> task = controller.saveAsync(state, (byte[]) null, null, null, cancellationToken);
  task.waitForCompletion();
  verify(restClient, times(0)).execute(any(ParseHttpRequest.class));
  assertTrue(task.isCancelled());
}

代码示例来源:origin: parse-community/Parse-SDK-Android

@Test
public void testCallFunctionInBackgroundFailure() throws Exception {
  // TODO(mengyan): Remove once we no longer rely on retry logic.
  ParseRequest.setDefaultInitialRetryDelay(1L);
  ParseHttpClient restClient = mock(ParseHttpClient.class);
  when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException());
  ParseCloudCodeController controller = new ParseCloudCodeController(restClient);
  Task<String> cloudCodeTask =
      controller.callFunctionInBackground("test", new HashMap<String, Object>(), "sessionToken");
  // Do not use ParseTaskUtils.wait() since we do not want to throw the exception
  cloudCodeTask.waitForCompletion();
  // TODO(mengyan): Abstract out command runner so we don't have to account for retries.
  verify(restClient, times(5)).execute(any(ParseHttpRequest.class));
  assertTrue(cloudCodeTask.isFaulted());
  Exception error = cloudCodeTask.getError();
  assertThat(error, instanceOf(ParseException.class));
  assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode());
}

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public T then(Task<List<T>> task) throws Exception {
    if (task.isFaulted()) {
      throw task.getError();
    }
    if (task.getResult() != null && task.getResult().size() > 0) {
      return task.getResult().get(0);
    }
    throw new ParseException(ParseException.OBJECT_NOT_FOUND, "no results found for query");
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public Task<JSONObject> then(Task<JSONObject> task) {
    synchronized (taskQueueSyncLock) {
      pendingEventuallyTasks.remove(uuid);
      uuidToOperationSet.remove(uuid);
      uuidToEventuallyPin.remove(uuid);
    }
    Exception error = task.getError();
    if (error != null) {
      tcs.trySetError(error);
    } else if (task.isCancelled()) {
      tcs.trySetCancelled();
    } else {
      tcs.trySetResult(task.getResult());
    }
    return tcs.getTask();
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public Task<T> then(Task<T> task) {
    final T object = task.getResult();
    if (object == null) {
      return task;
    }
    return Task.whenAll(Arrays.asList(
        from.deleteAsync(),
        to.setAsync(object)
    )).continueWith(new Continuation<Void, T>() {
      @Override
      public T then(Task<Void> task) {
        return object;
      }
    });
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public Task<Void> then(Task<Void> task) {
    synchronized (mutex) {
      if (task.isFaulted() || task.isCancelled()) {
        removeAuthData(authType);
        restoreAnonymity(oldAnonymousData);
        return task;
      }
      return synchronizeAuthDataAsync(authType);
    }
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

@Override
  public Task<ParseConfig> then(Task<String> task) {
    final String sessionToken = task.getResult();
    return toAwait.continueWithTask(new Continuation<Void, Task<ParseConfig>>() {
      @Override
      public Task<ParseConfig> then(Task<Void> task) {
        return getConfigController().getAsync(sessionToken);
      }
    });
  }
});

代码示例来源:origin: parse-community/Parse-SDK-Android

@Test
public void testCallFunctionAsync() throws Exception {
  ParseCloudCodeController controller = mockParseCloudCodeControllerWithResponse("result");
  ParseCorePlugins.getInstance().registerCloudCodeController(controller);
  Map<String, Object> parameters = new HashMap<>();
  parameters.put("key1", Arrays.asList(1, 2, 3));
  parameters.put("key2", "value1");
  Task cloudCodeTask = ParseCloud.callFunctionInBackground("name", parameters);
  ParseTaskUtils.wait(cloudCodeTask);
  verify(controller, times(1)).callFunctionInBackground(eq("name"), eq(parameters),
      isNull(String.class));
  assertTrue(cloudCodeTask.isCompleted());
  assertNull(cloudCodeTask.getError());
  assertThat(cloudCodeTask.getResult(), instanceOf(String.class));
  assertEquals("result", cloudCodeTask.getResult());
}

代码示例来源:origin: parse-community/Parse-SDK-Android

private ParseCurrentConfigController mockParseCurrentConfigControllerWithResponse(
    final ParseConfig result) {
  ParseCurrentConfigController controller = mock(ParseCurrentConfigController.class);
  when(controller.getCurrentConfigAsync())
      .thenReturn(Task.forResult(result));
  return controller;
}

相关文章