Reputation: 43
How to read data from firebase realtime database by indexPath or something like that?
JSON:
{
"2020" : {
"May" : {
"01" : {
"Room:2" : {
"Shift2" : {
"3" : {
"breaks" : {
"break d2M" : {
"endTime" : "00:39:22",
"startTime" : "00:24:17",
"totalTime" : "0 h 15 min 5 sec",
"totalTime70" : "905"
}
},
"endNotes" : {
"Notes" : "15 boxes worth also filled and sealed and labelled as cutter broke.",
"Pod" : "A-35",
"customer" : "Mc",
"numberOfPeople" : "4",
"quantity" : "18432",
"reasonOfRejects" : "Seal",
"rejects" : "5",
"teamLeader" : "Lydia "
},
"times" : {
"endCleaning" : "01:32:14",
"endTime" : "01:32:12",
"endTime70" : "15927",
"setUpToEndCleaning" : "4 h 25 min 30 sec",
"startCleaning" : "01:32:13",
"startRunToEndEun" : "4 h 25 min 27 sec",
"timeOfCleaning" : "0 h 0 min 1 sec",
"timeOfCleaning70" : "1",
"timeOfWork" : "4 h 25 min 27 sec",
"timeOfWork70" : "15930"
}
}
}
}
},
"22" : {
"Shift1" : [ null, {
"breaks" : {
"break 50B" : {
"endTime" : "8:28:56 AM",
"startTime" : "8:28:55 AM",
"totalTime" : "0 hours 0 minutes 1 seconds",
"totalTime70" : "1"
},
"break NLr" : {
"endTime" : "8:28:12 AM",
"startTime" : "8:28:11 AM",
"totalTime" : "0 hours 0 minutes 1 seconds",
"totalTime70" : "1"
}
},
"endNotes" : {
"Notes" : "Test note",
"Pod" : "B-22",
"customer" : "Germany",
"numberOfPeople" : "9",
"quantity" : "4200",
"reasonOfRejects" : "Test",
"rejects" : "30",
"teamLeader" : "Borys"
},
"issues" : {
"Issue d0c" : {
"Note" : "fight",
"Type" : "Multivac - not sealing",
"issueStartTime" : "8:28:09 AM",
"solvedTime" : "8:28:10 AM",
"timeOfIssue" : "0 hours 0 minutes 1 seconds",
"timeOfIssue70" : "1"
},
"Issue nCP" : {
"Note" : "sdf sdfgsdfg ",
"Type" : "Eric - not dosing correctly",
"issueStartTime" : "8:28:17 AM",
"solvedTime" : "8:28:19 AM",
"timeOfIssue" : "0 hours 0 minutes 2 seconds",
"timeOfIssue70" : "2"
}
},
"main" : {
"didSend" : "NO",
"id" : "ROOM2POD3",
"numberOfTargets" : "3999",
"product" : "PRODUCT: HFN-0007 Cajun Spice Mix / Cajun Kryddmix / Cajun Spice Mix 4g"
},
"times" : {
"endCleaning" : "8:28:59 AM",
"endTime" : "8:28:45 AM",
"endTime70" : "48",
"setUpTime" : "0 hours 0 minutes 30 seconds",
"setUpTime70" : "30",
"startCleaning" : "8:28:48 AM",
"startSetUpTime" : "8:27:27 AM",
"startTime" : "8:27:57 AM",
"timeOfCleaning" : "0 hours 0 minutes 11 seconds",
"timeOfCleaning70" : "11",
"timeOfWork" : "0 hours 1 minutes 2 seconds",
"timeOfWork70" : "62"
}
}, {
"breaks" : {
"break tmQ" : {
"endTime" : "8:34:37 AM",
"startTime" : "8:34:35 AM",
"totalTime" : "0 h 0 min 2 sec",
"totalTime70" : "2"
}
},
"endNotes" : {
"Notes" : "I have no idea",
"Pod" : "C-35",
"customer" : "HeliHansen",
"numberOfPeople" : "8",
"quantity" : "900",
"reasonOfRejects" : "Something",
"rejects" : "20",
"teamLeader" : "Mark"
},
"issues" : {
"Issue Epe" : {
"Note" : "dsfgsdfg",
"Type" : "Hot Water Tank - hot water has ran cold",
"issueStartTime" : "8:34:24 AM",
"solvedTime" : "8:34:25 AM",
"timeOfIssue" : "0 h 0 min 1 sec",
"timeOfIssue70" : "1"
},
"Issue fEq" : {
"Note" : "sdfgsdfg",
"Type" : "Hot Water Tank - hot water has ran cold",
"issueStartTime" : "8:34:28 AM",
"solvedTime" : "8:34:33 AM",
"timeOfIssue" : "0 h 0 min 5 sec",
"timeOfIssue70" : "5"
}
},
"main" : {
"didSend" : "NO",
"id" : "UFNGU877",
"numberOfTargets" : "800",
"product" : "PRODUCT: HFN-0006 Cajun Spice Mix / Cajun Kryddmix / Cajun Spice Mix 2g"
},
"times" : {
"endCleaning" : "8:35:15 AM",
"endTime" : "8:35:09 AM",
"endTime70" : "53",
"setUpTime" : "0 h 0 min 15 sec",
"setUpTime70" : "15",
"startCleaning" : "8:35:10 AM",
"startSetUpTime" : "8:34:01 AM",
"startTime" : "8:34:16 AM",
"timeOfCleaning" : "0 h 0 min 5 sec",
"timeOfCleaning70" : "5",
"timeOfWork" : "0 h 0 min 59 sec",
"timeOfWork70" : "59"
}
}, {
"breaks" : {
"break DKf" : {
"endTime" : "10:20:05",
"startTime" : "10:20:04",
"totalTime" : "0 h 0 min 1 sec",
"totalTime70" : "1"
},
"break s8y" : {
"endTime" : "10:20:22",
"startTime" : "10:20:20",
"totalTime" : "0 h 0 min 2 sec",
"totalTime70" : "2"
}
},
"endNotes" : {
"Notes" : "Test test",
"Pod" : "C-35",
"customer" : "Algeier",
"numberOfPeople" : "9",
"quantity" : "11000",
"reasonOfRejects" : "Test",
"rejects" : "500",
"teamLeader" : "Natan"
},
"issues" : {
"Issue Xjh" : {
"Note" : "buvbtg",
"Type" : "Hot Water Tank - hot water has ran cold",
"issueStartTime" : "10:20:14",
"solvedTime" : "10:20:18",
"timeOfIssue" : "0 h 0 min 4 sec",
"timeOfIssue70" : "4"
}
},
"main" : {
"didSend" : "NO",
"id" : "NATAN",
"numberOfTargets" : "10000",
"product" : "PRODUCT: HFN-0008 Cashews 20g"
},
"times" : {
"endCleaning" : "10:21:04",
"endTime" : "10:20:47",
"endTime70" : "40",
"setUpTime" : "0 h 0 min 23 sec",
"setUpTime70" : "23",
"setUpToEndCleaning" : "0 h 1 min 20 sec",
"startCleaning" : "10:20:48",
"startSetUpTime" : "10:19:44",
"startTime" : "10:20:07",
"timeOfCleaning" : "0 h 0 min 16 sec",
"timeOfCleaning70" : "16",
"timeOfWork" : "0 h 0 min 57 sec",
"timeOfWork70" : "57"
}
} ]
}
I want to read all "numberOfTargets" and print the values. How can I make path like this: "2020 -> May -> 11 -> (first index) -> main -> numberOfTargets" ? It is not constant, so I don't know name of it. Is it possible to do that by index number? I've tried to do that for different ways, and I didn't find solution.
I also tried something like this, but I thins it's wrong, and I don't know how can I do that :)
func allTargets() {
let ref1 = ref.child("\(av.currentYear())/\(av.currentMonth())/\(av.currentDay())")
ref1.observeSingleEvent(of: .value) { (snapshot) in
let value = snapshot.value as? NSDictionary
let numberOfTargets = value?.allKeys
print(numberOfTargets)
}
}
Upvotes: 0
Views: 559
Reputation: 35648
There's a bunch of ways to get to that data; I'll show two, a coding solution and then an alternative structure solution
Coding solution:
The ID's are unknown but the path to the parent node of those ID's is. So create a reference to the most-known key (the parent, 11) and then read the child nodes underneath.
Assemble them into an array of DataSnapshots (to keep the order) and then iterate over that array, getting the child data at numberOfTargets.
func readNumberOfTargets() {
let mayRef = self.ref.child("2020").child("May") //self.ref points to my firebase
let dayRef = mayRef.child("11")
dayRef.observeSingleEvent(of: .value, with: { snapshot in
let childSnaps = snapshot.children.allObjects as! [DataSnapshot]
for child in childSnaps {
let mainSnap = child.childSnapshot(forPath: "main")
let numTargets = mainSnap.childSnapshot(forPath: "numberOfTargets")
print(child.key, numTargets)
}
})
}
and the output
Jan Snap (numberOfTargets) 80
borys Snap (numberOfTargets) 77
ftfvdf Snap (numberOfTargets) 99
Note that it looks like the numberOfTargets is being stored as a string so use this if that's the case
let numTargets = mainSnap.childSnapshot(forPath: "numberOfTargets").value as? String ?? "No Targets"
if it's being stored as an Int, use this
let numTargets = mainSnap.childSnapshot(forPath: "numberOfTargets").value as? Int ?? 0
Alternative Structure Solution:
It's common practice to denormalize your data in NoSQL databases and also using compound keys to get to it. In this case, we'll do both and create a seperate node that contains a highly flattened structure that's super easy to get to
TargetCount
2020_May_11_Jan: 80
2020_May_11_borys: 77
2020_May_11_ftfvdf: 99
So when you want the target count for each node, it can just be read based on the path. In other words, when this node is created
2020
May
11
Jan
they key 'Jan' (the unknown key per your question) is returned when that node is created. So at the same time write that path to the TargetCount node
TargetCount
2020_May_11_Jan: 80
As you iterate over the child nodes, you'll know the unknown by descontstucting the key string
2020_May_11 and then the Jan value is 80
You could also do this if you want to isolate the specific dates
TargetCount
2020_May_11
Jan:80
borys: 77
ftfvdf: 99
Upvotes: 1