ios - 更新当前用户的解析对象而不是创建重复条目
<p><p>我试图让用户更新他们的位置。当我尝试保存数据时,它会创建一个重复条目,而不是更新旧信息。我有一个部署到 mongolabs 数据库的 heroku 解析服务器。</p>
<pre><code>class DropLoc: UIViewController, CLLocationManagerDelegate {
var user = PFUser.currentUser()
var post = PFObject(className:"Post")
var query = PFQuery(className:"Post")
var point:PFGeoPoint!
let porta = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
var curGate = porta.location
self.porta.delegate = self
self.porta.desiredAccuracy = kCLLocationAccuracyBest
self.porta.requestWhenInUseAuthorization()
self.porta.startUpdatingLocation()
point = PFGeoPoint(location: curGate)
query.whereKey("user", equalTo: user!)
query.findObjectsInBackgroundWithBlock {(objects: ?, error: NSError?) -> Void in
if error != nil {
print(error)
}else{
self.post["user"] = self.user ?? NSNull()
self.post["location"] = self.point ?? NSNull()
self.post.saveInBackground()
}
}
}
</code></pre>
<p>}</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>看来 <code>point</code> 只设置了一次。尝试从 <a href="https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/" rel="noreferrer noopener nofollow">CLLocationManagerDelegate</a> 添加以下方法协议(protocol)。</p>
<pre><code>func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
// updated coordinate
point = PFGeoPoint(location: manager.location!)
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 更新当前用户的解析对象而不是创建重复条目,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38816863/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38816863/
</a>
</p>
页:
[1]