Retrieving data after modifying data at cloud code - android

I'm trying to get Parse server date and time but I get to know the only way is to create or update an object then retrieve the value of updatedAt.
My class
//There is only one data/object in this class
Class Test{
int times
};
My cloud code
increment of value "times" is to get current server date and time via getUpdatedAt();
Parse.Cloud.define("updateTimes", function(request, response) {
var test = Parse.Object.extend("Test");
var query = new Parse.Query(test);
query.first({
success: function(results) {
results.increment("times");
results.save();
response.success("success updated");
},
error: function() {
response.error("test lookup failed");
}
});
});
My calling at android studio
//Call updateTimes function at cloud code
ParseCloud.callFunctionInBackground("updateTimes", new HashMap<String, Object>(), new FunctionCallback<String>() {
public void done(String result, ParseException e) {
if (e == null) { //success }
}
});
//Get date and time from server
ParseQuery<ParseObject> query = ParseQuery.getQuery("Test");
query.getInBackground("i6hmOEItvI", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// success
serverDate = object.getUpdatedAt();
Format formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
date = formatter.format(serverDate);
Log.d("ServerDate",date);
}
});
My problem is the serverDate retrieved from objectId is not correct. The date retrieved is always the older one. However, the updateAt field in database is successfully updated.
I have called ParseCloud.callFunctionInBackground before calling query object.getUpdatedAt();.
Any has any idea why the date I get is not the latest?

It seems a waste to create or touch data just to see what date stamp was written. Why not return a date directly:
Parse.Cloud.define("serverDateTime", function(request, response) {
response.success({dateTime: new Date()});
});

Related

Retrieve data from the class table using parse.com

I want to retrieve value from the class of the object.
example: I have one class Player_info.
In this class there is different field like Player name,Score,birthdate.
1.messi,50,7/12/1991.
2.ronaldo,45,7/7/1993.
3.rooney,40,7/12/1991.
now i want to retrieve data ronaldo score which is from this class using parse i was stuck over here from 2 day. plz guys help me out. It was very appreciable.
You need to do like this :
String ObjectId;
ParseQuery<ParseObject> playerQuery = ParseQuery.getQuery("Player_info");
playerQuery.whereEqualTo("Player Name", "ronaldo");
playerQuery.setLimit(1);
playerQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> playerList, ParseException e) {
if (playerList != null && playerList.size() > 0) {
int score = playerList.get(0).getInt("Score");
objectId = playerList.get(0).getObjectid();
}
}
}
Now you can use score of ronaldo wherever you want :) .
To update the value in table you can do that:
// Create a pointer to an object of class Point with id dlkj83d
ParseObject point = ParseObject.createWithoutData("Player_Info", objectId);
// Set a new value on quantity
point.put("Score", newScore);
// Save
point.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
// Saved successfully.
} else {
// The save failed.
}
}
});

Returns zero for all values retrieved from the parse database

I'm using parse backend to store and retrieve the datas for my android app, the storing gets done properly but i have problem in retrieving it. I just went through the parse documentation to retrieve the result but what i get is just 0 for all the retrieved values..im suret that the class exists in the parse cloud with valid values but still i get 0 for all the queries.. this is my code to save:
Toast.makeText(getApplicationContext(),"writing to parse",Toast.LENGTH_SHORT).show();
ParseObject dataObject = new ParseObject("Score");
dataObject.put("correct",correctAnswers);
dataObject.put("wrong",wrongAnswers);
dataObject.put("percent", percentage);
dataObject.saveInBackground();
this is how i get back the saved data
ParseQuery<Score> query = ParseQuery.getQuery("Score");
try {
List<Score> scoreList = query.find();
} catch (ParseException e) {
e.printStackTrace();
}
query = ParseQuery.getQuery("Score");
final Activity ctx = this;
query.findInBackground( new FindCallback<Score>() {
#Override public void done(List<Score> scoreList, ParseException e) {
if ( e == null ) {
ParseObject dataObject = ParseObject.create("Score");
int p = dataObject.getInt("correct");
int q = dataObject.getInt("wrong");
int r = dataObject.getInt("percent");
Toast.makeText(ExamRecordActivity.this,String.valueOf(p),Toast.LENGTH_SHORT).show();
Toast.makeText(ExamRecordActivity.this,String.valueOf(q),Toast.LENGTH_SHORT).show();
Toast.makeText(ExamRecordActivity.this,String.valueOf(r),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ctx,
"Error updating questions - please make sure you have internet connection",
Toast.LENGTH_LONG).show();
}
}
});
Inside the done method you are creating a new by calling ParseObject dataObject = ParseObject.create("Score"); and then trying to read values from it without putting any in.
I don't know what the structure of your class is but you need to be iterating through List<Score> scoreList in order to get the queried data.

A different way to retrieve an Object in Parse with Android Studios

Currently I am making an Android application with Android Studios and I am using Parse to handle the data. I want to be able send (put) data such as town names and info about that town (town_names would be a column and info would be another, there for each town_names would have its own info). I can do that with ease and it uploads to the Parse database. However, the part I can not seem to figure out is how to retrieve the data points I desire. I have code to retrieve data based off of an objectId shown below however that is not exactly what I want.
ParseQuery<ParseObject> query = ParseQuery.getQuery("userMessage");
query.getInBackground("KgsLojXPcq", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
retrievedText = object.getString("info");
} else {
retrievedText = "No information.";
}
}
});
"userMessage" is the ParseObject I have created and "info" is the column I want to get text from. Instead of searching for an objectId ("KgsLojXPcq") is there a way to search for town_names? So then I could for example search the database for "New York" and "retrievedText" would be set to the info for New York.
ParseQuery
.getQuery("userMessage")
.whereEqualTo("town_names", "New York")
.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
retrievedText = object.getString("info");
} else {
retrievedText = "No information.";
}
}
});

Getting data from Parse query

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.

Parse query.whereEqualTo String issue

I am using Parse backend for my android app. I need to query the database for a record that has a field with a specific string value. following is the Parse code
strObjectId is a String initialised with a Parse ObjectId as follows
String strObjectId = MyParseObject.getObjectId();
Parse code
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyDataTable");
query.whereEqualTo("code", Code);
query.getFirstInBackground(new GetCallback<ParseObject>()
{
public void done(ParseObject object, ParseException e)
{
if (object == null)
{
return;
}
else
{
ShowToast("Record found!");
}
}
});
The problem is that it works perfectly okay when 'Code' is hardcoded as follows prior to running above query
String Code = "krErZgz9Is";
But it DOES NOT work when Code is assigned Parse ObjectId as follows
String Code = strObjectId;
Obviously the Data Table does have a record with 'code' field with value 'krErZgz9Is'
Your help will be appreciated
Thanks
Have you tried to print out the strObjectId? If your object is not yet saved in Parse, or the object is not loaded from Parse. It will not have an object Id.
Also, have you tried to check the equality with the objectId directly?
query.whereEqualTo("code", MyParseObject.getObjectId());
I've got this working for me
I created an EditText and contained the text I want to find a match in my query. Just try to hide it in the layout if you don't really want to show it.
EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(strObjectId);
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyDataTable");
query.whereEqualTo("code", editText.getText().toString());
query.getFirstInBackground(new GetCallback<ParseObject>()
{
public void done(ParseObject object, ParseException e)
{
if (object == null)
{
return;
}
else
{
ShowToast("Record found!");
}
}
});
I don't get it why removing the getText() function doesn't work so you better keep it this way.

Categories

Resources