FireBase和位置的一些问题 - 选项,位置和用户保存

时间:2016-05-04 02:07:36

标签: swift firebase cllocationmanager cllocation

我正在制作跟踪注册用户的ap,这将主要用于狩猎。

我遇到的主要问题是当我保存一个位置时,当我只想让它成为“用户名”时,它会在firebase中创建一个名为Optional“User”的用户,我尝试在此代码中展开currUsername中的可选节目但是,如果我这样做,它保存正确,但不允许立即保存位置,并说当打开可选的currUsername时它找到nil!

这是我的代码我遇到了问题:

  @IBAction func getCoords(sender: AnyObject)
{
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()
    locationManager.startMonitoringSignificantLocationChanges()

}

@IBAction func logoutButton(sender: AnyObject)
{
    let myRootRef = Firebase(url: "https://hunttracker.firebaseio.com/");
    myRootRef.unauth();
    self.performSegueWithIdentifier(self.logout, sender: nil)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

    let latValue = locationManager.location!.coordinate.latitude
    let lonValue = locationManager.location!.coordinate.longitude

    LatLabel.text = String(latValue)
    LongLabel.text = String(lonValue)
    let myRootRef = Firebase(url: "https://hunttracker.firebaseio.com/users/\(**nameSingleton.currUsername!)"**);

    let usersRoot = myRootRef.childByAppendingPath("Location")
    let geoFire = GeoFire(firebaseRef: usersRoot.childByAppendingPath("LocationData"))

   geoFire.setLocation(CLLocation(latitude: lonValue, longitude: latValue), forKey: "Values"){ (error) in
        if (error != nil)
        {
            print("error \(error)")
        }
        else
        {
            print("Saved location")
            self.pintomap()
        }
    }

}

另外还有一个问题是,如何离线保存表格视图,以便从firebase中提取的数据保留在那里,并在用户更改位置时进行更新?如果有人有想法,我会把表格代码放在下面,但第一个问题是我的主要问题。

  @IBAction func addMember(sender: AnyObject)
{
    let alert = UIAlertController(title: "Add Party Member",
        message: "Add Party Member",
        preferredStyle: .Alert)

    let saveAction = UIAlertAction(title: "Add",
        style: .Default)
        { (action: UIAlertAction) -> Void in
             nameSingleton.addedmember = alert.textFields![0].text
            //query if party member exists 

            self.myRootRef.observeEventType(.ChildAdded, withBlock:
                {

                    snapshot in


                    //print(snapshot.key)
                    var memberslist = [String]()
                    memberslist.append(snapshot.key)
                    for(var i = 0; i < memberslist.count; i++)
                    {
                      if(memberslist[i] == nameSingleton.addedmember)
                      {
                        nameSingleton.partymembers.append(nameSingleton.addedmember!)
                        }
                    }
                                })
            self.getdata()
            nameSingleton.currUsername = alert.textFields![0].text

        }

    let cancelAction = UIAlertAction(title: "Cancel",
        style: .Default) { (action: UIAlertAction) -> Void in
    }

    alert.addTextFieldWithConfigurationHandler {
        (textName) -> Void in
        textName.placeholder = "Enter your Username"
    }

    alert.addAction(saveAction)
    alert.addAction(cancelAction)

    presentViewController(alert,
        animated: true,
        completion: nil)
}

感谢无论谁提供帮助

0 个答案:

没有答案