OGeek|极客世界-中国程序员成长平台

标题: ios - 更新当前用户的解析对象而不是创建重复条目 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:12
标题: ios - 更新当前用户的解析对象而不是创建重复条目

我试图让用户更新他们的位置。当我尝试保存数据时,它会创建一个重复条目,而不是更新旧信息。我有一个部署到 mongolabs 数据库的 heroku 解析服务器。

class DropLoc: UIViewController, CLLocationManagerDelegate { 

var user = PFUser.currentUser()
var post = PFObject(className:"ost")
var query = PFQuery(className:"ost")
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: [PFObject]?, 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()

        }
    }
}

}



Best Answer-推荐答案


看来 point 只设置了一次。尝试从 CLLocationManagerDelegate 添加以下方法协议(protocol)。

func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
        // updated coordinate
        point = PFGeoPoint(location: manager.location!)        
}

关于ios - 更新当前用户的解析对象而不是创建重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38816863/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4