首页 > 解决方案 > Swift 4 / Firebase - 从不同数组中的实时数据库读取和存储双嵌套项

问题描述

我真的很纠结如何从 firebase 实时数据库中读取和存储我的数据。这是我的数据库的样子:

text json
{
 "Live": {
    "Today": {
      "121321": {
        "title": "rnd",
        "description": "blah blah blah",
        "stamp": 1111
      },
      "121441": {
        "title": "arnd",
        "description": "blh blah blah",
        "stamp": 134
      }
    },
    "Tomorrow": {
      "143821": {
        "title": "brnd",
        "description": "blh blah blah",
        "stamp": 1134
      }
    },
    "ThisWeek": {
      "18934": {
        "title": "drnd",
        "description": "blh blh blah blah blah",
        "stamp": 237812
    },
      "323123": {
        "title": "crnd",
        "description": "blh blh blah blah",
        "stamp": 138921
     }
   }
  }
}

我用绿色突出显示了我所说的事件。事件有标题、描述和标记。然后我有一个今天、明天和本周的外巢。今天、明天和本周是各自独立的字典,包含任意数量的事件。

我在一个单独的 swift 文件中创建了一个类:

class Event: NSObject {

    var EventTitle: String?
    var EventDescription: String?
    var EventStamp: Int?
    }

我想要三个数组。第一个数组包含“今天”的所有事件,第二个数组包含“明天”的所有事件,第三个数组包含“本周”的所有事件。我最终需要通过标记对每个数组中的事件进行排序。但是,我被困在如何将 firebase 数据快照转换为事件,然后将所有事件放入数组中。到目前为止我的代码

//declaring empty arrays to fill later
    var todayEvents = [Event]()
    var tomorrowEvents = [Event]()
    var thisweekEvents = [Event]()

//Getting snapshots
ref?.child(".../LiveEvents").observeSingleEvent(of: .value, with: { (snapshot) in

     let todaysnap = snapshot.childSnapshot(forPath: "Today")
     let tomorrowsnap = snapshot.childSnapshot(forPath: "Tomorrow")
     let thisweeksnap = snapshot.childSnapshot(forPath: "ThisWeek")

     //need to assign data to events and to arrays Today, Tomorrow, Thisweek

     })

当我尝试打印我的快照数据时,我得到一个可选值,如果我尝试使用字符串分配变量,那么我会得到一个错误,如“无法将'DataSnapshot'类型的值转换为强制类型'Int'”。我看过其他答案,但我相信我的情况有所不同,因为我有双重嵌套数据并且我想分配给不同的数组。我正在使用 swift 4

谢谢

---如果我打印(今天快照)我得到

Snap (Today) {
121321 =     {
    title = rnd;
    description = blah blah blah;
    stamp = 1111
};
121441 =     {
    title = arnd;
    description = blh blah blah;
    stamp = 134
};

做 print(todaysnap.value) 给了我同样的东西,除了类型是“可选”而不是“快照(今天)”

标签: iosswiftfirebasefirebase-realtime-databasecasting

解决方案


这是读取 Live 节点中所有数据的完整代码,并将 Today、Tomorrow 和 ThisWeek 事件放入单独的数组中

var todayArray = [String]()
var tomorrowArray = [String]()
var thisWeekArray = [String]()

let liveRef = self.ref.child("Live")
liveRef.observeSingleEvent(of: .value, with: { snapshot in

    let todaySnap = snapshot.childSnapshot(forPath: "Today")
    for todayChild in todaySnap.children {
        let todayChildSnap = todayChild as! DataSnapshot
        let todayDict = todayChildSnap.value as! [String: Any]
        let title = todayDict["title"] as! String
        todayArray.append(title)
    }

    let tomorrowSnap = snapshot.childSnapshot(forPath: "Tomorrow")
    for tomorrowChild in tomorrowSnap.children {
        let tomorrowChildSnap = tomorrowChild as! DataSnapshot
        let tomorrowDict = tomorrowChildSnap.value as! [String: Any]
        let title = tomorrowDict["title"] as! String
        tomorrowArray.append(title)
    }

    let thisWeekSnap = snapshot.childSnapshot(forPath: "ThisWeek")
    for thisWeekChild in thisWeekSnap.children {
        let thisWeekChildSnap = thisWeekChild as! DataSnapshot
        let thisWeekDict = thisWeekChildSnap.value as! [String: Any]
        let title = thisWeekDict["title"] as! String
        thisWeekArray.append(title)
    }

    print("Today")
    for today in todayArray {
        print("  " + today)
    }

    print("Tomorrow")
    for tomorrow in tomorrowArray {
        print("  " + tomorrow)
    }

    print("This Week")
    thisWeekArray.map { print("  " + $0) } //gettin' all swifty here
})

输出是

Today
  rnd
  arnd
Tomorrow
  brnd
This Week
  drnd
  crnd

但是......我可能会改变结构:

Live
  event_0  //generated with childByAutoId
    title: "rnd"
    timestamp: "20180918"
  event_1
    title: "brnd"
    timestamp: "20180919"

因为您现在可以查询每个节点,提取今天的事件。明天的活动或本周活动或您喜欢的任何一天或范围。

我将每天上传新数据,所以今天、明天和本周将每天刷新

使用这种结构,您需要做的就是添加节点,您的查询将找出哪些节点在什么时间段内是什么。

由于您的 Event 类不做任何处理,本质上只是一个保存数据的结构,那么结构怎么样:

struct EventStruct {
   var EventTitle: String?
   var EventDescription: String?
   var EventStamp: Int?
}

然后将事件添加到数组中:

let title = thisWeekDict["title"] as! String
let desc = thisWeekDict["desc"] as! String
let stamp = thsWeekDict["stamp"] as! String
let anEvent = Event(title: title, desc: desc, stamp: stamp)
thisWeekArray.append(anEvent)

或将其保留为一个类,然后将快照传入并让该类从快照中分配属性。无论哪种方式,请为 nil 添加错误检查。


推荐阅读