Storing/Getting values in Parse - android

I'm using Parse as my back-end solution and I now need to store some booleans to the server and retrieve them as well.
I have created a ParseObject:
public ParseObject prefs = new ParseObject("Prefs");
and I stored it like this:
prefs.put("setting1", true);
and saved in background:
prefs.saveInBackground();
but when I try to use the bool after retrieving it simply like this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Prefs");
query.getInBackground(prefs.getObjectId(), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if(e==null){
if(parseObject.getBoolean("setting1")){
//Do something
}
} else{
utils.toast_error("Check your internet connection");
Gdx.app.log("PARSE", "Some "+e.getCode());
}
}
});
it doesn't return true, instead it returns it core value false.
Have I missed a step or something? Is there more to retrieving data than what I've tried?

Are sure to use the getInBackground method to retrieve the ParseObject?
This could be useful:
https://www.parse.com/docs/android/guide#subclasses

Related

Android - Parse - deleteInBackground, record not being deleted

I'm using Parse with Android in order to sync my data.
I'm trying to delete an object which is stored in the Parse cloud via
The callback returns and there's no exception, the Logcat message is "deleted".
But the object still exists in table when I check the Parse Data.
tastToEdit is an object from Task class (configured locally in my app).
ParseObject parse_task = new ParseObject("Task");
parse_task.put("Description",tastToEdit.getDescription());
parse_task.put("DueDate",tastToEdit.getDueDate());
parse_task.put("Priority",tastToEdit.getPriority().ordinal());
int com_state = (tastToEdit.getCompleted()) ? 1 : 0;
parse_task.put("IsCompleted",com_state);
parse_task.put("Location",0);
parse_task.put("Category",tastToEdit.getTask_catg().ordinal());
parse_task.put("Status", tastToEdit.getTask_sts().ordinal());
//parse_task.deleteInBackground();
parse_task.deleteInBackground(new DeleteCallback() {
public void done(ParseException e) {
if (e == null) {
Log.d("msg","deleted");
} else {
Log.d("msg", "not deleted");
e.printStackTrace();
}
}
});
What might be causing the callback to return as "deleted" but the object still remains?
Thanks in advance.
You are creating a new ParseObject and you try to delete it after but you don't provide an ObjectID.
A better way to do this will be to first do a ParseQuery for that task you are looking and the in the completion delete it.

Parse File Query Not Working

I am attempting to get a file from Parse through a query. Parse is very vague on how to get files from the cloud, but I have managed to create a basic query to attempt to get a file
ParseQuery query = ParseUser.getQuery();
query.getInBackground(objects.get(i).getObjectId(), new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
ParseFile fileObject = (ParseFile) object.get("picture");
fileObject.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
ImageView image = (ImageView) findViewById(R.id.profilePicture);
image.setImageBitmap(bmp);
} else {
Toast.makeText(getApplicationContext(), "Image Not Found", Toast.LENGTH_SHORT).show();
}
}
});
}
});
The code above gives me a NullPointerException on the line:
ParseFile fileObject = (ParseFile) object.get("picture");
Need help getting the file through this method. Or is there an easier way to get a file from the Parse Cloud? All help is appreciated.
In my experience, a null pointer exception on line containing the following:
ParseFile fileObject = (ParseFile) object.get("picture");
tells me that object is null during the attempt to access the get() method. This tells me that your query is either malformed or has no matching objects on the server, more likely the second.
I don't have any information regarding the data structure of your Parse cloud, so let me give you an example instead.
If you have a regular old user, and his Parse ObjectId is xyz123ab and he has a picture file under the key/column profilePicture. You have a User containing a ParseFile containing picture data, or [User [File [PictureData]]].
Here's how you would retrieve the above in code:
String objectIdOfUser = "xyz123ab";
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getInBackground(objectIdOfUser, new GetCallback<ParseUser>() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (e == null) {
ParseFile picture = parseUser.getParseFile("profilePicture");
if (picture == null) {
// no parseFile in column "profilePicture" in this user.
return; // there will be no data to retrieve
}
picture.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] data, ParseException e) {
if (e == null) {
if (data.length == 0) {
// data found, but nothing to extract. bad image or upload?
return; // failing out
}
// SUCCESS
// convert data and display in an imageview
} else {
// ParseFile contained no data. data not added to ParseFile?
}
}
});
} else {
// no users had this objectId
}
}
});
Change ParseUser to ParseObject in the following if you aren't using a ParseUser. Don't forget to change the string "ObjectName" to your object.
// ... etc
ParseQuery<ParseObject> query = ParseObject.getQuery("ObjectName");
query.getInBackground(objectIdOfObject, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
ParseFile picture = object.getParseFile("profilePicture");
// etc...
Also, be certain that your data is loaded into the server properly. This can be tricky to determine on the web-client for Parse, unfortunately. Uploading an image properly is similar to this but in reverse, and an answer for another day.
Please review your code to get an objectId as shown here:
objects.get(i).getObjectId()
to determine if you are indeed accessing the objectId you thought you were, perhaps with a log or toast or debug breakpoint. That could be were everything starts to break down.
Your code looks fine, you simply need to check if the current row has a file reference in it's picture column. If the column is empty then you will obviously get a null reference when reading it out as a ParseFile.

com.parse.ParseException: Clients aren't allowed to perform the find operation on the installation collection

I am new to parse ,i am able to store data to its database but whenever i try to retrieve some information like object-id from database i usually get the above mentioned error.Query which i am using to retrieve the object-id corresponding to the given Friend-id.
ParseQuery<ParseObject> query = ParseQuery.getQuery("_Installation");
query.whereEqualTo("FriendId", FrndTextString);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> results, ParseException e) {
if (e == null) {
for (ParseObject x : results) {
String Objectid = x.getString("objectId");
objectidEditText.setText(Objectid);
}
} else {
Toast.makeText(getApplicationContext(),
"Error in retreving Data", Toast.LENGTH_LONG)
.show();
}
}
});
Please help me in solving this issue as I googled a lot but haven’t found anything good solution.Any help will be appreciated.
Thanks.
The _Installation class is an internal class from Parse. The exception occurs because you are trying to access data that is protected by a specific user with specifc ParseACL .
Documentation from Parse:
Every Parse application installed on a device registered for push notifications has an associated Installation object. The Installation object is where you store all the data needed to target push notifications. For example, in a baseball app, you could store the teams a user is interested in to send updates about their performance. Saving the Installation object is also required for tracking push-related app open events.
In Android, Installation objects are available through the ParseInstallation class, a subclass of ParseObject. It uses the same API for storing and retrieving data. To access the current Installation object from your Android app, use the ParseInstallation.getCurrentInstallation() method. The first time you save a ParseInstallation, Parse will add it to your Installation class and it will be available for targeting push notifications.
Try to use another Class, like "Ranking". Ex:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Ranking");
query.whereEqualTo("FriendId", FrndTextString);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> results, ParseException e) {
if (e == null) {
for (ParseObject x : results) {
String Objectid = x.getString("objectId");
objectidEditText.setText(Objectid);
}
} else {
Toast.makeText(getApplicationContext(),
"Error in retreving Data", Toast.LENGTH_LONG)
.show();
}
}
});
You have to pay attention to setup ParseACL correctly for each user and Class, if you want to find public information of others users. At least, all user have to be "Public Read" for data that you want.
Before saving any data in Parse, you have to setup the environment with this tutorial. After, you have to Create the ParseUser and login with it. Parse has an automatic way to create an user, just calling:
ParseUser.enableAutomaticUser();
ParseUser.getCurrentUser().increment("RunCount");
ParseUser.getCurrentUser().saveInBackground();
To save any data in Parse, you can use:
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground();
Parse Server will automatically create the new class "GameScore", if is not exist. And will create all column, like "score", "playerNAme" and "cheatMode" if it not exist.
To go through with Parse, please read API Doc, it is so helpful.
The Installation class can't be queried like that. Use ParseInstallation.getCurrentInstallation(); instead.
for example,
ParseInstallation.getCurrentInstallation().getString("FriendId");

How to get latest updated parse object in android?

I don't know about parse and I want to learn it.I want to know how can I get the latest updated parse object in parse.I tried to use get first in background but I think there is a problem in my ParseQuery.Please provide me the right way of query to get the latest object I just pushed on the parse cloud back.
ParseQuery<ParseObject> query=ParseQuery.getQuery("RBSE");
query.whereEqualTo("roomA","900");
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject objectLatest, ParseException e) {
if(e==null){
}
else{
}
}
}
To get the most recently created/modified, simply sort descending by the "updatedAt" field (it is built-in).
ParseQuery<ParseObject> query = ParseQuery.getQuery("RBSE");
query.orderByDescending("updatedAt");
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("RBSE", "The getFirst request failed.");
} else {
// got the most recently modified object... do something with it here
}
}
});

table not updating in parse

I am trying to run a function to update some data in an existing table in parse.com. Below is the function:
private void saveMeToDBnCloud(final LocModel aLocObj)
{
db.addLoc(aLocObj);
ParseQuery<ParseObject> query = ParseQuery.getQuery("tblxxx");
query.getInBackground(myIdInCLoud, new GetCallback<ParseObject>() {
public void done(ParseObject upObj, ParseException e) {
if (e == null)
{
upObj.put("lat", "1");
upObj.put("lng", "2");
upObj.put("geoTime", "3");
upObj.saveInBackground();
Log.d("svcloud","inside func::");
}
else
{
Log.d("svcloud","error::" + e);
}
}
});
}
Note: all columns in the table are String type.
The function is getting called and also getting inside if condition, as its showing the log. But data isn't updating in parse.com.
Any solutions?
SOLVED!!
Just wrote the access permission to write for ACL and its working fine. :-)

Categories

Resources