菜鸟教程小白 发表于 2022-12-11 17:12:02

ios - 更新当前用户的解析对象而不是创建重复条目


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

<pre><code>class DropLoc: UIViewController, CLLocationManagerDelegate {

var user = PFUser.currentUser()
var post = PFObject(className:&#34;Post&#34;)
var query = PFQuery(className:&#34;Post&#34;)
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(&#34;user&#34;, equalTo: user!)
    query.findObjectsInBackgroundWithBlock {(objects: ?, error: NSError?) -&gt; Void in
      if error != nil {
            print(error)
      }else{
            self.post[&#34;user&#34;] = self.user ?? NSNull()
            self.post[&#34;location&#34;] = 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]
查看完整版本: ios - 更新当前用户的解析对象而不是创建重复条目