简体   繁体   中英

Reading Data from Firebase into Arrays

Reading snapshots from firebase is fairly simple, although transferring the information to arrays is more complicated. I have this snapshot

Snap (01-08-2019) {
    Sleep = "6.25 hrs";
    "Time Uploaded" = "07:10 AM";
}
Snap (01-09-2019) {
    Sleep = "6.72 hrs";
    "Time Uploaded" = "07:19 AM";
}
Snap (01-10-2019) {
    Sleep = "6.55 hrs";
    "Time Uploaded" = "07:10 AM";
}

How would I be able to make one array for the date, one for the sleep, and one for the time uploaded.

I think you should reconsider how you store your data in firebase. To Look something similar to this.

在此处输入图片说明

Also I would consider to create a data model for day that looks something like this.

class Day {
    var date: String
    var sleep: String
    var timeUploaded: String

    init(date: String, sleep: String, timeUploaded: String) {
        self.date = date
        self.sleep = sleep
        self.timeUploaded = timeUploaded
    }
}

Then you can just fetch your snapshots like this.

var days = [Day]()

private func fetchDays() {
    print(days.count)

    let ref = Database.database().reference().child("days")
    ref.observeSingleEvent(of: .value) { (snapshot) in
        guard let days = snapshot.value as? [String: Any] else { return }

        for (_,value) in days.enumerated() {
            guard let dayDict = value.value as? [String: String] else { return }
            let date = dayDict["date"] ?? ""
            let sleep = dayDict["sleep"] ?? ""
            let timeUploaded = dayDict["time_uploaded"] ?? ""

            //If you really want 3 different arrays just add them here
            // dateArray.append(date) and so on for the other two arrays
            let day = Day(date: date, sleep: sleep, timeUploaded: timeUploaded)
            self.days.append(day)
        }

        print(self.days.count)
    }
}
}

Hope this helps. Couldn't comment to ask how your data was structured.

I would suggest not keeping the data in different arrays, it may be better to store the data from each node within a class, and then keep an array of those classes.

Let's start with a class that keeps all of the data

class ChronoClass {
    var node_id = ""
    var sleep = ""
    var time_uploaded = ""

    init(withSnapshot: DataSnapshot) {
        let nodeId = withSnapshot.key
        let someSleep = withSnapshot.childSnapshot(forPath: "sleep").value as? String ?? ""
        let someTime = withSnapshot.childSnapshot(forPath: "time_uploaded").value as? String ?? ""

        self.node_id = nodeId
        self.sleep = someSleep
        self.time_uploaded = someTime
    }
}

and then a class array to keep all of the classes

var sleepArray = [ChronoClass]()

and finally the code to read in each node, populate the class and store the classes in an array.

func readFirebaseDataAndPopulateArray() {
    let sleepNode = self.ref.child("sleep_node")
    sleepNode.observeSingleEvent(of: .value, with : { snapshot in

        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let aChrono = ChronoClass(withSnapshot: snap)
            self.sleepArray.append(aChrono)
        }

        for x in self.sleepArray { //just prints out what's in the array
            print(x.node_id, x.sleep, x.time_uploaded)
        }
    })
 }

and the output based on your structure

01-08-2019 6.25 hrs 07:10 AM
01-09-2019 6.72 hrs 07:19 AM
01-10-2019 6.55 hrs 07:10 AM

The advantage with using a class is you can sort, search, extrapolate or do a variety of other functions on the objects instead of working with three separate arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM