无法访问Firebase子

时间:2017-05-18 20:07:17

标签: ios swift firebase firebase-realtime-database jsqmessagesviewcontroller

我有一个firebase数据库参考

import UIKit
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var map: MKMapView!

    var manager: CLLocationManager!


    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

        let uilpgr = UILongPressGestureRecognizer(target: self, action:Selector(("action:")))
        uilpgr.minimumPressDuration = 2.0
        map.addGestureRecognizer(uilpgr)

    }

    func action(gestureRecognizer:UIGestureRecognizer){
        if gestureRecognizer.state == UIGestureRecognizerState.began{
        let touchPoint = gestureRecognizer.location(in: self.map)
            let newCoordinate = self.map.convert(touchPoint, toCoordinateFrom: self.map)
            let annotation = MKPointAnnotation()

            annotation.coordinate = newCoordinate
            annotation.title = "Meu lugar"
            self.map.addAnnotation(annotation)



        }
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0]
        let latitude = userLocation.coordinate.latitude
        let longitude = userLocation.coordinate.longitude
        let coordinate = CLLocationCoordinate2DMake(longitude,latitude)
        let latDelta: CLLocationDegrees = 0.01
        let lonDelta: CLLocationDegrees = 0.01
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)
        self.map.setRegion(region, animated: false)

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

我想从DisplayViewController中的firebase数据库访问ReceiverName,但是当我调用数据库引用时,由于convoId我收到错误。这是convoId的声明方式(在MessagesViewController中)。

FIRDatabase.database().reference().child("messages").child("\(self.convoId!)").childByAutoId()

receiverData从UserviewController传递到MessagesViewController,“senderId”是唯一标识当前用户发送消息的字符串标识符,并在JSQMessagesViewController.h中自动声明。所以基本上,我不能在我的DisplayViewController中重新声明convoId。但是,在DisplayViewController中,我需要访问ReceiverName。

这是我尝试检索ReceiverName的方式:

 override func viewDidLoad() {
        super.viewDidLoad()
  let receiverId = receiverData as! String
        let receiverIdFive = String(receiverId.characters.prefix(5))
        let senderIdFive = String(senderId.characters.prefix(5))
        if (senderIdFive > receiverIdFive)
        {
            self.convoId = senderIdFive + receiverIdFive
        }
        else
        {
            self.convoId = receiverIdFive + senderIdFive
}
    }

然后我在tableviewcell中填充派生数据:

let rootRef = FIRDatabase.database().reference()        
rootRef.child("messages").observeSingleEvent(of: .value) { 
    (snapshot: FIRDataSnapshot) in
    let loggedInUserData = snapshot
    if let postsDictionary = snapshot .value as? [String: AnyObject] {
        for post in postsDictionary {
            self.messages.add(post.value)
        }
        self.MessageTableView.reloadData()
    }
}

1 个答案:

答案 0 :(得分:1)

observeSingleEvent函数中尝试此操作:

...
for post in postsDictionary {
    let messages = post.value as! [String: AnyObject]
    for (id, value) in messages {
        let info = value as! [String: AnyObject]
        let receiver = info["ReceiverName"]!
        print("\(id): \(receiver)")
        self.messages.add(value)
    }
}
...