Parse/Android. Getting User Associated with Object - android

I have an Android app that stores company names and those associated with the companies, using Parse.
I am able to get a column created called ownedby that stores the userID.
Now, I want to store information in the company column where the userID in ownedby equals the current user. Here is the query I am using:
ParseQuery<ParseObject> query = ParseQuery.getQuery("midwifefirm");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> midwives, ParseException e) {
// TODO Auto-generated method stub
if(e==null) {
for (ParseObject midwifefirm : midwives) {
String midwiferelation;
ParseUser currentUser;
String userID;
midwiferelation = midwifefirm.getString("ownedby");
currentUser = ParseUser.getCurrentUser();
userID = currentUser.getObjectId();
if (midwiferelation.equals(userID)) {
midwifefirm.put("yearsinpractice", yearsexperience);
midwifefirm.put("practicename", midwifefirmname);
midwifefirm.put("education", education);
}
}
}
else{
Log.d("notretreive", "Error: " + e.getMessage());
}
}
});
This would seem to work; but in the backend, no data is stored, so I guess my "if statement" never works.
Is there anything obvious I am doing wrong? This is for a thesis project, so I can attempt to fake things a bit for the demo, but would like to learn how to do it right.
Thanks so much
Michael

Not sure I got the code below completely right. But the issue you have lies in the fact that ownedBy is a (as I understand) pointer to a User.
When you then try do midwifefirm.getString("ownedby"), you are not getting the objectId as you expect, instead you get something like {'type':'__pointer', 'class' : '_User', 'objectId' : 'xxx'} (I do not remember the notation but hope you get the idea; the literal pointer string value).
Knowing this it is not odd that an objectId xxx String never can equal midwifefirm.getString("ownedby").
Instead of getString, you can call getParseUser, which will return an empty ParseUser with the correct objectId, recall that the pointer only has the minimal information needed to retrieve the object it points to.
You should now be able to make a direct comparison between your current ParseUser and the pointer ParserUser. The fact that the latter is empty should not matter as it only needs the objectId to perform the comparison.
Try this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("midwifefirm");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> midwives, ParseException e) {
// TODO Auto-generated method stub
if(e==null) {
for (ParseObject midwifefirm : midwives) {
ParseUser midwiferelation;
ParseUser currentUser;
midwiferelation = midwifefirm.getParseUser("ownedby");
currentUser = ParseUser.getCurrentUser();
if (midwiferelation.equals(currentUser)) {
midwifefirm.put("yearsinpractice", yearsexperience);
midwifefirm.put("practicename", midwifefirmname);
midwifefirm.put("education", education);
}
}
}
else{
Log.d("notretreive", "Error: " + e.getMessage());
}
}
});

Related

Fetch ParseObject data based on relations query

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

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

Getting data from Parse query

I am having some trouble getting some data from my Parse table/object with a query. I am trying to simply make a query which looks for the current Parse User's objectID in the "sender" column. When that result is returned, I want to extract the receiver's objectID from the "receiver" column associated with the user that I searched for. I keep getting 0 results, even though I know the data is there. Here is my code:
private List<String> potentialRelationQuery() {
mPotentialRelations = new ArrayList<>();
String currentUserId = mCurrentUser.getObjectId();
ParseQuery<ParseObject> query3 = ParseQuery.getQuery("PotentialRelation");
query3.whereEqualTo("sender", currentUserId);
query3.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
if (parseObjects.size() > 0) {
for (int i = 0; i < parseObjects.size(); i++) {
ParseUser receiver = (ParseUser) parseObjects.get(i).get("receiver");
String receiverId = receiver.getObjectId();
mPotentialRelations.add(receiverId);
}
}
} else {
Log.d("MyApp", "No matching objects returned from request");
}
}
});
return mPotentialRelations;
}
Since findInBackground is an asynchronous call to Parse isn't it possible that mPotentialRelations returns empty because the findInBackground query hasn't yet completed before the potentialRelationQuery method returns? I know I've had issues with this. Since you can't return data from an inner class (i.e. in the done method of FindCallback), writing this kind of query method has never really worked consistently for me.

Retrieving data from another class in one query

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.

No results found in query

I'm trying to modify a field for the the currentUser in Parse. I run the following code and my log keeps returning "no results found". I've logged the ObjectId for reference and verified that it's correct in my database.
ParseQuery<ParseUser> query = ParseQuery.getQuery("User");
query.getInBackground(ParseUser.getCurrentUser().getObjectId(), new GetCallback<ParseUser>() {
public void done(ParseUser object, ParseException e) {
if (e == null) {
Log.i("objectId", userObjectId);
object.put("instructorId", instructorId.getText().toString());
object.saveEventually();
} else {
Log.e("objectId", "Error: " + e.getMessage());
Log.i("objectId", userObjectId);
}
}
});
}
});
Your assumption that the User class has the classname User is probably the issue. Try _User.
BUT, you should really be creating your query as
ParseQuery<ParseUser> query = ParseUser.getQuery();
That way you don't even need to know the classname for the user object.

Categories

Resources