Roduck Nickes
Roduck Nickes

Reputation: 1021

Query objects newer then 24 hours

So the way Snapchat has, they only show in story images/videos that are newer then 24 hours old. How can I do the same using this query code:

func queryStory(){
        let query = PFQuery(className: "myClass")
        query.whereKey("isPending", equalTo: false)
        query.limit = 1000
        query.orderByDescending("createdAt")

        query.findObjectsInBackgroundWithBlock { (posts: [PFObject]?, error: NSError?) -> Void in
            if (error == nil){
                // Success fetching objects

                for post in posts! {

                    if let imagefile = post["userFile"] as? PFFile {
                        self.userFile.append(post["userFile"] as! PFFile)
                        self.objID.append(post.objectId!)
                        self.createdAt.append(post.createdAt!)
                    }
                }
            }
            else{
                print(error)
            }
        }
    }
}

I tried adding:

var date: NSDate = NSDate(timeIntervalSinceNow: -172800)
query.whereKey("createdAt", greaterThan: date)

But this gave me 0 items.(My class has currently total 28 items). Any ideas?

Upvotes: 0

Views: 60

Answers (1)

Erik Auranaune
Erik Auranaune

Reputation: 1414

The correct format for 24 hours would be:

let date: NSDate = NSDate(timeIntervalSinceNow: -86400)
query.whereKey("createdAt", greaterThan: date)

How old are the files you are trying to query from Parse?

Upvotes: 1

Related Questions