I am developing an app with Swift. this is where I am creating the structure and sending data to database:
func addRecord(userID: String,
recordDate: String?, recordTime: String?,
mealCategoryTitle: String?, mealCategoryImage: String?,
breakfastMealTitle: String?, breakfastMealImage: String?,
lunchDinMealTitle: String?, lunchDinMealImage: String?,
snackMealTitle: String?, snackMealImage: String?,
treatMealTitle: String?, treatMealImage: String?,
drinkTitle: String?, drinkImage: String?,
foodNote: String?,
placeTitle: String?, placeImage: String?,
moodTitle: String?, moodImage: String?,
reactionTitle: String?, reactionImage: String?,
moodNote: String?) {
ref?.child("Records").childByAutoId().setValue([
"userID": userID,
"date": recordDate!,
"time": recordTime!,
"mealCategory" : [ "itemTitle": mealCategoryTitle, "itemImage": mealCategoryImage ],
"breakfastMeal" : [ "itemTitle": breakfastMealTitle, "itemImage": breakfastMealImage ],
"lunchDinnerMeal" : [ "itemTitle": lunchDinMealTitle, "itemImage": lunchDinMealImage ],
"snackMeal" : [ "itemTitle": snackMealTitle, "itemImage": snackMealImage ],
"treatMeal" : [ "itemTitle": treatMealTitle, "itemImage": treatMealImage ],
"drink" : [ "itemTitle": drinkTitle,"itemImage": drinkImage ],
"foodNote": foodNote ?? "nil" ,
"place" : [ "itemTitle": placeTitle, "itemImage": placeImage ],
"mood" : [ "itemTitle": moodTitle, "itemImage": moodImage ],
"reaction" : [ "itemTitle": reactionTitle, "itemImage": reactionImage ],
"moodNote": moodNote ?? "nil" ,])
//later use this key to set Status node
// ref?.child("Records").child(key).child(moodNote).setValue.( /// set the rest of values)
//
}
When I am reading the data, I am only capable to access the first level, but need the MealCategoryTitle and Image for example.
func readRecords(reloadedTableView: UITableView ){
ref?.child("Records").observe(.childAdded, with: { (snapshot) in
if let rrecord = snapshot.value as? [String: Any] {
let userID = rrecord["userID"] as? String ?? ""
let date = rrecord["date"] as? String ?? ""
let time = rrecord["time"] as? String ?? ""
// problem
let node: NSArray = snapshot.children.allObjects as NSArray
let childNode = node.[0]
let foodNote = rrecord["foodNote"] as? String ?? ""
let moodNote = rrecord["moodNote"] as? String ?? ""
let actualRecord = Record(userID: userID, date: date, time: time, mealCategory: ItemDetail(itemTitle: "problem", itemImage:nil), breakfastMeal: nil, lunchDinMeal: nil, snackMeal: nil, treatMeal: nil, drink: nil, foodNote: foodNote, place: nil, mood: nil, reaction: nil, moodNote: moodNote)
self.records.append(actualRecord)
print(actualRecord)
print("This is snap:\(node)")
print("This is child:\(childNode)")
reloadedTableView.reloadData()
}
})
{ error in
print(error.localizedDescription)
}
CodePudding user response:
If you get the mealCategory
out of the snapshot, that'll give you a dictionary from which you can then access the properties under that key.
So for example to get the itemTitle
of the mealCategory
, you'd do:
let mealCategoryTitle = rrecord["mealCategory"]["itemTitle"]
You shouldn't have to cast the type of that rrecord["mealCategory"]
as far as I can tell, but if that is needed its type would be rrecord["mealCategory"] as? Dictionary<String, AnyObject>
or rrecord["mealCategory"] as? [String:Any]
.