Fetch ParseObject data based on relations query - android

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());
}
}
});

Related

Can't Pin a ParseRelation Query result in LocalDataStore?

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

Retrieve ParseUsers list in Java (Android)

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);
}
}
});

Querying a one to many relationship in parse

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.

How to set OR condition for keys in a query to parse.com in android

I have a table of users in parse.com where each of them contains fields first_name and last_name. I'm doing user search and cannot figure out how to make condition OR.
My example:
ParseRelation<ParseUser> relation = user.getRelation("Friends");
ParseQuery<ParseUser> query = relation.getQuery();
query.whereContains("first_name", "typed text"); //need to add OR for the same condition,
//but for the field "last_name"
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if(e == null){
//doing work
}else {
e.printStackTrace();
}
}
});
I tried to do this way:
query.whereContains("first_name", "typed text").whereContains("last_name", "typed text");
but no, it's searching for satisfying both conditions.
Method whereContainsAll() is applicable for list of values, but not keys.
I can do it by sending 2 requests, but this is horrible solution.
Is there a way we can do that?
You can use ParseQuery.or(queries)
Here is the docs link:
https://parse.com/docs/android/guide#queries-compound-queries
Updated 08-2022:
https://docs.parseplatform.org/android/guide/#compound-queries
Example code from doc:
ParseQuery<ParseObject> lotsOfWins = ParseQuery.getQuery("Player");
lotsOfWins.whereGreaterThan(150);
ParseQuery<ParseObject> fewWins = ParseQuery.getQuery("Player");
fewWins.whereLessThan(5);
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(lotsOfWins);
queries.add(fewWins);
ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
mainQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> results, ParseException e) {
// results has the list of players that win a lot or haven't won much.
}
});
ParseQuery<ParseObject> query1 = new ParseQuery<ParseObject>("Place");
query1.whereEqualTo("email","xyz#gmail.com");
ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("Place");
query2.whereEqualTo("place_mobile",909090XXXX);
List<ParseQuery<ParseObject>> list = new ArrayList<ParseQuery<ParseObject>>();
list.add(query1);
list.add(query2);
ParseQuery<ParseObject> query = ParseQuery.or(list);
query.findInBackground();

How to update in parse database custom table using android

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

Categories

Resources