On my application I observe a strange behavior.
I have a Class with a ParseRelation of "Users".
I query this Relation like this:
ParseRelation<ParseObject> relation = this.obj.getRelation("users");
ParseQuery<ParseObject> query = relation.getQuery();
query.whereEqualTo("objectId", anObjectId);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e != null) {
log("error in find : " + e);
} else {
if (!objects.isEmpty()) {
ParseObject.pinAllInBackground(objects)
The Object is found (because it in in Relation from the remote database)
Then I call the same query but adding
query.fromLocalDatastore();
But the object is not found from the localDataStore cache.
Can someone explain me why; and if it is possible to store the result of a ParseRelation query in localDataStore?
[EDIT]:
for info the Parse Initialisatin is done like this:
Parse.initialize(new Parse.Configuration.Builder(context)
//.applicationId("YOUR_APP_ID")
//.clientKey("YOUR_CLIENT_KEY")
.server(hostServer)
//.server("https://parseapi.back4app.com/")
.enableLocalDataStore()
.build()
);
AppId and Client ID are put directly in the Android Manifest file
Related
I have a table (ParseObject) called "Friends" with a "to" and "from" column which use pointers to objectId's of User's. This table tracks who's friends with who, how do I create a query that will return all my friends as user objects - not just the ID stored in that table?
Thanks!
The ParseUser class often confuses me as it is clearly different from a ParseObject and can't be used with the same methods (like ParseQuery.or)
Currently, I'm trying:
ParseQuery<ParseObject> friendsFrom = ParseQuery.getQuery("Friends");
friendsFrom.whereEqualTo("from", ParseUser.getCurrentUser());
ParseQuery<ParseObject> friendsTo = ParseQuery.getQuery("Friends");
friendsTo.whereEqualTo("to", ParseUser.getCurrentUser());
// where they've been accepted
friendsFrom.whereEqualTo("accepted", true);
friendsTo.whereEqualTo("accepted", true);
// combine the queries - parse does this a 'special' way
List<ParseQuery<ParseObject>> friends = new ArrayList<ParseQuery<ParseObject>>();
friends.add(friendsFrom);
friends.add(friendsTo);
ParseQuery<ParseObject> combinedQuery = ParseQuery.or(friends);
combinedQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> friends, ParseException e) {
if (friends != null) {
Log.d(getClass().getSimpleName(), "Found " + friends.size() + " friends");
contacts.clear();
for (ParseObject friend : friends) {
//show user, or whatever you'd like to do..
}
}
});
But a ParseObject cannot be cast to a ParseUser so I can't do anything here..
Update
According to Robert's advice I've tried the following, but it's still not providing me with the UserObjects I need - and for some reason, my Log aren't being written during these "findInBackground" callbacks by Parse..
ParseQuery < ParseObject > friendshipAndUserQuery = ParseQuery.getQuery("friends");
friendshipAndUserQuery.whereEqualTo("from", ParseUser.getCurrentUser().getObjectId());
friendshipAndUserQuery.include("to");
friendshipAndUserQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject friendship : objects) {
UserObject friend = (UserObject) friendship.getParseObject("to");
contacts.add(friend);
}
}
});
Hello im trying to query a one to many relationship in parse. I have an Event table and invitation Table. An event has many invitations and many invitations are created by an event. The Event table has a column called status. Status has a value of 1 which means the owner has. In invitation table i have a column also called status and others users if going to an event will update the status to 1. I was successful in creating the table. Now i am trying to attempt to query both Event and Invitation table where the status is 1. But i am having trouble accomplishing this task. Below is my code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
// Restrict to cases where the author is the current user.
//pass in a ParseUser and not String of that user
query.whereEqualTo("author", ParseUser.getCurrentUser());
query.whereEqualTo("Status", "1");
query.include("EventTitle");
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Invitation");
parseQuery.whereMatchesQuery("EventId", query);
// query.orderByAscending("createAt");
// Run the query
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objectList, ParseException e) {
if (e == null) {
// If there are results, update the list of event and notify the adapter
Log.d(TAG, "Im in background");
// eventList.clear();
invitationList.clear();
for (ParseObject event : objectList) {
invitationList.add((Invitation)event);
}
Log.d(TAG, String.valueOf(invitationList.size()));
updateEventsList();
} else {
Log.d(TAG, "Event retrieval error: " + e.getMessage());
}
}
});
}
And when i try to display in a ListView:
TextView tvTitle = (TextView)convertView.findViewById(R.id.textView_eventTitle);
Error: i get a null pointer indicating a fetch Might be needed. Please what i'm doing wrong
I believe you might have two options:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
query.whereEqualTo("author", ParseUser.getCurrentUser());
query.whereEqualTo("Status", "1");
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Invitation");
parseQuery.whereEqualTo("EventId", id)
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(query);
queries.add(parseQuery);
ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
mainQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> results, ParseException e) {
// results has the list of of invitations/events.
}
});
You can also try and save the eventId to the invitations table, that way you will not have to query two tables. That will however, duplicate some data, which you may or may not be okay with.
I've question about way in which I should create query to get expected data.
I've MyObject (ParseObject) which has pointer to ParseUser (column named createdByPtr). In ParseUser I have custom column userGroupPtr.
Now I want to get all MyObject rows which are created by all users (createdByPtr) belongs to user group (userGroupPtr) to which belongs current logged user (ParseUser.CurrentUser()).
At this moment I have some kind of draft like this:
ParseQuery<MyObject> query = ParseQuery.getQuery(MyObject.class);
query.include("createdByPtr");
query.whereEqualTo("createdByPtr.userGroupPtr", ParseUser.getCurrentUser().get("userGroupPtr"));
query.orderByDescending("updatedAt");
query.findInBackground(new FindCallback<MyObject>() {
#Override
public void done(List<MyObject> results, ParseException e) {
....
}
});
I don't have idea how should I transform line:
query.whereEqualTo("createdByPtr.userGroupPtr", ParseUser.getCurrentUser().get("userGroupPtr"));
in to working constrain.
Any suggestions?
Try like this. I have tested and it works perfectly. see Relational Queries
ParseQuery<ParseUser> innerQuery = ParseUser.getQuery();
innerQuery.whereEqualTo("userGroupPtr","userGroupPtr"); //value of userGroupPtr
ParseQuery<ParseObject> query = ParseQuery.getQuery(MyObject.class);
query.whereMatchesQuery("createdByPtr", innerQuery);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objectList, ParseException e) {
if(e == null){
}else{
Log.e("Object", e.getMessage());
}
}
});
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
ParseConstants.CLASS_POSTS);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> posts, ParseException e) {
if (e == null) {
// we found posts
mPosts = posts;
ParseObject.pinAllInBackground(mPosts);
I have retrieved posts and saved on parse localDatastore by code shown above.
and later I have used ParseQuery<ParseObject> query = ParseQuery.getQuery(ParseConstants.CLASS_POSTS);
query.fromLocalDatastore();
to get posts locally.
But by pinAllInBackground method whole copy of posts get downloaded and saved in localstore. I just want to pin new posts. which method I should call?
I have found solution.
to retrieve only new data we should compare createdAt by adding this to query.
query.whereGreaterthanOrEqualTo("createdAt",dateOfLastRetrievedObject);
I know about how to insert in custom table in parse.com database but i don't know about it how to update. i confuse in the how to get all object value from the table. so you have any idea about it.
Thanks in Advances.
You could try to retrieve the object you want to update and then save it edited like this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.whereEqualTo("firstName", "Dan");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
ParseObject person = list.get(0);
person.put("firstName", "Johan");
person.saveInBackground();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
For more examples you could read the chapter for updating objects in Parse.com documentation here https://www.parse.com/docs/android/guide#objects-updating-objects