0

For soon reason i can't seem to do this simple thing. Which is take the Array or Strings and put it into firebase. (with out a for loop). How do I do this?

Code (Error):

// Cannot convert value of type '[String]' to expected argument type '[AnyHashable : Any]

  var list = [String]()
  list.append("TestString")


   Database.database().reference().child("TestNode").updateChildValues(list) { (error, value) in
        if error != nil {
            return
        } else {
        }
    }

Code (Runs Then Crashes):

// Could not cast value of type 'Swift.Array<Swift.String>' (0x103f941b8) to 'Swift.AnyHashable' (0x103f548b0).

    Database.database().reference().child("TestNode").updateChildValues([list as! AnyHashable: "nil"]) { (error, value) in
        if error != nil {
            return
        } else {
        }
    }
Hunter
  • 1,321
  • 4
  • 18
  • 34

3 Answers3

0

Your code is close but let's take a look at what it's doing. Firebase strucures are always key: value pairs (e.g. think Dictionary) so this is valid

name: "Leeroy"

wheras this is not

"TestNode"

as you can see TestNode is not a key: value pair.

["TestNode", "Hello", "Test123"]

is also not valid as it's now an array of strings - not key: value pairs.

This is valid a valid key: value pair

user_0
  name: "Leeroy"

because user_0 is the key and name: "Leeroy" is the value, which is also a key: value pair.

The reason your code isn't working as it's trying to write a single string, TestNode to Firebase without it being in a key: value format. So fix it by making the data a key: value pair (Dictionary) instead of a single string (on in your case a single string stored in an array)

let dict = [
    "name": "Leeroy",
    "food": "pizza"]

let testRef = self.ref.child("TestNode")
testRef.updateChildValues(dict) { (error, value) in
    if error != nil {
        return
    } else {
    }
}

This will result in the following structure being written to Firebase

TestNode
   name: "Leeroy",
   food: "pizza"

Firebase needs to know where you want data stored; when you tell it to add or update the pizza value in the TestNode/food node, it knows specifically where you want the data written.

An array of strings should not be directly written to Firebase as it will be just that, an array. Each element of the array would generally be associated with a node (key) to make each element a key: value pair. This is good form, makes your Firebase queryable and more maintainable.

While it may be tempting to use arrays in Firebase (because it can be done) - don't. It's not a concept that works well in NoSQL databases. See Don't Use Arrays and also this post and maybe this one as well for further reading.

Jay
  • 34,438
  • 18
  • 52
  • 81
0

Firebase supports key-value pair so if you want to store an array, you have assign a key to that array like

let customArray = ["a","b","c","d"]
myRef!.child(recordId).updateChildValues([
   Keys.shopNameKey : shopName.text!,
   "custom" : customArray
])

Regards

-1

please check this code and it will be work

let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
        "author": username,
        "title": title,
        "body": body]
let childUpdates = ["/posts/\(key)": post,
                "/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)

as Firebase Documentation Chek Update Field in firebase

pradip kikani
  • 627
  • 6
  • 14
  • Not the downvoter but did you try this code? It will create two independent nodes, one *posts/firebase_id/child nodes* and one *user_posts/user_id/firebase_id/child nodes*. Was that intentional? It seems unrelated to the question and it's unclear why you are suggesting to write two separate nodes to Firebase. Maybe you can clarify the answer? – Jay Mar 03 '18 at 14:42