I have done storing into database and but when I wish to retrieve the database elements, I always need to specify the objectID which needs to be checked in database. I want a way by which I can get objectID from the element or better get the object id while storing.
"o1l5gCCPB4" is the objectID
Here is code I used for retrieval:
query.getInBackground("o1l5gCCPB4", new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
String playerName = object.getString("foo");
tv.setText(playerName);
} else {
// something went wrong
tv.setText("Something went wrong!!");
}
}
});
You can get an element, this way:
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(PARSE_CLASS_DRAGS);
query.whereEqualTo("objectId", objectId);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
//convert the parseObjects to your objects
}
});
If you want to get all of the elements, you only have to ignore the query.whereEqualTo() row.
Related
I have a table with two attributes: a name and a number. I have editext into which a user enters a name and I should be able to delete the row containing that name.
First of all use:
String text = edittext.getText().toString();
After that, use this query:
ParseQuery<ParseObject> query = ParseQuery.getQuery(TableName);
query.whereEqualTo("Name", text);
query.getInBackground(objectId, new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
object.deleteInBackground();
} else {
// something went wrong
}
}
});
If you need more info, go to: https://parse.com/docs/android/guide
private EditText txtDescription = (EditText) layout.findViewById(R.id.txtDescription)
String string = txtDescription.getText().toString();
Parse Object :
Storing data on Parse is built around the ParseObject. Each ParseObject contains key-value pairs of JSON-compatible data. This data is schemaless, which means that you don't need to specify ahead of time what keys exist on each ParseObject. You simply set whatever key-value pairs you want, and our backend will store it.
https://parse.com/docs/android/guide
String userName= edittext.getText().toString();
If userName contains multiple rows in parse,
To delete one by one use below code,
ParseQuery<ParseObject> query = ParseQuery.getQuery("your table name");
query.whereEqualTo("table_coloumn_name", userName);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject object : objects) {
try {
object.delete();
object.saveInBackground();
} catch (ParseException exe) {
exe.printStackTrace();
}
}
}
});
To delete all rows use below code,
ParseQuery<ParseObject> query = ParseQuery.getQuery("your table name");
query.whereEqualTo("table_coloumn_name", userName);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
ParseObject.deleteAllInBackground(objects, new DeleteCallback() {
#Override
public void done(ParseException e) {
Log.d("delted", "success");
}
});
}
});
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
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.
Hi I was wondering why this code does not work. I am trying to query a ParseUser's username field to find a certain user but it keeps saying that it cant find it.
private void findUserName(String user) {
// query the User database to find the passed in user
ParseQuery query = ParseUser.getQuery();
query.whereEqualTo("username", user);
query.findInBackground(new FindCallback() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
foundUser = (objects.size() != 0);
}
});
}
Here is my method that calls it
if (!foundUser) {
errorMessage.setText("Invalid user name");
}
foundUser is a field because I couldnt return it in the method...
Parse treats User objects separate from Parse objects. You should use List<ParseUser> instead of List<ParseObject>. The Parse Android Guide provides an example https://parse.com/docs/android_guide#users-querying.
Here is the Parse example with your where clause.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", user);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
} else {
// Something went wrong.
}
}
});
İf you want to get current user this may be helpful;
String currentUser = ParseUser.getCurrentUser().getUsername();
Hi I am using the Parse API's database for users and I was wondering how to get the actual ParseUser object so that I can add fields to it? Would I have to query to get the id first then retrieve the obejct that way? As in
ParseQuery query = new ParseQuery("GameScore");
query.getInBackground("xWMyZ4YEGZ", new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
} else {
// something went wrong
}
}
});
Is there an easier way...
Do you mean the currently logged in user?
ParseUser currentUser = ParseUser.getCurrentUser();
Of do you have a relation to a user in another class?
If that is the case you can use ParseQuery.include to include that class in the response also.
ParseQuery query = new ParseQuery("GameScore");
query.include("User");
query.getInBackground("xWMyZ4YEGZ", new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
ParseUser user = object.getParseObject("User"); // We have a user object
} else {
// something went wrong
}
}
});
Be aware for typos made here :)