How to get latest updated parse object in android? - 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
}
}
});

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.

Storing/Getting values in Parse

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

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.

my query.findInBackground not getting executed in android

I am using parse for my data in android app. for some reason the code inside query.findInBackground is not getting executed.
public List<Date> sessionHeaderFetch(){
Log.d("test", "session fetch entry");
ParseQuery<Sessions> query = ParseQuery.getQuery(Sessions.class);
final List<Date> sessionHeaders = null;
query.findInBackground(new FindCallback<Sessions>() {
#Override
public void done(List<Sessions> sessionsObjects, com.parse.ParseException e) {
Log.d("test", "session internal entry");
if (e == null) {
Log.d("test", "Retrieved " + sessionsObjects.size() + " sessions");
for (Sessions session : sessionsObjects) {
sessionHeaders.add(session.getNetsDate());
};
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
Log.d("test", "session last value");
return sessionHeaders;
}
the code inside public void done() is not all invoked.
I don't think you have understood how to query correctly in Parse.
When you define the parse query. The get query should contain the name of the table that you are trying to query. Also the queries returned will be ParseObjects normally, so I would expect that your callback should be new FindCallback().
I've adjusted the parse query below.
ParseQuery<Sessions> query = ParseQuery.getQuery("ParseTableName");
final List<Date> sessionHeaders = null;
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> sessionsObjects, com.parse.ParseException e) {
Log.d("test", "session internal entry");
if (e == null) {
// Find succeeded, so do something
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
Obviously you will need to replace "ParseTableName" with the name of the table that you are trying to query in parse.
I actually solved it by using an ArrayList, there were some problems with the List initialization and also the results are fetched asynchronous manner so my functions which calls these functions were getting null values so I had to write by calling functions that calls the parse fetch functions asynchronously as well.
but the query can be written like this and it works.
public void sessionFetch(Date headers, final ParseIOListener<Sessions> listener){
ParseQuery<Sessions> query = ParseQuery.getQuery(Sessions.class);
Log.d("test", "input header" + headers );
query.whereEqualTo("NetsDate",headers);
final ArrayList<Sessions> sessionsData = new ArrayList<Sessions>();
query.findInBackground(new FindCallback<Sessions>() {
#Override
public void done(List<Sessions> sessionsObjects, com.parse.ParseException e) {
if (e == null) {
for (Sessions session : sessionsObjects) {
Log.d("test", "output objects" + session.toString() );
sessionsData.add(session);
Log.d("test", "Retrieved -- sessions" + sessionsObjects.size() );
}
listener.onDataRetrieved(sessionsData);
} else {
listener.onDataRetrieveFail(e);
}
}
});
}
If you want more details on implementing this, check this link
The reason why you feel the code is not being invoked is because you are returning from the method before the ".findInBackground" operation has completed. Remember the query is performed on a background thread. So this is how your code will run:
ParseQuery query = ParseQuery.getQuery(Sessions.class);
final List sessionHeaders = null;
------> 1. query.findInBackground (Popped onto background thread)
return sessionHeaders; (by the time you get here, sessionHeaders will probably still be null).
So change the logic of the code and wait till the callback returns before doing any processing on the "sessionHeaders" object.

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