public void updateData() { ParseQuery<Task> query = ParseQuery.getQuery(Task.class); query.whereEqualTo("user", ParseUser.getCurrentUser()); query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK); query.orderByDescending("createdAt"); query.findInBackground(new FindCallback<Task>() { @Override public void done(List<Task> tasks, ParseException error) { if (tasks != null) { mAdapter.clear(); for (int i = 0; i < tasks.size(); i++) { mAdapter.add(tasks.get(i)); } } } }); }
public void updateData() { ParseQuery<Promo> query = ParseQuery.getQuery(Promo.class); query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK); query.orderByDescending("createdAt"); query.findInBackground(new FindCallback<Promo>() { @Override public void done(List<Promo> promo, ParseException error) { if (promo != null) { mAdapter.clear(); for (int i = 0; i < promo.size(); i++) { mAdapter.add(promo.get(i)); } } } }); }
/** * Called when the app starts. * * Load all the available photos for the meme background, * then setup the rest of the photo-related UI. * * You should: * - Set the allPhotos member variable to the photos retrieved from Parse * - Call setUpPhotoSelectorAndPreview(imageSelect, previewImage) * @param imageSelect * @param previewImage */ private void getPhotosAndSetUpUI(final Spinner imageSelect, final ParseImageView previewImage) { ParseQuery<ParseObject> query = ParseQuery.getQuery("Photo"); query.addDescendingOrder("createdAt"); query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK); query.findInBackground(new FindCallback<ParseObject>() { @Override public void done(List<ParseObject> photos, ParseException e) { if (e == null) { Log.d(TAG, "Retrived " + photos.size() + " photos"); allPhotos = photos; setUpPhotoSelectorAndPreview(imageSelect, previewImage); } else { Log.d(TAG, "Error retrieving photos: " + e.getMessage()); } } }); }
/** * Creates a query for talks with all the includes and cache policy set. */ private static ParseQuery<Talk> createQuery() { ParseQuery<Talk> query = new ParseQuery<>(Talk.class); query.include("speakers"); query.include("room"); query.include("slot"); query.include("event"); query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK); return query; }
public void getTodos() { ParseQuery<ParseObject> itemQuery = ParseQuery.getQuery("todoItems"); itemQuery.orderByDescending("itemPriority"); itemQuery.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK); itemQuery.findInBackground(new FindCallback<ParseObject>() { @Override public void done(List<ParseObject> items, ParseException e) { // TODO Auto-generated method stub if (mSwipeRefreshLayout.isRefreshing()) { mSwipeRefreshLayout.setRefreshing(false); } pItems = items; String[] todos = new String[pItems.size()]; int i = 0; ArrayList<String> todo_items = new ArrayList<String>(); // very // important // line. // Fixes // issue // with // swipe // to // dismiss for (ParseObject obj : pItems) { todo_items.add(obj.getString("itemName")); todos[i] = obj.getString("itemName"); i++; } mAdapter2 = new ListAdapter(ToDoListActivity.this, R.layout.list_item, todo_items); // mAdapter = new ArrayAdapter<String>(ToDoListActivity.this, // android.R.layout.simple_list_item_1,todo_items); todolist.setAdapter(mAdapter2); } }); }
private void clearCache() { L.d("Clearing cache. Cache policy: %s", query.getCachePolicy().toString()); cache = null; ParseQuery.clearAllCachedResults(); createQuery(); query.setCachePolicy(CachePolicy.NETWORK_ONLY); notifyDataSetChanged(); }
@Override public void setUp() throws Exception { super.setUp(); // Register a mock cachedQueryController, the controller maintain a cache list and return // results based on query state's CachePolicy ParseQueryController queryController = mock(ParseQueryController.class); Answer<Task<List<ParseObject>>> queryAnswer = new Answer<Task<List<ParseObject>>>() { private List<ParseObject> cachedThings = new ArrayList<>(); @Override public Task<List<ParseObject>> answer(InvocationOnMock invocation) throws Throwable { ParseQuery.State state = (ParseQuery.State) invocation.getArguments()[0]; int start = state.skip(); // The default value of limit in ParseQuery is -1. int end = state.limit() > 0 ? Math.min(state.skip() + state.limit(), TOTAL_THINGS) : TOTAL_THINGS; List<ParseObject> things; if (state.cachePolicy() == CachePolicy.CACHE_ONLY) { try { things = new ArrayList<>(cachedThings.subList(start, end)); } catch (IndexOutOfBoundsException e) { // Cache miss, throw exception return Task.forError( new ParseException(ParseException.CACHE_MISS, "results not cached")); } } else { things = new ArrayList<>(savedThings.subList(start, end)); // Update cache for (int i = start; i < end; i++) { if (i < cachedThings.size()) { cachedThings.set(i, savedThings.get(i)); } else { cachedThings.add(i, savedThings.get(i)); } } } return Task.forResult(things); } }; when(queryController.findAsync(any(ParseQuery.State.class), any(ParseUser.class), any(Task.class))) .thenAnswer(queryAnswer); ParseCorePlugins.getInstance().registerQueryController(queryController); // Register a mock currentUserController to make getSessionToken work ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync()).thenReturn(Task.forResult(mock(ParseUser.class))); when(currentUserController.getCurrentSessionTokenAsync()) .thenReturn(Task.<String>forResult(null)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); ParseObject.registerSubclass(Thing.class); // Make test data set for (int i = 0; i < TOTAL_THINGS; i++) { ParseObject thing = ParseObject.create("Thing"); thing.put("aValue", i * 10); thing.put("name", "Thing " + i); thing.setObjectId(String.valueOf(i)); savedThings.add(thing); } }
public void testLoadObjectsWithCacheThenNetworkQueryAndPagination() throws Exception { QueryFactory<Thing> factory = new QueryFactory<Thing>() { @Override public ParseQuery<Thing> create() { ParseQuery<Thing> query = new ParseQuery<Thing>(Thing.class); query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK); return query; } }; final ParseQueryAdapter<Thing> adapter = new ParseQueryAdapter<>(activity, factory); final int pageSize = 5; adapter.setObjectsPerPage(pageSize); adapter.setPaginationEnabled(true); final Capture<Integer> timesThrough = new Capture<>(0); final Semaphore done = new Semaphore(0); adapter.addOnQueryLoadListener(new OnQueryLoadListener<Thing>() { @Override public void onLoading() { } @Override public void onLoaded(List<Thing> objects, Exception e) { if (e != null) { return; } switch (timesThrough.get()) { case 0: // Network callback for first page assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); adapter.loadNextPage(); break; case 1: // Network callback for second page assertEquals(TOTAL_THINGS - pageSize, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); adapter.loadObjects(); break; case 2: // Cache callback for first page assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); break; case 3: // Network callback for first page assertEquals(pageSize, objects.size()); assertEquals(pageSize + 1, adapter.getCount()); adapter.loadNextPage(); break; case 4: // Cache callback for second page assertEquals(TOTAL_THINGS - pageSize, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); break; case 5: // Network callback for second page assertEquals(TOTAL_THINGS - pageSize, objects.size()); assertEquals(TOTAL_THINGS, adapter.getCount()); done.release(); break; } timesThrough.set(timesThrough.get() + 1); } }); adapter.loadObjects(); // Make sure we assert in callback is executed assertTrue(done.tryAcquire(10, TimeUnit.SECONDS)); }
private void createQuery() { // Parse: Create a query for model Photo and set caching options query = ParseQuery.getQuery(Constants.PARSE_MODEL); query.setCachePolicy(CachePolicy.CACHE_ELSE_NETWORK); }