This is a basic question, but I can't understand how the relationship works in Parse.
I have this relationship: Image link
Briefly, it is a relationship 1 - N. One FeedPost have several comments.
I wish I can send the post ID in the Query and just get the araylist of comments on that post.
ParseQuery<ParseObject> innerQuery = ParseQuery.getQuery("Comments");
innerQuery.whereExists("UXKFwWyn3l"); //ID of the post
ParseQuery<ParseObject> query = ParseQuery.getQuery("FeedPost");
query.whereMatchesQuery("objectId", innerQuery);
Anyone can help me?
With this line
innerQuery.whereExists("UXKFwWyn3l");
you are saying "all records that have a value in the column 'UXKFwWyn3l'"
Also, you are using PFRelation when you should rather use pointers. In Comment, you should have a column with a pointer to the FeedPost. If you did, this query would get you the comments you want, providing you have the FeedPost object already:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
query.whereEqualTo("post", thePostObject );
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> comments, ParseException e) {
if (e == null) {
// "comments" is now a list of the comments
} else {
// Something went wrong...
}
}
});
You can also have a reverse relationship in FeedPost, which should be an array of pointers to the comments (not a PFRelation). If you do, you can get both the FeedPost and the comments with one query:
ParseQuery<ParseObject> query = ParseQuery.getQuery("FeedPost");
query.include("comments"); // this is the column with an array of pointers to comments
query.getInBackground("UXKFwWyn3l", new GetCallback<ParseObject>() {
public void done(ParseObject feedPost, ParseException e) {
if (e == null) {
// Your feedPost now has an array with all the comments
} else {
// something went wrong
}
}
});
You should only use PFRelation for advanced relations (like many-to-many).
Related
I am making a query in a class my problem is that I have some license plates, here in Colombia licenses plates are as it follows ABC 123. I need my query to only return to me, in this case, the ones that end with 2
This is what i did following the documentation...
private void queryForPlaca(String terminaEn){
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestDrive");
query.whereFullText("Placa", terminaEn);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects
) {
listaDeVehiculos.add(obj);
}
ListaVehiculosPicoYPlacaAdapter adapter= new ListaVehiculosPicoYPlacaAdapter(getActivity(),listaDeVehiculos);
listadoTestDrive.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
terminaEn variable querys for the licenses places plates depending on the day selected in a calendar (in this case 2).
My problem is that not only my listaDeVehiculos is not returning any values and the other problem that I see is that it also returns values I don't need for example ABS 124, AXX 524, AEE 234 etc. How should i modify my query?.
You won't be able to do that using text search, since MongoDB text search feature only matches complete terms. You will have to do that using regular expressions. Try the following:
private void queryForPlaca(String terminaEn){
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestDrive");
query.whereMatches("Placa", terminaEn + "$");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects) {
listaDeVehiculos.add(obj);
}
ListaVehiculosPicoYPlacaAdapter adapter= new ListaVehiculosPicoYPlacaAdapter(getActivity(),listaDeVehiculos);
listadoTestDrive.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
Please have in mind that regular expressions are very expensive to the database, so it's important for you to create the appropriate index. A { Placa: 1 } index in the TestDrive collection should help you a lot. For your reference, please take a look here:
https://docs.parseplatform.org/android/guide/#regular-expressions
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...
}
}
});
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.
I need help with Parse queries. I have three classes which have pointers in this relation. Activity class with column "video" pointing to Video class which has another column "videoOwner" pointing to the _User class. What I want to achieve is to query the Activity class for a video and get the associated owner's details from the _User class. I have tried this
ParseQuery<ParseObject> activityQuery = ParseQuery.getQuery("Activity");
activityQuery.setLimit(1000);
activityQuery.whereEqualTo("video", ParseObject.createWithoutData("Video", objectID));
activityQuery.whereEqualTo("type", "comment");
activityQuery.whereExists("video");
activityQuery.include("video");
activityQuery.include("video.videoOwner");
activityQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> vList, ParseException e) {
if (e == null) {
for (int i = 0; i != vList.size(); i++) {
commenterUsername = vList.get(i).getParseObject("video.videoOwner").getString("username");
commenterName = vList.get(i).getParseObject("video").getParseObject("videoOwner").getString("fullName");
Log.e("User", commenterUsername);
}
}
else {
}
}
});
but it clashes at the Log because commenterUsername is null. Any help to enable me to make this query is highly appreciated as i don't want to be making too many queries if there's a simple and cleaner solution available. Thanks
In the code you posted you have getParseObject("video.videoOwner") which is invalid.
getParseObject("video.videoOwner") should be getParseObject("video").getParseObject("videoOwner")
The dot notation only works for include statements, so you have to get the individual objects like this.
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!!