如何正确构建 Firebase 数据库以方便阅读和删除 [英] How to properly structure Firebase database to allow easy reading and removal

查看:24
本文介绍了如何正确构建 Firebase 数据库以方便阅读和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个食谱查找应用程序,基本上我有一个收藏夹",如果用户喜欢某个食谱,他们将能够在应用程序的收藏夹选项卡中轻松访问它.基本上这是我现在的结构:

I am creating a recipe finding app and basically I have a "favorites" where if the user favorites a recipe, they will be able to easily access it in the favorites tab of the app. Basically this is my structure right now:

app:
 users:
  2ReAGRZlYiV5F2piwMakz59XDzl1(uid):
   favorites:
    -KRUe6mMhaQOX62zgXvg(childByAutoId):
     ID: 172171 (recipe id)
  UTJ0rVst9zMSuQhWkikTCu8558C2(uid):
   favorites:
    -KRUzMxTvv-uNvX9J9_-(childByAutoId):
     ID: 578141 (recipe id)

基本上每当他们进入收藏夹选项卡时,我都需要所有配方 ID 的列表,以便我可以进行 API 调用来检索配方信息.我基本上是在遍历字典.我还希望能够允许用户不喜欢该食谱,因此将其从数据库中删除.如果我正在使用,我将如何删除它:

Basically whenever they go to the favorites tab, I need the list of all the recipe ids so that I can make an API call to retrieve the recipe information. I am basically looping through the dictionary. I also want to be able to allow the user to unfavorite the recipe, so removing it from the database. How will I be able to remove it if I am using:

USERS_REF.child(uid).child("favorites").childByAutoId().setValue(["ID": recipeID])

要添加食谱?

是否有更好的结构可以用来读取配方 ID 并轻松删除它们?

Is there a better structure that I can use to read recipe ids and remove them easily?

推荐答案

您可能想考虑将收藏作为 NSDictionary:-

You might wanna consider making favourites as a NSDictionary:-

 app:
  users:
   2ReAGRZlYiV5F2piwMakz59XDzl1: //(uid)
    favorites:
      {172171 : true,
        4123123 : true,..} 

  • 对于附加在收藏夹中:-

      USERS_REF.child(uid).child("favorites").updateChildValues([recipeID: "true"]) 
    

    请注意,如果您的 recipeID 是唯一的,即收藏节点中尚不存在只有这样它才会附加值,如果 recipieID 已经存在,它只会更新它的值(不喜欢这个附加值,查找下一个选项)

    Mind that , if your recipeID is unique, i.e doesnt already exist at favourites node Only then it will append the value, if the recipieID already exists it will just update its value (Dont prefer this for appending, look up the next option)

    或者

    let prntRef = USERS_REF.child(uid).child("favorites")
    prntRef.observeSingleEventOfType(.Value, withBlock: { (snap) in
    
        if let favDict = snap.value as? NSMutableDictionary{
            favDict.setObject("true",forKey : recipeID)
            prntRef.setValue(favDict) 
        } else {
            prntRef.setValue(["true":recipeID])
        }
    })
    

  • 对于收藏夹中的更新:-

     USERS_REF.child(uid).child("favorites").updateChildValues([recipeID: "false"])  //User doesn't like's the recipe anymore
    

  • 对于从收藏夹中删除:-

     USERS_REF.child(uid).child("favorites").child(recipeID).removeValue()  //User want to remove this item from its history
    

  • 同时检索

     let prntRef = USERS_REF.child(uid).child("favorites")
     prntRef.observeSingleEventOfType(.Value, withBlock: {(snap) in
    
         if let favDict = snap.value as? [String:AnyObject]{
    
             for each in favDict{
    
                 let eachRecipeId = each.0 //recipeID
                 let isMyFav = each.1 // Bool
             }
    
         } else {
             print("No favourites")
         }
    })
    

  • 同时检索对于已知的键值

     let prntRef = USERS_REFFIR.child("users").queryOrderedByChild("favorites/(recipeID)").queryEqualToValue(true)
     prntRef.observeSingleEventOfType(.Value, withBlock: {(snap) in
    
         //snap containing all the Users that carry that recipeID in their favourite section 
     })
    

  • 这篇关于如何正确构建 Firebase 数据库以方便阅读和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆