Firebase数据检索 - 编辑注释 [英] Firebase Data Retrieval - Edit Annotation

查看:216
本文介绍了Firebase数据检索 - 编辑注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个应用程序,用户可以在他们的地图上保存注释,这一切都很好!地图的位置,名称和副标题都发送到数据库,并将引脚放入地图。



但是,我正在努力弄清楚如何提取这些数据从[Firebase] [1]退后,以便用户能够编辑以前的信息,然后进行更新。要读取数据,您需要使用 / database / ios / read-and-writerel =nofollow noreferrer> observe 属性。这会返回包含您的数据的快照



由于 Skatepark class已经有一个快照构造函数,所有你需要做的就是把快照传递给它并创建你的对象。

pre $ $ $ c> reference.child(yourNode)。observeSingleEvent(of:.value,with:{(snapshot)in

/ *这里可能发生两件事情:快照可以包含一个元素或多个。
为了处理这个问题,你需要迭代快照,创建你的Skatepark
对象,并把它填充到你的全局滑板场阵列中。
* /

如果快照.value是NSNull
{
print(Error Getting Snapshot)
}
else
{
for(child in snapshot.children)
{
let snap = child as!FIRDataSnapshot
let skateObject = Skatepark(child)//这将调用以快照作为参数
globalSkateParkArray的构造函数。附加(skateObject)
}
}

})

要更新您的值,请这是您所需要的


$ b


  1. 考虑制作坐标 name 字幕转换为 var 而不是 let

  2. 考虑先创建一个 Skatepark 发送到数据库。这个构造函数应该可以做到:
    $ b $ pre $ init $(坐标:CLLocationCoordinate2D,名称:字符串,字幕:字符串,类型:SkateboardType,可编辑: Bool)
    {
    self.coordinate = coordinate
    self.name = name
    self.subtitle = subtitle
    self.type = type
    self.editable = editable
    }


  3. 我会创建一个字典 Skatepark 里面的变量,从对象创建一个字典。这样我就不会有 [lat:locationManager.location?.coordinate.latitude,lng:locationManager.location?.coordinate.longitude,name:skateTitleText,subtitle: skateStyleText,type:(selected - 1),editable:true] 浮动在我的代码库中。


  4. b

    所以在你的课里有这个

      var字典:[String:Any] 
    {
    return

    lat:coordinate.latitude,
    lng:coordinate.longitude,
    name:title,
    subtitle:subtitle,
    type:type,
    editable:editable
    ]
    }

    每次你想要一个字典超出你的对象,只需使用 object.dictionary


    I am currently working on an application where the users can save an annotation on their map and this all works great! The map location, name and subtitle are all sent to the database and the pin is put into the map.

    However, I am struggling to figure out how to pull this data back down from [Firebase][1] so the users are able to edit the previous information and then update it.

    解决方案

    To read data, you want to use the observe attribute from your firebase reference. This returns a snapshot consisting of your data.

    Since your Skatepark class already has a snapshot constructor, all you'd have to do is pass the snapshot to it and create your object.

    reference.child("yourNode").observeSingleEvent(of: .value, with: { (snapshot) in
    
    /* Two things can happen here. Snapshot can either consist of one element or multiple.
       To handle this, you want to iterate through the snapshot, create your Skatepark
       object and populate it into your global skatepark array.
    */
    
        if snapshot.value is NSNull
        {
            print("Error Getting Snapshot")
        }
        else
        {
            for (child in snapshot.children)
            {
                let snap = child as! FIRDataSnapshot
                let skateObject = Skatepark(child) // this calls the constructor which take snapshot as parameter
                globalSkateParkArray.append(skateObject)
            }
        }
    
    })
    

    To update your values, this is all you need.

    Personal Recommendations

    1. Consider making coordinate, name, subtitle into var instead of let.
    2. Consider creating a Skatepark object first out of the data before sending it to the database. This constructor should do the trick

      init(coordinate: CLLocationCoordinate2D, name: String, subtitle: String, type: SkateboardType, editable: Bool)
      {
          self.coordinate = coordinate
          self.name = name
          self.subtitle = subtitle
          self.type = type
          self.editable = editable
      }
      

    3. I'd create a dictionary variable inside Skatepark that creates a dictionary from the object. This way I won't have ["lat": locationManager.location?.coordinate.latitude, "lng": locationManager.location?.coordinate.longitude, "name": skateTitleText, "subtitle": skateStyleText, "type": (selected - 1), "editable": true] floating all around my codebase.

    So have this in your class

    var dictionary: [String:Any]
    {
        return
        [
            "lat": coordinate.latitude,
            "lng": coordinate.longitude,
            "name": title,
            "subtitle": subtitle,
            "type": type,
            "editable": editable
        ]
    }
    

    Every time you'd want a dictionary out of your object, simply use object.dictionary

    这篇关于Firebase数据检索 - 编辑注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆