Add two users to parse object and Query - android

I want to create a parse object with two users and query for the object using both users. My get other user method returns the other user i want to add to the group.
String id;
final ParseUser[] user = new ParseUser[1];
public void getOtherUser()
{
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "amanda");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
user[0] = objects.get(0);
addUserGroup();
} else {
// Something went wrong.
}
}
});
}
my add userGroup created a parse object with both users
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
After ive done this i want to be able to be able to update the parse object with new content and query for the object based on both users. The following mehtod is breaking on me though and im not sure why.
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
Its returning the following error
java.lang.IllegalArgumentException: invalid type for ParseObject: class [Lcom.parse.ParseUser;
anybody know why this might be happening?

When you call ParseUser.getCurrentUser() parse return a user object. Now assuming you want to query usernames you need to change the ParseUser.getCurrentUser() to ParseUser.getCurrentUser().getUsername()
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser().getUsername());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser().getUsername() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
}

Related

Query a ParseObject(Pointer) in User Class

Hello i am having some trouble in a Query. I made a pointer called "Empresa" in the _User class. i understand that this pointer is a ParseObject.So i did this i tried to do it in two ways...
private void queryEmpresa(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId);
query.include("Empresa");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects
) {
empresa=obj.getParseObject("Empresa");
String id=empresa.getObjectId();
}
}
});
}
and also...
private void queryEmpresa(){
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
query.include("Empresa");
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
for (ParseUser obj:objects
) {
empresa=obj.getParseObject("Empresa");
String id=empresa.getObjectId();
}
}
});
}
tell me which is the correct code and what do i need to modify in order to work. Can you please explain to me why is the reason this isnt working so that i dont incur into this problem in the near future?.
Try this and check if you get some error from the server:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.include("Empresa");
query.getInBackground(ParseUser.getCurrentUser().getObjectId(), new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your user and you should be able to retrieve Empresa like this
empresa = object.getParseObject("Empresa");
} else {
// something went wrong. It would be good to log.
}
}
});

How to query a pointer in a ParseObject class?

Hi i made ParseObject which contains a field called Usuario which is a pointer to _User(ParseUser class)
ive been trying to query for this value with no success...
private void queryConversaciones(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Conversaciones");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects
) {
mensaje=obj.getString("Mensaje");
enviaMensaje= (ParseUser) obj.get("Usuario");
// mMessageList.add(obj);
}
}
});
}
Just add query.include("key"); to retrieve the pointer object.
So your complete query should look like this:
private void queryConversaciones(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Conversaciones");
// Include data at pointer
query.include("Usuario");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects
) {
mensaje=obj.getString("Mensaje");
enviaMensaje= (ParseUser) obj.get("Usuario");
// mMessageList.add(obj);
}
}
});
}
Now you should be able to access all the data stored in the _User object.
I got all this information from the relational queries section in the Parse Android guide.

Retrieve data from parse based on object ID?

I have a list of objectId where I am trying to retrieve another list in another class where the pointer value equals the objectId of the current parseObject.
I realised that I am comparing a string "objectId" to a pointer "threadPointer" so can someone help me with the query to retrieve a list of items from a class if the objectId equals the threadPointer _Pointer value
public void retrieveThreadListStoreInDB()
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Threads");
query.include("postedBy");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
// Query is successful now lets load data from parse
ParseObject.pinAllInBackground((List<ParseObject>) list, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
if (!isFinishing()) {
// TODO : Notify to refresh data
}
} else {
Log.d("DEBUG", "ERROR PINNING DATA WITH EXCEPTION : " + e);
}
}
});
}
}
});
}
public void retrievePostsListStoreInDB()
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.include("postedBy");
query.include("threadPointer");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> list, ParseException e) {
if (e == null) {
// Query is successful now lets load data from parse
ParseObject.pinAllInBackground((List<ParseObject>) list, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
if (!isFinishing()) {
// TODO : Notify to refresh data
Log.d("DEBUG: ","Finished retrieval");
Log.d("DEBUG", list.toString());
ParseObject object = (ParseObject)list.get(1).get("threadPointer");
Log.d("DEBUG_THREAD_POINTER", object.getObjectId());
}
} else {
Log.d("DEBUG", "ERROR PINNING DATA WITH EXCEPTION : " + e);
}
}
});
}
}
});
}
This is where I am retrieving the ThreadList from local db
public void retrieveThreadList(){
List<ParseObject> parseObjectList = new ArrayList<ParseObject>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Threads");
query.orderByDescending("updatedAt");
query.fromLocalDatastore();
// Data retrieval was successfull
try {
parseObjectList= query.find();
} catch (ParseException e) {
e.printStackTrace();
}
for (ParseObject parseObject : parseObjectList) {
String title = (String) parseObject.get("threadTitle");
ParseUser parseUser = (ParseUser) parseObject.get("postedBy");
String profilePictureUrl="local";
try {
profilePictureUrl = ((ParseFile) parseUser.get("profilePicture")).getUrl();
}catch (Exception exception)
{
Log.d("DEBUG_FB","profilePicture not retrieved");
}
String threadObjectId = parseObject.getObjectId();
Log.d("DEBUG_FB_THREAD_ID",threadObjectId);
ParseQuery parseQueryToRetrievePosts = ParseQuery.getQuery("Posts");
parseQueryToRetrievePosts.fromLocalDatastore();
//----****** NEED HELP HERE ******----
parseQueryToRetrievePosts.whereEqualTo("threadPointer",parseObject.getObjectId());
try {
List<ParseObject> postsList = parseQueryToRetrievePosts.find();
Log.d("DEBUG_FB_POSTS_LIST",postsList.toString());
} catch (ParseException e) {
e.printStackTrace();
}
/* To retrieve data over the network --- THIS WORKS WELL BUT I DONT WANT A NETWORK CALL
ParseRelation <ParseObject> posts = parseObject.getRelation("postMessage");
ParseQuery postsQuery = posts.getQuery();
List<ParseObject> postsList = new ArrayList();
try {
postsList=postsQuery.find();
for (ParseObject parseObject1: postsList)
{
String postMessage = (String)parseObject1.get("postMessage");
Log.d("DEBUG_FB_MSG", postMessage);
}
Log.d("DEBUG_FB_POSTS", postsList.toString());
}catch (Exception e)
{
}
*/
threadModelStore threadObject = new threadModelStore(title,profilePictureUrl);
Log.d("DEBUG_FB", profilePictureUrl);
Log.d("DEBUG_FB", title);
threadItemList.add(threadObject);
}
Sir my scenario is I have these classes
THREADS(Class) which has POSTS(RELATION TO _POSTS) class I want to retrieve the related posts of a particular thread. I was unable to pin
relations to local data store please let me know if there is a way to
do so ? -- (UNSOLVED)
So what I want to do is to compare the thread pointer in the POSTS
class and retrieve the related posts. For this I needed to compare a
string object id to pointer field _ThreadPointer in POSTS Class
Suggest a possible solution -- SOLVED BY ( VED ).
ParseObject threadPointer=ParseObject.createWithoutData("Pointer_class_Name", "pointer_object_id");
ParseQuery<ParseObject> query=ParseQuery.getQuery("className");
query.whereEqualTo("_Pointer", threadPointer);
query.include("_Pointer");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> listCommunity, ParseException e) {
// TODO Auto-generated method stub
Log.d(TAG, "done");
if(e==null){
// access your data
}else{
// Error in retrieving data
}

SaveAllInBackground doesn't work inside deleteAllInBackground as desired

SaveAllInBackground doesn't work inside deleteAllInBackground as desired.
I am trying to save a list of parseobjects using save all in background. To avoid duplicates in the table, I am querying for the already existing rows and deleting them if any and then save the new copy. Therefore I am calling the saveAllInBackground inside the deleteAllInBackground's callback.
The problem is this :
For ex: if the list to delete contains [a,b,c,d] and the list to upload has [a,b,c,d,e,f] only [e,f] get persised to parse. I am passing [a,b,c,d,e,f] to the saveAllInBackground but only [e,f] get persisted.
Is there something I am missing? How to solve this?
Can I use a different approach?
Is there a better way to avoid duplicates? I dont want to add a
beforeSave hook. The whole purpose of calling the saveAll is to reduce the number of API calls. I guess if I use beforeSave, I will have to run some queries in the cloud code anyway.
This is my code
ParseQuery query = new ParseQuery("PostChoice");
query.fromPin();
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> localList, ParseException e) {
if (localList != null && !localList.isEmpty()) {
List<ParseObject> postList = new ArrayList<ParseObject>();
for (ParseObject object : localList) {
postList.add(object.getParseObject("post"));
}
ParseQuery query = new ParseQuery("PostChoice");
query.whereContainedIn("post", postList);
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseCloudList, ParseException e) {
if (parseCloudList != null && !parseCloudList.isEmpty()) {
ParseObject.deleteAllInBackground(parseCloudList, new DeleteCallback() {
#Override
public void done(ParseException e) {
// this gets executed and rows are accordingly deleted
ParseObject.saveAllInBackground(localList, new SaveCallback() {
#Override
public void done(ParseException e) {
// this gets executed but the rows are not uploaded.
//the locallist is not empty. it contains the right data.
editor.putLong(Four.LAST_CHOICE_SYNC_TIME, System.currentTimeMillis());
editor.commit();
Log.i("SyncChoiceService", "Synced Choices");
}
});
}
});
}
else{
ParseObject.saveAllInBackground(localList, new SaveCallback() {
#Override
public void done(ParseException e) {
Log.i("SyncChoiceService", "Synced Choices");
editor.putLong(Four.LAST_CHOICE_SYNC_TIME,System.currentTimeMillis());
editor.commit();
}
});
}
}
});
}
}
});
I have come up with a solution like this. and it meets my requirement. I use the updatedValue and delete the old ones and the remaining get updated as a whole list.
ParseQuery query = new ParseQuery("PostChoice");
query.fromPin();
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> localList, ParseException e) {
if (localList != null && !localList.isEmpty()) {
List<ParseObject> postList = new ArrayList<ParseObject>();
for (ParseObject object : localList) {
postList.add(object.getParseObject("post"));
}
ParseQuery query = new ParseQuery("PostChoice");
query.whereContainedIn("post", postList);
query.whereLessThan("updatedAt",System.currentTimeMillis());
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> parseCloudList, ParseException e) {
if (parseCloudList != null && !parseCloudList.isEmpty()) {
ParseObject.deleteAllInBackground(parseCloudList, new DeleteCallback() {
#Override
public void done(ParseException e) {
Log.i("SyncChoiceService", "Deleted old Choices");
}
});
}
}
});
ParseObject.saveAllInBackground(localList, new SaveCallback() {
#Override
public void done(ParseException e) {
Log.i("SyncChoiceService", "Synced Choices");
}
});
}
}
});
Yes, if i understand you correctly you want to save only new data from localdb to parse backend.
Best and less request solution would be to have anther field in your table called "Draft" or "isUpdated" (name as you want). Role of this flag is to identify whether this field is saved in backend or not. if its a new field "isUpdated" is false else its true. Then in query you can query only the isUpdated is false. Then save them in backend. Then
You don't want to delete any data.
Reduce requests
Less unnecessary logic in your code.
it's clean
Hope this helps

how to fetch ParseUser object by objectId?

I have objectId and I want to fetch ParseUser object which has same objectId.
I have read that we can use get to get ParseUser by id at Parse documentation but syntax is not given there. If anyone know please help me.
I have tried this but it is not working.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", userId);
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> object, ParseException e) {
// TODO Auto-generated method stub
if(e==null)
for(ParseUser obj: object)
{
adress.setText(obj.getString("address"));
contact.setText(obj.getString("contact"));
}
else
{
adress.setText("Not found");
}
}
});
You can use the following code to get the user;
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("objectId",userId);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
//List contain object with specific user id.
} else {
// error
}
}
});
Hope this helps
Regards.

Categories

Resources