Roduck Nickes Roduck Nickes - 6 months ago 16
iOS Question

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?

Answer

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?