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

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

问题描述

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

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

基本上每当他们去收藏夹选项卡,我需要所有配方ID的列表,以便我可以进行API调用来检索配方信息。我基本上循环了字典。我也希望能够让用户不喜欢这个配方,所以从数据库中删除它。如果我正在使用,我将如何删除它:

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

添加配方? / p>

是否有更好的结构可以用来阅读recipe id并轻松删除它们?

解决方案

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




  • 对于追加收藏夹: -

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

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

      let prntRef = USERS_REF.child(uid).child(favorites)
    prntRef .observeSingleEventOfType(.Value,withBlock:{(snap)in

    如果让favDict = snap.value as? NSMutableDictionary {
    favDict.setObject(true,forKey:recipeID)
    prntRef.setValue(favDict)
    } else {
    prntRef.setValue([true:recipeID] )



    $ b $
  • 对于更新在收藏夹中: - $ / $>

      USERS_REF.child(uid).child(favorites)。updateChildValues([recipeID :false])//用户不喜欢的食谱


  • 从收藏夹删除:


    $ b $ pre $ US $ c $ USERS_REF.child(uid).child(favorites)。 child(recipeID).removeValue()//用户希望从历史记录中删除该项目

  • 正在检索

      let prntRef = USERS_REF.child(uid).child(favorites) 
    prntRef.observeSingleEventOfType(.Value,withBlock:{(snap)in
    $ b $ if favDict = snap.value as?[String:AnyObject] {

    for each in favDict {

    let eachRecipeId = each.0 // recipeID
    let isMyFav = each.1 // Bool
    }

    } else {
    print(No favorites)
    }
    })


  • 检索对于已知的 键值


    $ b $ pre $ let prntRef = USERS_REFFIR.child(users)。queryOrderedByChild(favorites / \(recipeID))。queryEqualToValue(true)
    prntRef.observeSingleEventOfType(.Value,withBlock:{(snap)in
    $ b $ // //包含所有在其最喜欢的部分中携带recipeID的用户
    })



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)

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])

to add a recipe?

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

解决方案

You might wanna consider making favourites as a NSDictionary:-

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

  • For Appending in the favourites:-

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

    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)

    Or

    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])
        }
    })
    

  • For Updating in the favourites:-

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

  • For Deleting from the favourites:-

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

  • Whilst Retrieving

     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")
         }
    })
    

  • Whilst Retrieving For a known key-value pair

     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天全站免登陆