2015-05-07 40 views
0

晚上好!Swift语言:更新分析对象

我创建了一个查询,获取用户名和设备位置7miles内我周围的人的位置,我想更新这些对象每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 

    */ 

    } 

回答

0

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

For further details for that check out this link

,当你每次查询后发现,数据则删除所有以前的数据并将其替换为新数据并刷新您的视图。

NSTimer可能是它的好选择。即使我也使用计时器在每5分钟的背景中同步,它对我来说工作正常。

希望这会帮助你。

+0

我正在使用的查询做了所有这些,我只是使用了解析的示例查询,我的问题的关键是我应该如何定期更新对象,我应该创建一个计时器并以这种方式更新它们?还是有更实际的方法? –

+0

查看我在回答中的编辑 –