Skip to content

Instantly share code, notes, and snippets.

@AleksandarIlic
Last active February 25, 2016 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AleksandarIlic/88881d4de3b187bb34c9 to your computer and use it in GitHub Desktop.
Save AleksandarIlic/88881d4de3b187bb34c9 to your computer and use it in GitHub Desktop.
Code snippet for reproducing the issue with retrieving objects from ParseRelation from a Local Datastore.
public void BugReproduction_ParseRelation_getQuery_fromLocalDatastore() throws ParseException {
// Let's create a simple Post ParseObject
ParseObject post = new ParseObject("Post");
post.put("author", "Aleksandar Ilic");
post.put("text", "This is a post.");
post.save(); // Post gets saved on the Cloud with objectId "Q46yLYlx7e"
// And some comments
ParseObject comment1 = new ParseObject("Comment");
comment1.put("text", "This is 1st comment.");
comment1.save();
ParseObject comment2 = new ParseObject("Comment");
comment2.put("text", "This is a 2nd comment.");
comment2.save();
// Now we are going to put the Comments in the Post
ParseRelation commentsRelation = post.getRelation("comments");
commentsRelation.add(comment1);
commentsRelation.add(comment2);
post.save(); // Updating the post in the cloud
// After a while ...
// ... we want to query the Post from the Cloud
ParseQuery query = new ParseQuery("Post");
query.whereEqualTo("objectId", "Q46yLYlx7e");
ParseObject networkPost = query.getFirst(); // Success - Post from above fetched
networkPost.pin("Post"); // Pinning the ParseObject for offline usage
// And also the Comments
List<ParseObject> networkComments = networkPost.getRelation("comments").getQuery().find(); // Success - 2 comments found
ParseObject.pinAll("Comments", networkComments); // Pinning the comments for offline usage
// After another while ...
// ... device went offline and we want our Post
ParseQuery localQuery = new ParseQuery("Post");
localQuery.fromLocalDatastore();
localQuery.whereEqualTo("objectId", "Q46yLYlx7e");
ParseObject localPost = query.getFirst(); // Success - Post found in local datastore
// And again also the Comments
List<ParseObject> localComments = post.getRelation("comments").getQuery().fromLocalDatastore().find();
/*
* Expected result is that 'localComments' (fetched from Local Datastore) have 2 comments
* as it was the case with 'networkComments' (fetched from the Cloud).
*
* Actual result is that 'localComments' has no comments. It's an empty list.
*/
// Important
// If a "Comment" class would have a pointer to a "Post" class everything would work fine.
// Notes
/*
* (1) Code from the above is written with the purpose to reproduce the issue.
*
* (2) Pulling the database from the devie and viewing it with SQLiteManager
* reveals that Comments are pinned and available in the database.
*
* (3) In my production app (not related to this code example) I'm experience
* this issue after the app has been killed by the system or force closed by the user.
* Basically, it works only until the app gets purged out of the memory.
*/
}
@momen-sisalem92
Copy link

We try to reproduce the issue using your code, but it seem that the code not work and need some modification

Why you used query nested of localQuery at this line? https://gist.github.com/AleksandarIlic/88881d4de3b187bb34c9#file-gistfile1-java-L40, we replace them and the code works perfectly

Can you please help us reproduce the issue so we can help solving it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment