user1853149
user1853149

Reputation: 23

Retrieving data after modifying data at cloud code

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?

Upvotes: 2

Views: 341

Answers (1)

danh
danh

Reputation: 62686

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()});
});

Upvotes: 2

Related Questions