@Override public void close() { // Basically close _eventually_. Task.call(new Callable<Void>() { @Override public Void call() { cursor.close(); return null; } }, executor); }
@Override public Task<Void> setAsync(final T object) { return Task.call(new Callable<Void>() { @Override public Void call() { saveToDisk(coder, object, file); //TODO (grantland): check to see if this failed? We currently don't for legacy reasons. return null; } }, ParseExecutors.io()); }
@Override public Task<T> getAsync() { return Task.call(new Callable<T>() { @Override public T call() { if (!file.exists()) { return null; } return getFromDisk(coder, file, ParseObject.State.newBuilder(className)); } }, ParseExecutors.io()); }
@Override public Task<Boolean> existsAsync() { return Task.call(new Callable<Boolean>() { @Override public Boolean call() { return file.exists(); } }, ParseExecutors.io()); }
public Task<Void> setCurrentConfigAsync(final ParseConfig config) { return Task.call(new Callable<Void>() { @Override public Void call() { synchronized (currentConfigMutex) { currentConfig = config; saveToDisk(config); } return null; } }, ParseExecutors.io()); }
@Override public void done(final Integer percentDone) { Task.call(new Callable<Void>() { @Override public Void call() { progressCallback.done(percentDone); return null; } }, ParseExecutors.main()); } };
@Override public Task<Void> deleteAsync() { return Task.call(new Callable<Void>() { @Override public Void call() { if (file.exists() && !ParseFileUtils.deleteQuietly(file)) { throw new RuntimeException("Unable to delete"); } return null; } }, ParseExecutors.io()); } }
public Task<ParseConfig> getCurrentConfigAsync() { return Task.call(new Callable<ParseConfig>() { @Override public ParseConfig call() { synchronized (currentConfigMutex) { if (currentConfig == null) { ParseConfig config = getFromDisk(); currentConfig = (config != null) ? config : new ParseConfig(); } } return currentConfig; } }, ParseExecutors.io()); }
public Task<Boolean> restoreAuthenticationAsync(String authType, final Map<String, String> authData) { final AuthenticationCallback callback; synchronized (lock) { callback = this.callbacks.get(authType); } if (callback == null) { return Task.forResult(true); } return Task.call(new Callable<Boolean>() { @Override public Boolean call() { return callback.onRestore(authData); } }, ParseExecutors.io()); }
public Task<Void> deauthenticateAsync(String authType) { final AuthenticationCallback callback; synchronized (lock) { callback = this.callbacks.get(authType); } if (callback != null) { return Task.call(new Callable<Void>() { @Override public Void call() { callback.onRestore(null); return null; } }, ParseExecutors.io()); } return Task.forResult(null); } }
@Override public void networkConnectivityStatusChanged(Context context, Intent intent) { final boolean connectionLost = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); final boolean isConnected = ConnectivityNotifier.isConnected(context); /* Hack to avoid blocking the UI thread with disk I/O setConnected uses the same lock we use for synchronizing disk I/O, so there's a possibility that we can block the UI thread on disk I/O, so we're going to bump the lock usage to a different thread. TODO(grantland): Convert to TaskQueue, similar to ParsePinningEventuallyQueue */ Task.call(new Callable<Void>() { @Override public Void call() { if (connectionLost) { setConnected(false); } else { setConnected(isConnected); } return null; } }, ParseExecutors.io()); } };
/** * Retrieves the results of the last time {@link ParseQuery#find()} was called on a query * identical to this one. * * @param sessionToken The user requesting access. * @return A list of {@link ParseObject}s corresponding to this query. Returns null if there is no * cache for this query. */ private <T extends ParseObject> Task<List<T>> findFromCacheAsync( final ParseQuery.State<T> state, String sessionToken) { final String cacheKey = ParseRESTQueryCommand.findCommand(state, sessionToken).getCacheKey(); return Task.call(new Callable<List<T>>() { @Override public List<T> call() throws Exception { JSONObject cached = ParseKeyValueCache.jsonFromKeyValueCache(cacheKey, state.maxCacheAge()); if (cached == null) { throw new ParseException(ParseException.CACHE_MISS, "results not cached"); } try { return networkController.convertFindResponse(state, cached); } catch (JSONException e) { throw new ParseException(ParseException.CACHE_MISS, "the cache contains corrupted json"); } } }, Task.BACKGROUND_EXECUTOR); }
/** * Retrieves the results of the last time {@link ParseQuery#count()} was called on a query * identical to this one. * * @param sessionToken The user requesting access. * @return A list of {@link ParseObject}s corresponding to this query. Returns null if there is no * cache for this query. */ private <T extends ParseObject> Task<Integer> countFromCacheAsync( final ParseQuery.State<T> state, String sessionToken) { final String cacheKey = ParseRESTQueryCommand.countCommand(state, sessionToken).getCacheKey(); return Task.call(new Callable<Integer>() { @Override public Integer call() throws Exception { JSONObject cached = ParseKeyValueCache.jsonFromKeyValueCache(cacheKey, state.maxCacheAge()); if (cached == null) { throw new ParseException(ParseException.CACHE_MISS, "results not cached"); } try { return cached.getInt("count"); } catch (JSONException e) { throw new ParseException(ParseException.CACHE_MISS, "the cache contains corrupted json"); } } }, Task.BACKGROUND_EXECUTOR); }
return Task.call(new Callable<Void>() { @Override public Void call() throws Exception {
@Test public void testMultipleAsynchronousWrites() throws ParseException { int max = 100; ParseKeyValueCache.maxKeyValueCacheFiles = max; // Max out KeyValueCache for (int i = 0; i < max; i++) { ParseKeyValueCache.saveToKeyValueCache("key " + i, "test"); } List<Task<Void>> tasks = new ArrayList<>(); for (int i = 0; i < 1000; i++) { tasks.add(Task.call(new Callable<Void>() { @Override public Void call() { ParseKeyValueCache.saveToKeyValueCache("foo", "test"); return null; } }, Task.BACKGROUND_EXECUTOR)); } ParseTaskUtils.wait(Task.whenAll(tasks)); }
return Task.call(new Callable<Boolean>() { @Override public Boolean call() {
/** * Invokes the callable on a background thread, returning a Task to represent the operation. */ public static <TResult> Task<TResult> callInBackground(Callable<TResult> callable) { return call(callable, BACKGROUND_EXECUTOR); }
/** * Invokes the callable on the current thread, producing a Task. */ public static <TResult> Task<TResult> call(final Callable<TResult> callable) { return call(callable, IMMEDIATE_EXECUTOR); }
/** * 通过图片的缓存地址来初始化界面 */ private void initViewWithCache(final int position) { Task.call(new Callable<List<String>>() { @Override public List<String> call() throws Exception { for (String imageUrl : mImageUrlList) { mCachePathList.add(GlideHelper.getImagePathFromCache(imageUrl, DetailActivity.this)); } return mCachePathList; } }, Task.BACKGROUND_EXECUTOR).continueWith(new Continuation<List<String>, Object>() { @Override public Object then(Task<List<String>> task) throws Exception { List<String> mCachePathList = task.getResult(); if(!Check.isEmpty(mCachePathList)){ for (String cachePath : mCachePathList) { DetailFragment fragment = DetailFragment.newInstance(cachePath); mFragments.add(fragment); } CommonPagerAdapter adapter = new CommonPagerAdapter(getSupportFragmentManager(), mFragments); mVpShowPhoto.setAdapter(adapter); mVpShowPhoto.setCurrentItem(position); } return null; } }, Task.UI_THREAD_EXECUTOR); }