I'm creating a Swift Firebase app with a TableViewController where each section of the tableview is a room and every row is the furniture of each room.
Now to the problem: The first time it loads everything works fine and when any of the rooms are getting changed/removed from the backend the table view update just fine. But I have a problem getting the data to reload when any of my furniture values updates. I have no idea how to change my code to get this working. I'm starting thinking I might be using an incorrect structure of my data? How can I fix this issue?
The structure of the Firebase database is this (I removed some keys/values from the structure just to simplify so the real structure has more values):
rooms:
{
"room 1": {
"name": "Bedroom",
"somedata": 1
},
"room 2": {
"name": "Living room",
"somedata": 2
}
}
furniture:
{
"room 1": {
"furniture 2": {
"name": "Chair"
},
"furniture 3": {
"name": "Sofa"
}
},
"room 2": {
"furniture 1": {
"name": "Table"
}
}
}
The code I use to load the data in the TableViewController is:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DataManager.shared.getRooms(completion: { (rooms) in
self.rooms = rooms
self.tableView.reloadData()
})
}
DataManager:
func getRooms(completion:@escaping ([Room])->Void) {
FIRDatabase.database().reference(withPath: "rooms").observe(.value, with: { snapshot in
var rooms: [Room] = []
var counter: UInt = 0
for roomItem in snapshot.children {
let room = Room(snapshot: roomItem as! FIRDataSnapshot)
FIRDatabase.database().reference(withPath: "furniture/\(room.key)").observe(.value, with: { snap in
for furnitureItem in snap.children {
let furniture = Furniture(snapshot: furnitureItem as! FIRDataSnapshot)
room.furnitureList.append(furniture)
}
counter = counter + 1
if (counter == snapshot.childrenCount) {
completion(rooms)
}
})
rooms.append(room)
}
})
}
Thanks for helping!