0

晚上好编码员!

我创建了一个查询,获取设备位置 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

  */

  }
4

1 回答 1

0

尝试添加 whereKey:nearGeoPoint:withinMiles: 在您的查询中,它将找到距您所在位置 7 英里内的对象

有关详细信息,请查看此链接

当您在每次查询后找到数据时,删除所有以前的数据并将其替换为新数据并刷新您的视图。

NSTimer 可能是不错的选择。甚至我也使用计时器每 5 分钟在后台同步一次,这对我来说效果很好。

希望这会帮助你。

于 2015-05-07T07:02:59.953 回答