I looked through the documents on Firebase and through Stack Overflow and YouTube tutorials, but I can learn how to get the data if you get it through Firebase.
I am new to Firebase and in the process of switching my project from Parse to Firebase.
Example: I have data in Firebase that looks like this:

I can capture all topics in Swift by doing this:
let refDB = FIRDatabase.database().reference(fromURL: firebaseDB_URL) let topicsRef = refDB.child("topics") // FIRDataSnapshot. topicsRef.observe(.value, with: { snapshot in for child in snapshot.children { print("child ------") print(child) // Get the bits HOW DO I PARSE EACH SET } })
When I repeat the for loop, I print things that look like this:
child ------ Snap (-KYCqk2_AVkUd8s9cKit) { createdBy = FeMFeeDat4VZb5tmFO2tKixgQIy1; description = "Match states with their capitals"; name = "State Caiptals"; tags = { 0 = Geography; 1 = USA; }; } child ------ Snap (-KYCqk2_AVkUd8s9cKiu) { createdBy = FeMFeeDat4VZb5tmFO2tKixgQIy1; description = "Name the parts of an Atom"; name = "Parts of an Antom"; tags = { 0 = Physics; 1 = Atom; 2 = Electron; }; }
My problem is how can I get the data:
I need a key (KYCqk2_AVkUd8s9cKiu) I need a description and a name I need an array of tags
- everything in local variables?
Basically, I just want to read in all Topics and have a Topics array in local memory.
I can take care of creating an array of class threads, but I tried several approaches to getting data without any luck. There should be an easy way to analyze the result, but I did not find an example or documentation.
Would thank for help or a pointer to some document or textbook.
===================================
Updated Code
Hi, I changed the code to try to match the pattern. The code now looks lower. I set the loop counter to see what happens and why it crashes.
FDataSnapshot is not defined, so I used FIRDataSnapshot.
Here is a new attempt at code that is now crashing. Next, I show my changes to make this not a failure - and the question of the safe delivery of the tag subtree . Thanks for the pointer. I now have something that works.
// HERE is a way to get all the Topics let refDB = FIRDatabase.database().reference(fromURL: firebaseDB_URL) let topicsRef = refDB.child("topics") // FIRDataSnapshot. topicsRef.observe(.value, with: { snapshot in if snapshot.value is NSNull { print("not found") } else { var loopCount = 1 // count loops to see how may time trying to loop for child in snapshot.children { print(" ") print(" ") print("child ------ loop \(loopCount)") print(child) let snap = child as! FIRDataSnapshot //each child is a snapshot let dict = snap.value as! [String: String] // the value is a dictionary let name = dict["name"]! let description = dict["description"]! let createdBy = dict["createdBy"]! print("the bits ------") print("name .... \(name)") print("description .... \(description)") print("createdBy .... \(createdBy)") loopCount += 1 } } })
I have zero breakpoints, but the code stops at that breakpoint (when I know the exact zero breakpoints)
libswiftCore.dylib`_swift_bridgeNonVerbatimFromObjectiveC: 0x1144a4270 <+0>: pushq %rbp 0x1144a4271 <+1>: movq %rsp, %rbp 0x1144a4274 <+4>: pushq %r15
... is torn here three times, and then the application crashes in this line let dict = snap.value like! [String: String] with the message "Subject 1: EXC_BAD_INSTRUCTION (code = EXEC_1386_INVOP, lowering = 0x0)
I am not sure why the code has a breakpoint and why it crashes. Maybe crashed when hit tags because tags are sub node and not suitable [String, String]
I print it in a magazine and then go boom !!!
child ------ loop 1 Snap (-KYI2MszjC9pK_4oIvKu) { createdBy = FeMFeeDat4VZb5tmFO2tKixgQIy1; description = "Match states with their capitals"; name = "State Caiptals"; tags = { 0 = Geography; 1 = USA; }; }
=====
If I change the string to use "Any" ... then it works
let dict = snap.value as! [String: Any]
new working code ....
// HERE is a way to get all the Topics let refDB = FIRDatabase.database().reference(fromURL: firebaseDB_URL) let topicsRef = refDB.child("topics") // FIRDataSnapshot. topicsRef.observe(.value, with: { snapshot in if snapshot.value is NSNull { print("not found") } else { var loopCount = 1 // count loops to see how may time trying to loop for child in snapshot.children { print(" ") print(" ") print("child ------ loop \(loopCount)") let snap = child as! FIRDataSnapshot //each child is a snapshot if snap.value != nil { print("key ... \(snap.key)") let dict = snap.value as! [String: Any] // the value is a dictionary let name = dict["name"] as! String let description = dict["description"] as! String let createdBy = dict["createdBy"] as! String let tags = dict["tags"] as! NSArray // Test to see what we got ... print("the bits ------") print("name .... \(name)") print("description .... \(description)") print("createdBy .... \(createdBy)") print("tags ... \(tags) ... count \(tags.count)") loopCount += 1 } else { print("bad snap") } } } })
I understood the topic key from the doc link sent by another answer. Thanks.
I'm not sure if I get the tag values ββcorrectly. This is actually just a dictionary, and I tried to use it that way, but it crashes at runtime and wants to use tags for NSArray .... so I did it in the code and it works, but not sure if it is safe, since it is not defined as an array, although it is returned as an array.