I have created 5 tables in parse.com for my Android App:
1. users (name, password,phone, etc)
2. list (List-name ,Note, Image,user)
3. subscribe_user_list (list, user)
4. favorite_user_list (list, user)
5. contacts (contact_name, number, list)
Every list has multiple contacts. A user can subscribe to a list and can mark a list as favorite.
I want to get all lists from list table to which user has subscribed with following details for every list:
1. Total count of users who have subscribed to the list
2. Total number of users who have marked the list as favorite
3. Total number of contact in that list.
This is easily possible in SQL using multiple join queries, however, I don't know how to get this in a single query in parse. Is there anyone who can help me in this..
You can use compound queries to create queries based on other queries results.
"you must create a separate table in which the relationship is tracked.
This table, which we will call Favourite, would have a 'User_Id'
column and a 'Favourite_Id' column, each with a pointer to a Parse User."
currentUser = ParseUser.getCurrentUser();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Favourite");
query.whereEqualTo("User_Id", currentUser);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objectList, ParseException e) {
if (e == null) {
userList = new ArrayList<ParseObject>();
parseUsers = new ArrayList<ParseUser>();
for (int i = 0; i < objectList.size(); i++) {
userList.add((ParseObject) objectList.get(i).get("Favourite_Id"));
}
for (int i = 0; i < userList.size(); i++) {
userList.get(i).fetchIfNeededInBackground(new GetCallback<ParseUser>() {
#Override
public void done(ParseUser object, ParseException e) {
if (e == null) {
parseUsers.add(object);
}
}
});
}
}
}
});
Related
I feel like I'm pretty close on this one, just need the last bit.
I have the following tables:
_User (standard Parse table)
Category (object Id, name)
Exercises (object Id, name, description, thumbnail, image, etc)
and UserFavourites which is where I store the user's preferred exercises
(objectId, user->users table, exercise->exercises table, category->category table)
I have writing to Parse using pointers just fine:
//create new parse object
ParseObject favouriteExercise = new ParseObject("UserFavourites");
//create pointers to the Exercise table and Category table
ParseObject exercise = ParseObject.createWithoutData("Exercises", mExerciseId);
ParseObject category = ParseObject.createWithoutData("Category", mCategoryId);
//put those pointers into the Userfavourites table and save
favouriteExercise.put("user",ParseUser.getCurrentUser());
favouriteExercise.put("exercise",exercise);
favouriteExercise.put("category",category);
//save
favouriteExercise.saveInBackground();
Now I'm trying to retrieve all the exercises a user has favourited and put them in to a listview by searching the table for any objects that match the user's pointer to the user's table:
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> exercises, ParseException e) {
if (exercises != null) {
Toast.makeText(getApplicationContext(), "Favourites found, can't list yet", Toast.LENGTH_SHORT).show();
mAdapter.clear();
//add all the exercises to the list
mAdapter.addAll(exercises);
//sort the list alphabetically
mAdapter.sort(new Comparator<Exercises>() {
#Override
public int compare(Exercises exercises, Exercises t1) {
return exercises.getName().compareTo(t1.getName());
}
});
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
Where I'm stuck is when I run this I can see my query is working -> I am retrieving the 4 rows in UserFavourites that I favourited out of the table of 8, so it is filtering correctly, but the objects I'm getting aren't pointing to the exercises I want. They are just empty pointers.
Thanks.
Yes it will return only reference (Pointer). If you want actual object data call fetchInBackground
myObject.fetchInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// Success!
} else {
// Failure!
}
}
});
I figured it out based on the logic kishore jethava gave.
I queried the favorites table, then with the results I wanted (which pointed to another table) I cycled through each result and got the object it pointed to and added it to my ArrayList.
public void getFavourites() {
//set progress bar
setProgressBarIndeterminateVisibility(true);
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
query.include("exercise");
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> objects, ParseException e) {
if (objects.size() != 0) {
for(ParseObject object : objects)
{
//for each pointer found, retrieve the object it points to
obj = object.getParseObject("exercise");
mAdapter.add((Exercises) obj);
}
});
}
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
//stop progress bar
setProgressBarIndeterminateVisibility(false);
}
});
}
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'm making a small application. trying to retrieve posts inserted, and then I want to retrieve only new inserted posts, and not retrieving all the posts again.
So do you have any idea on how to I can retrieve last items ( Since the list object retrieved)
Here my Query code :
public void getFeed(int limit, int skip){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Feed");
query.setSkip(skip);
query.setLimit(limit);
query.setCachePolicy(ParseQuery.CachePolicy.CACHE_ELSE_NETWORK);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> feedList, ParseException e) {
if (e == null) {
for (int i = 0; i < feedList.size(); i++) {
Post p = new Post(feedList.get(i).get("Text").toString());
mAdapter.addItem(p);
}
Log.d("result", "Here is it:" + feedList.size());
mRecyclerView.setAdapter(mAdapter);
} else {
Log.d("Feed", "Error: " + e.getMessage());
}
}
});
}
keep the last time you checked and then use that time to pull anything greater than the last time checked from the createdAt column or updatedAt column (if its possible for someone to update something you are pulling) of the object. then after your query is finished update that time to the current time.
you can store the viewed posts count and next time when retrieving the list set the count as parameter for skip:
query.setSkip(count);
I've built an application that uses Parse. My application allows users to register, login and then post to a parse cloud database.
I have two Parse classes, one called User and one called Posts. User is made up of ObjectId, username and password, and Posts is made up of ObjectId, text and user. Of which user is a pointer to ObjectId within the User class.
I've created a method in my app called getData() which contains a ParseQuery, this queries the Posts class, selects the text field and includes the user field. The query then retrieves the data into a List and then loops through each row of the List, collecting the String from the text field and then adds it into a ListView on the UI using postList.add(textList.get(i).getString("text")); each time the program goes through the loop.Within the loop is another query, which queries the User class, selects the objectId field, I then add a constraint to the query to tell it to only retrieve data where the objectId field is equal to the user field within the Posts class(I think).
ParseQuery<ParseObject> queryUser = ParseQuery.getQuery("User");
queryUser.selectKeys(Arrays.asList("objectId"));
queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
Next I want to take the collected username data that the query retrieved, put it into a String and display it on screen in a toast. So basically the getData() method should collect all of the strings from the text field and the username of the user that posted it. The problem is that I'm unsure if i'm trying to go about this in the right way. My app throws an error when this piece of code is executed so I'm obviously doing something wrong.
java.lang.IllegalStateException: ParseObject has no data for this key. Call fetchIfNeeded() to get the data.
at com.parse.ParseObject.checkGetAccess(ParseObject.java:3235)
at com.parse.ParseObject.getString(ParseObject.java:2817)
at com.text.parse.MainActivity$3.done(MainActivity.java:186)
Code at line 186 : queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
My questions are:
1. Am I trying to do this in the right way?
2. Why am I receiving this error?
Code for getData() method:
public void getData() {
final ArrayList<String> postList = new ArrayList<String>();
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.listview_row, postList);
final ParseQuery<ParseObject> queryPosts = ParseQuery.getQuery("Posts");
queryPosts.selectKeys(Arrays.asList("text"));
queryPosts.include("user");
queryPosts.addDescendingOrder("createdAt");
queryPosts.setLimit(20);
queryPosts.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> textList, ParseException e) {
if (e == null) {
//query successful
for (int i = 0; i < textList.size(); i++) {
postList.add(textList.get(i).getString("text"));
ParseQuery<ParseObject> queryUser = ParseQuery.getQuery("User");
queryUser.selectKeys(Arrays.asList("objectId"));
queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
queryUser.setLimit(20);
queryUser.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> userList, ParseException e) {
if (e == null) {
String s = userList.get(0).getString("username").toString();
Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
}
else {
//query error
Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
}
}
});
}
lvText.setAdapter(listAdapter);
} else {
//query error
Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
}
}
});
}
Sorry for the long question. Any help would be greatly appreciated, thanks.
UPDATE:For anyone stuck with a similar problem, here's how I got it to work:
public void getData() {
final ArrayList<String> postList = new ArrayList<String>();
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.listview_row, postList);
final ParseQuery<ParseObject> queryPosts = ParseQuery.getQuery("Posts");
queryPosts.include("user");
queryPosts.addDescendingOrder("createdAt");
queryPosts.setLimit(20);
queryPosts.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> textList, ParseException e) {
if (e == null) {
//query successful
for (int i = 0; i < textList.size(); i++) {
postList.add(textList.get(i).getString("text"));
ParseObject po1 = textList.get(i);
ParseObject po2 = po1.getParseObject("user");
String username = po2.getString("username");
Toast.makeText(MainActivity.this, username, Toast.LENGTH_SHORT).show();
}
lvText.setAdapter(listAdapter);
} else {
//query error
Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
}
}
});
}
You simply include the column in the class you are querying that holds a pointer to another class, that then gives you access to all of the columns of data within the second class.
This method as shown is doing nothing useful:
ParseQuery<ParseObject> queryUser = ParseQuery.getQuery("User");
queryUser.selectKeys(Arrays.asList("objectId"));
queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
The selectKeys statement is telling it to only return the contents of the objectId column, which you are passing in to the whereEqualTo statement as a parameter... seems silly to run a query to get a value you already have!?. I would not user selectKeys until you think you need to optimise your queries. The only use this query would have is to let you know if the objectId is valid, since the query will return null if it isn't a valid objectId for a User.
I'm hoping that you want to get more information about the user, so if you remove selectKeys then the other columns will be returned.
The fact that fetchIfNeeded is throwing an exception on due to this line:
queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
That suggests that textList.get(i).getString("user") is not returning an objectId for a user. If that is instead returning a username as suggested by some of your other comments (not sure here), then you need to change that line of code to read:
queryUser.whereEqualTo("username", textList.get(i).getString("user"));
If there are some other questions you have, you'll need to be a bit more precise in your questions as it isn't really clear what you're asking at the moment.