ios - Swift 4/Firebase - 从不同数组中的实时数据库读取和存储双重嵌套项目

标签 ios swift firebase firebase-realtime-database casting

我真的很苦恼如何从 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 datasnapshots 转换为事件,然后将所有事件放入一个数组中。 到目前为止我的代码

//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

     })

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

谢谢

---如果我这样做 打印(todaysnap)我得到

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

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

最佳答案

下面是完整的代码,用于读取 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"

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

I'm going to uploading new data each day so today, tomorrow and this week will be refreshed each day

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

由于您的 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 添加错误检查。

关于ios - Swift 4/Firebase - 从不同数组中的实时数据库读取和存储双重嵌套项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52373988/

相关文章:

带键盘的 iPhone/iOS + 自定义工具栏布局

Swift 1.2 -> Swift 2 转换时间

ios - 如何测试用 DispatchQueue.main.async 调用的方法?

ios - 动画不执行

java - 使用 Firebase 在 Android 应用程序中使用 Spring Boot

java - firebase查询性能orderByChild + .indexOn VS orderByKey

firebase - 仅更新列表中点击的ListTile颜色

iphone - 在应用程序委托(delegate)之外注册远程通知

iOS:核心数据异步数据获取和保存 block

ios - 字母滚动条不显示