NSDate与Parse Data相比没有按预期查询记录

时间:2015-10-17 15:36:16

标签: swift parsing nsdate

我有一个专栏" checkInTime"在我的Parse类中,类型为Date。

我试图在一段时间后查询任何checkInTimes

以下是我正在使用的代码

func reloadCheckInView() {

    checkIns.removeAll(keepCapacity: true)

    let friendsListQuery = PFQuery(className: "Friends")
    friendsListQuery.includeKey("friendId")
    friendsListQuery.includeKey("user")
    friendsListQuery.whereKey("friendId", equalTo: PFUser.currentUser()!)
    friendsListQuery.whereKey("approved", equalTo: true)

    friendsListQuery.findObjectsInBackgroundWithBlock {(objects:[PFObject]?, error:NSError?) -> Void in
        if error == nil {
            for object in objects! {
                let user = object["user"] as! PFObject
                let now = NSDate()
                print(now)

                let BeforeNow = NSDate().dateByAddingTimeInterval(self.hourWindow * -3600)

                print(self.hourWindow)
                print(BeforeNow)

                let checkInQuery = PFQuery(className: "LocationCheckIn")
                checkInQuery.includeKey("user")
                checkInQuery.whereKey("CheckInTime", greaterThan: BeforeNow)
                checkInQuery.whereKey("user", equalTo: user)

                //let subQuery = PFQuery.orQueryWithSubqueries([friendsListQuery, checkInQuery])
                checkInQuery.findObjectsInBackgroundWithBlock {(results: [PFObject]?, error: NSError?) -> Void in
                    if error == nil {
                        //print(results)
                        for result in results! {
                            self.checkIns.append(result)
                        }
                        print(self.checkIns)

                    }
                    else {

                    }
                }

            }

        }
        else {
            print(error)
        }
    }
}

和我的打印显示计算正在按预期工作..在这种情况下1小时之前。

2015-10-17 15:23:49 +0000
1.0
2015-10-17 14:23:49 +0000
[]

并且在我的查询中找不到任何记录。

如果我发表评论

checkInQuery.whereKey("CheckInTime", greaterThan: BeforeNow)

打印记录,您可以看到checkIn时间应该通过我的查询,但不是。有什么想法吗?

[<LocationCheckIn: 0x12903edb0, objectId: M7zTD3qiCL, localId: (null)> {
   checkInPlaceId = "ChIJfc7vF8upVogRk20hK-LzaAM";
   checkInPlaceName = "12434 Willingdon Rd";
   checkInTime = "2015-10-17 14:26:00 +0000";
   location = "<PFGeoPoint: 0x127ed52f0, latitude: 35.436804, longitude: -80.828736>";
   user = "<PFUser: 0x127ed76d0, objectId: WCMmKTvNUC, localId: (null)>";
}]

1 个答案:

答案 0 :(得分:0)

正如我刚读完自己的问题,我看到了我的错误......一个错字

checkInQuery.whereKey("CheckInTime", greaterThan: BeforeNow)

应该是

checkInQuery.whereKey("checkInTime", greaterThan: BeforeNow)