2

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?

Bob
  • 741
  • 2
  • 9
  • 18

1 Answers1

5

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 
     })
    
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Wouldn't it not be ideal to keep the id's there even when its false? – Bob Sep 12 '16 at 20:11
  • So that you can keep the track wether or not your user ever liked that particular reciepie or not. Yes. This will also help you while building *Recommendation Engine* of your app – Dravidian Sep 12 '16 at 20:16
  • Basically when I want to read the favorited recipes, I would have to loop through all of them right? – Bob Sep 12 '16 at 20:24
  • Yes you will have to loop through, but if you know which recipeID you are looking for, you can easily use `queryOrderedByChild(recipeID)` to FIND IT.And storing as a Dictionary is a good practice :)Happy coding – Dravidian Sep 12 '16 at 20:28
  • Sorry can you explain to me how to use the queryOrederedByChild(recipeID)? – Bob Sep 12 '16 at 21:38
  • It wont be useful in your case , since there would be only one key of that recipeID in your Fav. But anyways answer updated – Dravidian Sep 12 '16 at 21:52