/** * Asynchronously gets the data from cache if available or fetches its content from the network. * * @return A Task that is resolved when the data has been fetched. */ public Task<byte[]> getDataInBackground() { return getDataInBackground((ProgressCallback) null); }
/** * Asynchronously gets the data from cache if available or fetches its content from the network. * A {@code ProgressCallback} will be called periodically with progress updates. * A {@code GetDataCallback} will be called when the get completes. * * @param dataCallback A {@code GetDataCallback} that is called when the get completes. * @param progressCallback A {@code ProgressCallback} that is called periodically with progress updates. */ public void getDataInBackground(GetDataCallback dataCallback, final ProgressCallback progressCallback) { ParseTaskUtils.callbackOnMainThreadAsync(getDataInBackground(progressCallback), dataCallback); }
/** * Synchronously gets the data from cache if available or fetches its content from the network. * You probably want to use {@link #getDataInBackground()} instead unless you're already in a * background thread. */ public byte[] getData() throws ParseException { return ParseTaskUtils.wait(getDataInBackground()); }
/** * Asynchronously gets the data from cache if available or fetches its content from the network. * A {@code GetDataCallback} will be called when the get completes. * * @param dataCallback A {@code GetDataCallback} that is called when the get completes. */ public void getDataInBackground(GetDataCallback dataCallback) { ParseTaskUtils.callbackOnMainThreadAsync(getDataInBackground(), dataCallback); }
ParseFile parseFile = new ParseFile(state); byte[] data = ParseTaskUtils.wait(parseFile.getDataInBackground()); byte[] dataAgain = ParseTaskUtils.wait(parseFile.getDataInBackground());
@Test public void testCancel() { ParseFile file = new ParseFile(new ParseFile.State.Builder().build()); TaskQueueTestHelper queueHelper = new TaskQueueTestHelper(file.taskQueue); queueHelper.enqueue(); List<Task<Void>> saveTasks = Arrays.asList( file.saveInBackground(), file.saveInBackground(), file.saveInBackground()); List<Task<byte[]>> getDataTasks = Arrays.asList( file.getDataInBackground(), file.getDataInBackground(), file.getDataInBackground()); file.cancel(); queueHelper.dequeue(); for (int i = 0; i < saveTasks.size(); i++) { assertTrue("Task #" + i + " was not cancelled", saveTasks.get(i).isCancelled()); } for (int i = 0; i < getDataTasks.size(); i++) { assertTrue("Task #" + i + " was not cancelled", getDataTasks.get(i).isCancelled()); } }
Task<byte[]> getDataTaskA = file.getDataInBackground(); queueHelper.enqueue(); Task<Void> saveTaskB = file.saveAsync(null, null, null); queueHelper.enqueue(); Task<byte[]> getDataTaskB = file.getDataInBackground();
/** * Kick off downloading of remote image. When the download is finished, the image data will be * displayed. * * @return A Task that is resolved when the image data is fetched and this View displays the image. */ public Task<byte[]> loadInBackground() { if (file == null) { return Task.forResult(null); } final ParseFile loadingFile = file; return file.getDataInBackground().onSuccessTask(new Continuation<byte[], Task<byte[]>>() { @Override public Task<byte[]> then(Task<byte[]> task) throws Exception { byte[] data = task.getResult(); if (file != loadingFile) { // This prevents the very slim chance of the file's download finishing and the callback // triggering just before this ImageView is reused for another ParseObject. return Task.cancelled(); } if (data != null) { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); if (bitmap != null) { setImageBitmap(bitmap); } } return task; } }, Task.UI_THREAD_EXECUTOR); }
@NonNull @CheckReturnValue public static Single<byte[]> getData(@NonNull final ParseFile file) { return RxTask.single(() -> file.getDataInBackground()); }
@NonNull @CheckReturnValue public static Single<byte[]> getData(@NonNull final ParseFile file, @NonNull final ProgressCallback progressCallback) { return RxTask.single(() -> file.getDataInBackground(progressCallback)); }
@Test public void testParseObservableGetDataProgress() { rx.parse2.ParseObservable.getData(mocker(ParseFile.class) .when(it -> it.getDataInBackground(any(ProgressCallback.class))) .thenReturn(it -> Task.<byte[]>forResult(new byte[1])) .mock(), mock(ProgressCallback.class)) .test() .assertNoErrors() .assertComplete(); }
@Test public void testParseObservableGetData() { rx.parse2.ParseObservable.getData(mocker(ParseFile.class) .when(it -> it.getDataInBackground()) .thenReturn(it -> Task.<byte[]>forResult(new byte[1])) .mock()) .test() .assertNoErrors() .assertComplete(); }