调用最新的PFObject

时间:2015-10-30 18:03:49

标签: swift parse-platform pfquery pfobject

我有一个计时器,将当前日期倒计时到我在Parse中设置的日期(classname:" Date")。现在,它使用我设置的ObjectId检索该数据。我想将它设置为调用班级中最近更新的对象,使用" updatedAt"中的日期。柱。我怎样才能做到这一点?

    super.viewDidLoad()

    //      Retrieve the date in Parse

    var query = PFQuery(className:"Date")
        query.getObjectInBackgroundWithId("CSJFqvJDqs") {
            (dateFromParse: PFObject?, error: NSError?) -> Void in
            if error == nil && dateFromParse != nil {
                // Process everything here

                // Step 1: Get the current date.
                let currentDate = NSDate()

                // Step 2: Convert the dateFromParse PFObject into a NSDate object
                let electionDateComponents = NSDateComponents()
                electionDateComponents.day = dateFromParse!["Day"] as! Int
                electionDateComponents.month = dateFromParse!["Month"] as! Int
                electionDateComponents.year = dateFromParse!["Year"] as! Int
                let electionDate = NSCalendar.currentCalendar().dateFromComponents(electionDateComponents)!

                // Step 3: Find the difference between dateFromParse and currentDate
                let timeToElection = electionDate.timeIntervalSinceDate(currentDate)
                let daysToElection = (((timeToElection/60)/60)/24)
                print(daysToElection)

                //              Step 4: Display the number of days of difference on countDown.text
                let daysText =  String(format: "%.0f days", floor(daysToElection))
                self.countDown.text = daysText
            } else {
                print(error)
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您应该浏览SDK文档和指南:

https://parse.com/docs/ios/api/

https://parse.com/docs/ios/guide#queries

查询有排序方法。你想要按照

的方式做点什么
[query sortDescending:@"updatedAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *dateObject, NSError *error)
{
    //do your stuff
}];

抱歉,刚刚意识到我给了你obj-c语法,而你正在使用swift。我不熟悉swift,但方法应该是一样的。只需称它们为Swift风格。

答案 1 :(得分:0)

想出来!首先,我必须在Parse" electionDate"中创建选举日期。然后我必须创建currentDate常量,以便我可以将它用作" greaterThanOrEqualTo"的参考。

let date = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let currentDate = cal.startOfDayForDate(date)

    let query = PFQuery(className:"Date")
    query.whereKey("electionDate", greaterThanOrEqualTo: currentDate)
    query.orderByAscending("electionDate")
    query.getFirstObjectInBackgroundWithBlock {