晚上好,程序员们!
我创建了一个查询来获取我周围距离设备位置7英里的人的用户名和位置,我想每5-10秒更新一次这些对象(我还没有决定)。这方面的最佳实践是什么?我应该创建一个NSTimer()并以这种方式调用它吗?请帮帮我!!
var timer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
timer == NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "queryFunc", userInfo: nil, repeats: true)
}
func queryFunc() {
var query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
NSLog("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
for object in objects { NSLog("%@", object.objectId)
}
} else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
/*
This is just an example query, the point of this is the timer and how to update objects periodically
*/
}发布于 2015-05-07 15:03:00
尝试在查询中添加whereKey:nearGeoPoint:withinMiles:,该查询将查找距离您的位置7英里内的对象
For further details for that check out this link
当您在每次查询后找到数据时,则删除所有以前的数据,并用新数据替换它,然后刷新视图。
NSTimer可能是个不错的选择。甚至我也使用计时器在后台每隔5分钟同步一次,它对我来说工作得很好。
希望这能对你有所帮助。
https://stackoverflow.com/questions/30093996
复制相似问题