Complex Parse Query in Android - android

I have these Classes in parse db.
User ...
Post (user pointer<_User> | images Relation )
Image (post pointer | image File )
Follow ( user_from pointer | user_to pointer)
The questions are,
1- How to get the list of posts with their images of users who i am following. (in java android)
2- How to get a list of my posts.
3- How o get a list of my followers.
4- example of java class of User, Post, Image and Follow objects.
thanks

probably you'll need to construct a relational queries, like the example below:
ParseQuery<ParseObject> innerQuery = ParseQuery.getQuery("Post");
innerQuery.whereExists("image");
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("postPointer", innerQuery);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
// comments now contains the comments for posts without images.
}
});
You can check more about it at Parse Docs

Related

Parse Relation Query:How to Get Only one user from relation list

Hi there I'm new to parse, android and stackoverflow.com, here is my question
I've two classes on parse one is "post" and other is "user" class. Anyone can like post and dislike post.
I've created two relation column "whoLiked" and "whoDisliked" which points the list of users who has liked/disliked a specific post.
When I'm showing the post to the user I want the current User to like/dislike the post Only once which I've set properly.
But only thing I'm not getting is how can I fetch only currentuser from thisPost object.
I don't wanna fetch the list of user who liked this post I just want to know if current User has liked it or not.?
I'm new to stackoverflow.com plz ignore if any mistakes here. Thanks u all in advance.
Below is the query to fetch all the liker
ParseObject post = ...
// create a relation based on the authors key
ParseRelation relation = book.getRelation("whoLiked");
// generate a query based on that relation
ParseQuery query = relation.getQuery();
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> comments, ParseException e) {
if (e == null) {
// "user" is now a list of the user who liked
} else {
// Something went wrong...
}
}
});
Just add a constraint to check if the objectId is the same as the currentUsers's objectId:
// create a relation based on the authors key
ParseRelation relation = book.getRelation("whoLiked");
// generate a query based on that relation
ParseQuery query = relation.getQuery();
// ADD CONSTRAINT HERE:
ParseUser currentUser = ParseUser.getCurrentUser();
query.whereEqualTo("objectId", currentUser.getObjectId());
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> comments, ParseException e) {
if (e == null) {
// "user" is now a list of the user who liked
} else {
// Something went wrong...
}
}
});

Android Parse.com query relation

I have a custom Employee table and custom Organization table.
Employee table has a column Relation< Organization >.
I want to query organizations of an employee object.
// I have an employee object
ParseObject employee = ParseUser.getCurrentUser().getParseObject("employee");
// I get relation query
ParseQuery<ParseObject> query = employee.getRelation("organizations").getQuery();
// I execute query like this
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
// code here
});
In my callback I'm getting ClassCastException:
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject
at com.parse.ParseRESTQueryCommand.constructParameters(ParseRESTQueryCommand.java:84)
at com.parse.ParseRESTQueryCommand.findCommand(ParseRESTQueryCommand.java:15)
at com.parse.ParseDefaultQueryController.findFromNetworkAsync(ParseDefaultQueryController.java:57)
at com.parse.ParseDefaultQueryController.access$100(ParseDefaultQueryController.java:17)
at com.parse.ParseDefaultQueryController$6.runOnNetworkAsync(ParseDefaultQueryController.java:193)
at com.parse.ParseDefaultQueryController.runCommandWithPolicyAsync(ParseDefaultQueryController.java:292)
at com.parse.ParseDefaultQueryController.findWithCachePolicyAsync(ParseDefaultQueryController.java:201)
at com.parse.ParseDefaultQueryController.findAsync(ParseDefaultQueryController.java:28)
at com.parse.ParseQuery.findAsync(ParseQuery.java:1187)
at com.parse.ParseQuery$2$1.then(ParseQuery.java:1129)
at com.parse.ParseQuery$2$1.then(ParseQuery.java:1125)
at bolts.Task$14.run(Task.java:796)
at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
at bolts.Task.completeAfterTask(Task.java:787)
at bolts.Task.continueWithTask(Task.java:599)
at bolts.Task.continueWithTask(Task.java:610)
at bolts.Task$12.then(Task.java:702)
at bolts.Task$12.then(Task.java:690)
at bolts.Task$14.run(Task.java:796)
at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
at bolts.Task.completeAfterTask(Task.java:787)
at bolts.Task.continueWithTask(Task.java:599)
at bolts.Task.continueWithTask(Task.java:574)
at bolts.Task.onSuccessTask(Task.java:690)
at bolts.Task.onSuccessTask(Task.java:680)
at bolts.Task.onSuccessTask(Task.java:714)
at com.parse.ParseQuery$2.call(ParseQuery.java:1125)
at com.parse.ParseQuery$2.call(ParseQuery.java:1122)
at com.parse.ParseQuery.doWithRunningCheck(ParseQuery.java:1091)
at com.parse.ParseQuery.findInBackground(ParseQuery.java:1122)
What am I doing wrong? This is done exactly like in Parse tutorial (https://www.parse.com/docs/android/guide#relations-using-parse-relations). I didn't find any answer to this on Google or SOF.
Any help will be appreciated.
From comment by #cYrixmorten
ParseUser.getCurrentUser().getParseObject("employee") will not by default return the employee object, only the pointer.
Should work if you fetch() your user before calling getParseObject().
If you switch to use an array of pointers instead of a relation you can do something like ParseQuery.getQuery("Employee").get("CITLvyOFuK").include("organizations"). The return object will contain a Employee object where the list of organizations is populated with data, e.g. employee.getList("organizations").get(0).getString("name") will work.
A relation is great if you expect a lot (100+) pointers. This way avoiding to fetch a lot of data for every employee. In your case I see no reason for not simply using an array of organizations.
And a note from me
If you want to add array of pointers in Parse Data Browser use this pattern:
[{"__type":"Pointer","className":"CLASSNAME","objectId":"OBJECTID"},{"__type":"Pointer","className":"CLASSNAME","objectId":"OBJECTID"}]
here's how you do it. survey being the header record with hearaboutusID as the key field in survey linking to hearaboutus table which is a relation.
ParseQuery query = new ParseQuery("survey");
try {
query.include("hearaboutus");
poSurvey = query.get(<objectID>);
} catch (ParseException e) {
e.printStackTrace();
}
ParseQuery<ParseObject> qry = poSurvey.getRelation("hearaboutusID").getQuery();
qry.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> hearList, ParseException e) {
for(ParseObject hear : hearList){
Log.d("HEAR ABOUT US ITEM: ", hear.getObjectId());
}
}
});
https://www.dropbox.com/s/b4h2to1cdbx087b/SAmpleApp.zip?dl=0 try this i have done similar few weeks back whole src code is in it,you might are gonna need few libraries to run it....hope this will help.

Find all following users where their username begins with myString

These are the columns of my Follow Table:
user (Pointer _User)
follower (Pointer _User)
What I want to do is to get all the user where
follower = currentUser
user.username begins with a certain string
I know that in lower-lever db (like mySQL) these data can all be fetched with a single query.
Is it possible in parse? If not, what's the best way to do such a thing?
Assuming your currentUser object is a PFUser
Note... I am program with parse in objective-c so I am not 100% sure this will run. But I feel like this is what your looking for... Your going to have to first query for all the followers who's follower field is equal to the current user.
Then join that query with a user query whose usernames or names, whatever, are equal to your string you pass in... beware the string you pass into the query is case-sensitive... i think...
So if a user in your app searches for BOB and the database has Bob stored... the query won't return Bob... It will just return nothing....
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("username", certainString);
ParseQuery<ParseObject> followerQuery = ParseQuery.getQuery("Followers");
followerQuery.whereEqualTo("follower", ParseUser.getCurrentUser());
followerQuery.matchesKeyInQuery("user", "follower", userQuery);
userQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
// comments now contains the comments for posts with images.
}
});
EDIT Going through Parse's Anypic project that does what you want... I feel that this is not possible... Your going to have to first make a query for the users in the followers table whose followers key is the current user. Then find the objects. Once the result is returned, iterate through the objects to whose names is contained within the contains string.
ParseQuery<ParseObject> followerQuery = ParseQuery.getQuery("Followers");
followerQuery.whereEqualTo("username", certainString);
followerQuery.whereNotEqualTo("username", ParseUser.getCurrentUser().username);
followerQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
for (ParseObject element : list) {
if (element.user == ParseUser.getCurrentUser()) {
//This is a follower of the current user... append to a list object or whatever
}
}
// update a label or table once the iteration is done.
}
});
There still may be a solid solution for what you want to do in one query but I can't think of it right now... Ill come back to this post if I do.

Android | Parse.com | Query User Collection with another Collection

i have basic question. Iam using parse android SDK to retrieve some data from backend. I have 2 collections : "User" and "Photo". What i want to do is that i want to query "Photo" collection which contains some data about image and join that against "User" collection and get some info about owner of picture. Please see code below:
ParseQuery<ParseUser> queryInner = ParseUser.getQuery();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Photo");
query.whereMatchesKeyInQuery("owner", "username", queryInner);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> resultSet, ParseException exception) {
if (exception == null) {
parsqQueryResult = new ArrayList<ObjectUserPhoto>();
ObjectUserPhoto row = new ObjectUserPhoto();
Log.e("size",String.valueOf(resultSet.size()));
for (int a = 0; a < resultSet.size(); a++) {
row = new ObjectUserPhoto();
//this works
row.setDescription(resultSet.get(a).getString(Constants.DB_COL_USER_PHOTOS_DESCRIPTION));
//this is not working
Log.e("username",String.valueOf(resultSet.get(a).getString("col_from_user_table")));
parsqQueryResult.add(row);
}
}
}
});
So basically iam able to acces columns from "Photo" collection, but iam not able to acces any column from "User" collection. How can i acces data from "User" collection ??
Thanks
Martin
In your comment you mentioned that you are not using pointers. I assume that you are just storing the id of the ParseUser objects.
Given a ParseObject's ID, you can retrieve an object by issuing a ParseQuery and passing the ID. Refer to this document.
However, I do not see any reason why you would want to store just the ID and not the Pointer to the object. Imagine if you want to display the information on a list then that means for every item you will need to perform a ParseQuery.
If you store a Pointer then you can leverage on the "include" method (see it here at the bottom part of the "Relational Queries"). This allows you to include the actual object the Pointer is pointing to. So in your case, the results of the Photo query will include the User objects too.
And lastly, I suggest that you read through at least the whole Objects and Queries sections of the documentation as you would learn the basics on how to use the SDK.

Query last rows from parse.com table?

I am trying to query last 20 rows from my Parse.com table. I have followed tutorial and generated the code below. The code is returning 20 rows from the table but not last 20 rows. I returns first 20 items. How to retrieve only last 20 rows?
ParseQuery<ParseObject> query = ParseQuery.getQuery("Activities");
query.setLimit(20);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
}
});
It seems you would only need a different sort method on the ParseQuery you are making. You can order the query results by the CreatedDate field:
// Sorts the results in descending order by the Created Date field
query.orderByDescending("dateCreated");
So, in your code, it would be something like:
//Where "CreatedDate", set the name of the Created Date field in your table.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Activities");
query.orderByDescending("CreatedDate").setLimit(20);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
}
});
In the end, you get also the first 20 results, but the results are ordered the other way around, so that should work as intended.
I had the challenge to order ascending but want to get the last record anyhow.
The solution is simple:
query.ascending("your col");
query.find({
success: function(result){
var lastRow = (result.length) - 1;
var highestValue = result[lastRow].get("your col");
}, error: function //... and so on
Sometimes you don't want to order descending ...
Hope I could give something back to this great forum which helps me a lot!!

Categories

Resources