1

I a retriving data from parse using a PFQuery, like this

var downloadQuery = PFQuery(class: "MyDatabase") 

 downloadQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

//retriving data her
                } 

But is there a way that I can filter out objects created more than a week ago, or something like that?

I have used downloadQuery.whereKey("price", equalTo: "Free") to filter out certain objects but I can't figure out how to filter out objects based on the dates when they were created

2 Answers 2

1

You can use Parse's greaterThan, lessThan, greaterThanOrEqualTo, lessThanOrEqualTo with a date to filter your query.

Eg:

var downloadQuery = PFQuery(class: "MyDatabase") 
downloadQuery.whereKey("createdAt", lessThanOrEqualTo: lastWeek)

lastWeek is a date object which you can calculate using @yesthisisjoe's answer above. This will only fetch objects created within a week from the current day so no extra filtering will be required on your app side.

Sign up to request clarification or add additional context in comments.

Comments

1

You can start by using downloadQuery.orderByDescending("createdAt") to sort the results by the date they were created.

Then you can do something like:

let calendar = NSCalendar.currentCalendar()
let lastWeek = calendar.dateByAddingUnit(.Week, value: -1, toDate: NSDate(), options: [])

for object in objects {
    if object.createdAt!.timeIntervalSinceDate(lastWeek).isSignMinus {
        //it's older than a week, so stop iterating through objects
        break
    }
    //it's within the past week, do something with the object
}

Comments

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.