是否可以将数组结果(包含图像URL和食物名称)存储到firebase数据库中,然后从firebase数据库中检索并显示在集合视图中?如果是,如何实现?
由于无法在集合视图中显示图像URL和食物名称,因此我尝试使用其他解决方案来解决此问题。
根据您的要求,尝试进行此操作以将数据插入Firebase中。这只是参考代码。
func temp() {
var arrTemp : [[String : Any]] = [[String : Any]]()
for index in 0...3 {
print("index :: \(index)")
let dictTemp = [ "foodName" : "Abcd","imgUrl" : "https://"] as [String : Any]
arrTemp.append(dictTemp)
}
let dictDetails = [
"uniqueId" : UUID().uuidString,
"arrList" : arrTemp] as [String : Any]
print("dictDetails :: \(dictDetails)")
self.insertDateInFirebase(dictDetails) { (success) in
print(success)
}
}
func insertDateInFirebase(_ dictRecipeInfo : [String : Any], completionHandler:@escaping DBcompletionHandler) {
if let currentUser = Auth.auth().currentUser {
Database.database().reference().child("TableName").child(currentUser.uid).child(dictRecipeInfo["uniqueId"] as! String).setValue(dictRecipeInfo) {
(error:Error?, ref:DatabaseReference) in
if let error = error {
print("Error :: \(error.localizedDescription)")
completionHandler(false)
}
else {
completionHandler(true)
}
}
}
else {
completionHandler(false)
}
}
要从Firebase检索数据:
func retrieveData() {
if let currentUser = Auth.auth().currentUser {
let dataBaseRefExeRoutine = Database.database().reference().child("TableName").child(currentUser.uid)
dataBaseRefExeRoutine.observeSingleEvent(of: .value, with:{ (snapshot) in
for snap in snapshot.children {
let objSnap = snap as! DataSnapshot
var arrMeals = [[String : Any]]()
//get meal item array (array with key 0,1 etc..)
if let arrMealItems1 = objSnap.childSnapshot(forPath: "uniqueId").value as? NSArray {
for obj in arrMealItems1 {
if let dict = obj as? [String:Any] {
arrMeals.append(dict)
}
}
}
}
}) { (error) in
print("Error :: \(error.localizedDescription)")
}
}
}