0

I am working on creating a keyword search. The first code "Creating a child with all created keywords here" saves how I would like it too where each keyword is assigned a "key" (First Picture). Yet, for the second part of the code where I am saving the "values" of the "post" I want to save the keywords that are written in the post as a child "keywords" with each keyword assigned a key. The way the second part of the code saves keywords is (second picture).

The problem is when I am saving my "values" (second piece of code) how can I set the keywords I am saving to have matching "keys" instead of zero's and one's.

How I want the keywords saved

How keywords save when saving a "post"

Creating a child with all created keywords here / Saves how I want

let caption = self.postCaption.text

let hashtag = caption?.split{$0 == " "}.map(String.init)

    for item in hashtag! {
        if item.contains("#") {
            self.keywords.append(item)
            print(self.keywords)

            Database.database().reference().child("Keywords").childByAutoId().setValue(item)

        } else {
            print("There are no keywords for this post")
        }
    }

Saving keywords from post caption here to "post" as a child / does not save how I want

        photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
        if let error = error {
            print("there was an error")
            print(error.localizedDescription)
            return
        } else {
            // store downloadURL
            photoRef.child("\(imageName)").downloadURL(completion: {(url, error) in
                if error != nil {
                    print(error!.localizedDescription)
                    return
                }

                let downloadURL = url?.absoluteString

                let values: Dictionary<String, Any> = ["Keywords":self.keywords, "onesignal":self.loggedInUserData?["onesignal"] as! String,"employeeName":self.loggedInUserData?["name"] as! String,"employeeEmail":self.loggedInUserData?["email"] as! String,"uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": Int(Date().timeIntervalSince1970), "businessName":self.otherUser?["businessName"] as! String, "businessStreet":self.otherUser?["businessStreet"] as! String, "businessCity":self.otherUser?["businessCity"] as! String, "businessState":self.otherUser?["businessState"] as! String, "businessZIP":self.otherUser?["businessZIP"] as! String, "businessLatitude":self.otherUser?["businessLatitude"] as! String, "businessLongitude":self.otherUser?["businessLongitude"] as! String, "imageID": imageName, "postID": postID, "businessUID":self.otherUser?["uid"] as! String]

                // store downloadURL at database
                let databaseRef = Database.database().reference()
                let path = databaseRef.child("Businesses").child(self.otherUser?["uid"] as! String).child("posts_requests").childByAutoId()

                path.setValue(values) { (error, ref) -> Void in
                    if error != nil {
                        print("error saving post in db")
                    } else {
                        // reset caption field
                        self.postCaption.text = ""
                        // reset placeholder image
                        self.imageView.image = UIImage(named: "filterPlaceholder")
                        MBProgressHUD.hide(for: self.view, animated: true)
                        let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Employee Profile") as! UITabBarController
                        self.present(viewConrolller, animated: true, completion: nil)
                    }
                }
            })
        }
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lukas Bimba
  • 817
  • 14
  • 35
  • Your keywords are not a list (the `-L` keys), nor an array (the `0` and `1` keys), but rather a mathematical set. See my answer here for what this means in Firebase Realtime Database: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jun 18 '19 at 16:12
  • Are you recommending I use “Sets” for what I am trying to accomplish? – Lukas Bimba Jun 18 '19 at 16:55
  • Everything about your use-case suggests that tags are unique and unordered, which is not an array nor a list. The linked post explains how to model that so that you can also query for it. If that matches with your case, you'll need to indeed model it as in the linked post. Note: also consider Cloud Firestore, which has more native [support for these arrays-that-are-actually-sets](https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html). – Frank van Puffelen Jun 18 '19 at 20:15
  • I am not sure I fully understand the intent here but self.keywords is an array of stings `self.keywords.append(item)` and then when it's written `path.setValue(values)` you are writing an array - and array's do not have keys so the keys are assigned for you; 0: #hashtag and 1: #keywords. So it's doing what you told it to do. Could be maybe clarify what you want it to do? – Jay Jun 18 '19 at 20:23

0 Answers0