Swift 3 - Firebase:不能保存到数据库

时间:2017-06-11 22:17:15

标签: ios swift firebase firebase-realtime-database

我对当前应用有疑问。当我想保存到Firebase数据库时没有任何反应。我找不到我的错误,甚至打印出“无法保存帖子到DB”。有人能找到我的错误吗?我不太擅长编程和Swift 3 ......这段代码我从另一个项目中复制了它并且它在那里工作所以我真的很困惑......?帮助将不胜感激!

import UIKit
import Firebase
import MapKit

class MapAddPostHeader: UICollectionViewCell, UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate, MKMapViewDelegate {

var locationData: CLLocation!
let locationManager = CLLocationManager()

 lazy var sharePositionButton: UIButton = {
    let button = UIButton(type: .system)
    button.setImage(#imageLiteral(resourceName: "Plus_Unseleted"), for: .normal)
    button.addTarget(self, action: #selector(handleShare), for: .touchUpInside)
    return button

}()

func handleShare() {


    self.sendToDatabase()

}

fileprivate func sendToDatabase() {
    guard let uid = FIRAuth.auth()?.currentUser?.uid else { return }
    guard let locationdata = locationData else { return }

    let userPositionRef = FIRDatabase.database().reference().child("position").child(uid)
    let ref = userPositionRef.childByAutoId()
    let timestamp = NSNumber(value: Int(Date().timeIntervalSince1970))
    let fromId = FIRAuth.auth()?.currentUser?.uid
    let values = ["latitude" : (locationdata.coordinate.latitude), "longitude" : (locationdata.coordinate.longitude), "fromId" : fromId ?? "", "timestamp" : timestamp] as [String : Any]
    ref.updateChildValues(values) { (err, ref) in

        if let err = err {
            print("Failed to save post to DB", err)
            return
        }

        print("Successfully saved position to DB")

    }

}

0 个答案:

没有答案