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

时间:2018-09-17 18:44:47

标签: 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数据快照转换为事件,然后将所有事件放入数组感到困惑。 到目前为止,我的代码

//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)给我同样的事情,除了类型是“ Optional”而不是“ Snap(Today)”

1 个答案:

答案 0 :(得分:1)

这是读取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事件或您希望的任何日期或范围。

  

我每天要上传新数据,所以今天,明天等等   一周将每天刷新

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

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