0

I have an application where posts have a limited lifetime. Every like adds life to the post.

in my table I have a field initialDeletionDate and a counter that tracks the number of likes. In my newsfeed, I'm trying to query objects, but I only want to get objects that still alive.

So basically, what I want to do is get all object where:

initialDeletionDate + counter*time < [NSDate date]
//time = 1200 sec

something like...:

[query whereKey:@"initialDeletionDate" lessThan:[NSDate date] - "key: counter"];

How can I do this?

1 Answer 1

1

I would recommend adding a 3rd field to your class that has the final deletion date. You could either update your client code or create a cloud method that sets the value of this date to initialDeletionDate + (counter*1200).

Then when you query you can just ask for records that haven't reached their calculated deletion date yet:

[query whereKey:@"calculatedDeletionDate" greaterThan:[NSDate date]];

Update:

Here's a starting point for a before-save Cloud Function.

// include Moment library for easier date handling
var moment = require("moment");

Parse.Cloud.beforeSave("YourClassNameHere", function(request, response) {
    var yourClass = request.object;

    if (yourClass.dirty("initialDeletionDate")
        || yourClass.dirty("counter"))
    {
        // recalculate
        var initialDate = yourClass.get("initialDeletionDate");
        var counter = yourClass.get("counter");
        // use Moment library to manipulate the date
        var calculatedDate = moment(initialDate)
            .add('minutes', counter * 2)
            .toDate();
        yourClass.set("calculatedDeletionDate", calculatedDate);
    }
    response.success();
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer Timothy. Do I call the cloudcode method from the iOs client? If multiple users increment the counter at the same time will the "calculatedDeletionDate" be accurate?
@SanchoSanchez If you create a before-save Cloud Function then it will be able to keep the calculation updated properly. I'll add a sample to my answer for you
that is really nice of you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.