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.
Related
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 am using the parse cloud to store data .
in this case I have a table "MyUser" in this table I want to search user by name in case insensitive.
Search is working fine but the search operation is not case insensitive
I have to search case insensitive
here is my code.:-
ParseQuery<ParseObject> query = ParseQuery.getQuery("BetsUpUser");
query.whereContains("name", searchData);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objList,ParseException e) {
if (e == null) {
Log.d("score","####Retrieved " + objList.size()+ " scores");
} else {
Log.d("score", "###Error: " + e.getMessage());
}
}
});
Thanks in advance.
I Solve the problem of case insensitive string Parse Query
replace the line
query.whereContains("name", searchData);
with
query.whereMatches("name", "("+searchData+")", "i");
I also came across with this question and realized there are no much ways to do this simple thing. Another interesting approach from #ardrian by using JavaScript. For Java it will almost the same. He suggests to create one more field with lowercase version of the searchable one. It look like below:
ParseObject gameScore = new ParseObject("BetsUpUser");
gameScore.put("name", name);
gameScore.put("name_lowercase", name.toLowerCase());
gameScore.saveInBackground();
And while searching we transform input text to lower case. This way we still can use whereContains() method and it will not affect us much.
ParseQuery<ParseObject> query = ParseQuery.getQuery("BetsUpUser");
query.whereContains("name_lowercase", searchData.toLowerCase()); // the only one change
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objList,ParseException e) {
if (e == null) {
Log.d("ParseQuery", objList.get(0).get("name"));//name in actual case
} else {
Log.d("ParseQuery", e.getMessage());
}
}
});
See this post on the Parse Blog about implementing scalable search.
http://blog.parse.com/2013/03/19/implementing-scalable-search-on-a-nosql-backend/
Basically you can use an afterSave event to save a searchable field (i.e. lower-cased name) on the object.
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).
Hi I was wondering how I can get a ParseUser object from a ParseObject. I need to do this because a ParseQuery returns a List. Here is my code and thank you for the help!
// get the currentUser
currentUser = ParseUser.getCurrentUser();
List<ParseObject> friendsList = currentUser.getList("friendsList");
// search for the person the user wants to add as a friend
List<ParseObject> userResults = new ArrayList<ParseObject>();
ParseQuery otherUserQuery = ParseUser.getQuery();
otherUserQuery.whereEqualTo("username", "jyo2");
try {
userResults = otherUserQuery.find();
} catch (ParseException e1) {
// fail
e1.printStackTrace();
}
// get the friend from the query if there was one and add it to the
// currentUser friends list
if (userResults.size() != 0) {
ParseObject currentObject = userResults.get(0);
friendsList.add(currentObject);
currentUser.put("friendsList", friendsList);
}
// save the update
try {
currentUser.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// try to view the friends
List<ParseObject> currentFL = currentUser.getList("friendsList");
ParseObject currentPU = currentFL.get(0);
System.out.println("THIS IS SIZE" + currentFL.size());
System.out.println(currentPU.get("name"));
Use the existing "User" class in Parse but subclass it using
#ParseClassName("_User")
public class PUser extends ParseUser {
As per this article. You actually specifically need to refer to the _User "ParseClassName" when subclassing this object. It's super hairy, I was stuck on this for ages because for other classes you only need to "register" the class with Parse using it's literal name as per the Parse Data Browser, which in this case is "User", "Info", "Post" etc, but the User class specifically requires the underscore
Edit Sorry, I should have mentioned, then you simply cast the subclass PUser when you get your returned objects from the query. You may need to change the query to be a User query instead of an object one too.
I know this is an old question, but this wasn't clear for me and there wasn't a complete thread on the issue I had, on SO. Hope this helped.
I don't think you need to "cast" is to ParseUser, a parseuser is a parseobject as well, apart from having some basic pre-defined properties attached to it, which you can anyways query like you'd query any other parseobject
I have two tables CustomerBalance and Customer which are bound with CustomerRefId field.
I want the CustomerBalance records that are lets say greater than 100 for a field balance of this tables. I also want to include into my results the name of the particular customer that fulfills that criteria. I created the following method that works!
public List<CustomerBalance> getCustomerBalanceFilter(String filterVal) {
try {
PreparedQuery<CustomerBalance> preparedQuery = mDbHelper.getCustomerBalanceDao().queryBuilder()
.where().gt(CustomerBalance.DB_COL_CUSTOMER_BALANCE, filterVal)
.prepare();
List<CustomerBalance> result = mDbHelper.getCustomerBalanceDao().query(preparedQuery);
for(CustomerBalance alert : result) {
PreparedQuery<Customer> getCustQuery = mDbHelper.getCustomerDao().queryBuilder()
.where().eq(Customer.DB_COL_CUSTOMER_REF_ID, alert.getCustomerID())
.prepare();
List<Customer> customer = mDbHelper.getCustomerDao().query(getCustQuery);
alert.setCustomer(customer.size() == 1 ? customer.get(0) : null);
}
return result;
} catch(Exception ex) {
return null;
}
}
This methods is working, is this the best way to write such a query? or is there a more appropriate approach?
One improvement to your query is to use ORMLite's SelectArg to pass in the customer-id instead of a new query each time. Something like:
...
List<CustomerBalance> result = mDbHelper.getCustomerBalanceDao()
.query(preparedQuery);
SelectArg custIdArg = new SelectArg();
PreparedQuery<Customer> getCustQuery = mDbHelper.getCustomerDao().queryBuilder()
.where().eq(Customer.DB_COL_CUSTOMER_REF_ID, custIdArg)
.prepare();
for (CustomerBalance alert : result) {
custIdArg.setValue(alert.getCustomerID());
List<Customer> customer = mDbHelper.getCustomerDao().query(getCustQuery);
alert.setCustomer(customer.size() == 1 ? customer.get(0) : null);
}
Here are the docs for SelectArg:
http://ormlite.com/docs/select-arg
FYI, there also is an UpdateBuilder, but I don't see an easy way to turn your code above into a single UPDATE statement.