I am trying to retrieve 2 values from my Cloud Code function. A List of ParseUser and a List of ParseObject.
This is a snippet of the Cloud Code.
...
var finalResult = {"users":users,"groups":groups};
//users is an array of Parse.User
//groups is an array of Parse.Object
response.success(finalResult);
This is the call from the client.
ParseCloud.callFunctionInBackground("serviceUpdate", new HashMap<String, Object>(),new FunctionCallback<HashMap<String, List<ParseObject>>>() {
public void done(HashMap<String, List<ParseObject>> result, ParseException e) {
if (e == null) {
List<ParseObject> users = result.get("users");
List<ParseObject> groups = result.get("groups");
As you can see, users is now a ParseObject. How can I obtain the ParseUser? Any suggestion is appreciated.
Can't you use casting, as follows:
ParseCloud.callFunctionInBackground("serviceUpdate", new HashMap<String, Object>(),new FunctionCallback<HashMap<String, List<ParseObject>>>() {
public void done(HashMap<String, List<Object>> result, ParseException e) {
if (e == null) {
List<ParseUser> users = result.get("users");
List<ParseObject> groups = result.get("groups");
To obtain as results Parse User you have to query the "User" table with this specific query https://parse.com/docs/android/guide#users-querying.
So you can retrieve the unique id from your posted query (it is a string) and then use that as filter for the new query.
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 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.
I am trying to retrieve some data from a couple of parse tables using parse cloud code.
ParseCloud.callFunctionInBackground("getData", parameters, new FunctionCallback<List>() {
#Override
public void done(List results, ParseException e) {
if (e == null) {
for (ParseObject country : results) {
}
}
}
});
The List results returns all the data from all the tables included in the cloud code. But I don't know how to access them.
Here is the data that the arrayList retrieves:
[{startup=com.parse.ParseObject#1480b86a,
resolvedRelations=
{advisors=[com.parse.ParseObject#3efb015b, com.parse.ParseObject#32ab7df8, com.parse.ParseObject#393662d1],
activeSyndicate= {investors=[com.parse.ParseObject#2ce9f136, com.parse.ParseObject#2cde5837, com.parse.ParseObject#b1681a4, com.parse.ParseObject#3ced5b0d, com.parse.ParseObject#3bdbeac2]},
images=[com.parse.ParseObject#123640d3, com.parse.ParseObject#3f0c1410],
founders=[com.parse.ParseObject#1aa19b09, com.parse.ParseObject#601f10e, com.parse.ParseObject#251a172f],
tags=[com.parse.ParseObject#1c86613c, com.parse.ParseObject#73fdec5, com.parse.ParseObject#1d70101a, com.parse.ParseObject#3bbaf74b, com.parse.ParseObject#34f65528, com.parse.ParseObject#1fc4a241, com.parse.ParseObject#2f6f13e6]}}
The ArrayList returns objects, but I need ParseObject. Help please.
I am new to parse data from parse.com.I am trying to update a column.The column in parse table is of array type.I am trying to add a value in array.For Example :
it is showing in data browser like this:
["Ado", "Wassja", "Cristi_3"]
And I want to add "ABC" value in this array programmatically like this:
["Ado", "Wassja", "Cristi_3","ABC"]
I have searched for this and got to know that first I need to fetch all the data of that particular row which I have to update ,then put the data in array I have fetch successfully the data for that particular row like this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserMaster");
query.whereEqualTo("userName",str_uname2);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> userList, ParseException e) {
dlg.dismiss();
if (e == null) {
if (userList.size()>0) {
for (int i = 0; i < userList.size(); i++) {
ParseObject p = userList.get(i);
str_dbpassword = p.getString("password");
String email = p.getString("email");
List<ParseObject> mfollowers = p.getList("followers");
List<ParseObject> mfollowing = p.getList("following");
ParseFile pp = (ParseFile)p.get("photo");
str_dbuname = p.getString("userName");
}
Log.d("password", "Retrieved " +str_dbpassword +"<uname>"+str_dbuname);
}
}
else {
Alert.alertOneBtn(LoginActivity.this,"Something went wrong!");
}
}
});
Now I have to update data in
List<ParseObject> mfollowers = p.getList("followers");
And
List<ParseObject> mfollowing = p.getList("following");
I don't know how to do this.Please help me.Your small clue will be very helpful.
Per the docs: http://parse.com/docs/android/api/com/parse/ParseObject.html
You can use add, addUnique, addAll, or addAllUnique to add elements to an array on a Parse Object:
someParseObject.add("arrayColumn", "ABC");
someParseObject.saveEventually();
To expand on Fosco's answer:
ParseObject parseObject = new ParseObject("YOURCLASS");
String[] stringArray = ["Ado", "Wassja", "Cristi_3"];
List<String> stringArrayList = new ArrayList(Arrays.asList(stringArray));
if (stringArray != null)parseObject.addAll("YOURCOLUMN", stringArrayList);
Make sure that the security settings for YOURCLASS enable you to create columns automatically or that you have the correct types set for your columns.
I hope that completes Fosco's answer.
My Class in parse is called "MyClass" and this one has several objects like
| ObjectId | Names | owners | users | (owners is a pointer of another class)
I want to do a query that gives me all of the names in my object "Names" that belong to the owner but, when I do this query i get this:
com.parse.ParseObject#41828fe0
com.parse.ParseObject#41829fdd
com.parse.ParseObject#4182aa28
my code is this
final ParseQuery query = new ParseQuery("MyClass");
query.whereEqualTo("owners", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback() {
public void done( List<ParseObject> MyList, ParseException e) {
if (e == null) {
adapter = new ArrayAdapter<ParseObject>(MyActivity.this, android.R.layout.simple_list_item_1, MyList);
listDev.setAdapter(adapter);
}else{
//error
}
please help me how to do that query that gives me all of name that belong to the owner.
EDIT
I found a solution and is something like this.
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
ParseUser user = ParseUser.getCurrentUser();
query.whereEqualTo("owners", user);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject parseObject : objects){
String task;
task= parseObject.get("owners").toString();
adapter.add(task);
adapter.notifyDataSetChanged();
}
} else {
// Something went wrong.
Toast.makeText(getActivity(),"Error: " + e.getMessage().toString(),Toast.LENGTH_SHORT).show();
}
}
});
Your real problem here is your ArrayAdapter. You would need a custom adapter if you want to use Parse objects as your data type. The built in adapter doesn't know how to use Parse objects and is outputting the object as a string for you. Instead you should do something like
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, MyNamesList);
Where MyNamesList is of type String.
It's hard to help on your query without more information but you are getting Parse objects back, you just need to get the name out of them with something like
MyList.get(i).getString("name");
somewhere outside the query method
private ArrayList<YourObjectType> list;
public void done(List<ParseObject> objects, ParseException e) {
if (objects == null || objects.size() == 0) {
return; //no objects
}
list = new ArrayList<YourObjectType>();
for (int i = 0; i < objects.size(); i++) {
YourObjectType myObject = new YourObjectType();
ParseObject object = objects.get(i);
myObject.objectId = object.getObjectId();
myObject.names = object.get("Names");
myObject.owners = object.get("owners");
myObject.users = object.get("users");
list.add(myObject);
}
adapter = new ArrayAdapter<YourObjectType>(MyActivity.this,android.R.layout.simple_list_item_1, MyList);
listDev.setAdapter(adapter);
}
now set the list to your adapter
your adapter should be able to handle objects of your type, so i think you need to create a custom adapter, the one provided by the android sdk, won't do here
The solution is pretty easy, since you are getting com.parse.whatever, all u need to do is ParseUser user = ParseUser.getCurrentUser();
Then query.whereequalto("owner", user.getUsername());
The problem was, u where querying for currentUser instead of usernames.