1

What's the best practice to save/return a 2d array in Firestore? Instead of creating a new collection for each array, is there a more efficient say of keeping the data structure together? Thanks!

struct appleCounter {
    var tree = [branches]
}

var branches = [Int]()

let treeFullOfApples = [[10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10]]

let morningCount = appleCounter{
    tree: treeFullOfApples
}
moosgrn
  • 379
  • 3
  • 13
  • Try this https://stackoverflow.com/questions/46593953/nested-arrays-are-not-supported – Alon Yeager Jul 31 '19 at 18:03
  • The question is a bit unclear and if you could explain what 'together' means we may be able to provide a solution. Also, you would not need to create a separate collection for each array or even separate documents; they could be be stored within one but again, your query use case will determine what would work the best. – Jay Jul 31 '19 at 19:57
  • @Jay Thank you for the response. I'd like to be able to store and update objects built from the appleCounter struct, however I'm having difficulties on how to use this nested array with Firestore. – moosgrn Aug 01 '19 at 13:32

1 Answers1

2

The structure should be fairly straightforward

arrays //collection
   array_doc_0 //document
      array_0     //field
         0: 10
         1: 10
         2: 10
      array_1
         0: 10
         1: 10
         2: 10

Then a class to hold the array of arrays

class MyArrayClass {
    var myArrayOfArrays = [ [Int] ]()
}

In the above, the myArrayOfArrays is a var that would contain multiple arrays of Int arrays, like shown in the question.

[[10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10]]

and then the code to read that structure from Firestore and populate a MyArrayClass object.

var myNestedArray = MyArrayClass()

let arraysCollection = self.db.collection("arrays")
let thisArrayDoc = arraysCollection.document("array_doc_0")
thisArrayDoc.getDocument(completion: { documentSnapshot, error in
    if let err = error {
        print(err.localizedDescription)
        return
    }

    guard let doc = documentSnapshot?.data() else { return }

    for arrayField in doc {
        let array = arrayField.value as! [Int]
        myNestedArray.myArrayOfArrays.append(array)
    }

    for a in myNestedArray.myArrayOfArrays { //output the arrays
        print(a)
    }
})

The end result will be an object, that has a var that is an array of arrays. The last part of the closure iterates over the object we created to verify it's value is an array of arrays. The output is

[10, 10, 10]
[20, 20, 20]
Jay
  • 34,438
  • 18
  • 52
  • 81
  • Thank you for the help! – moosgrn Aug 02 '19 at 21:03
  • I'm having an issue with casting "arrayField.value as! [Int]". Is there any easy adjustment? Thanks! – moosgrn Aug 09 '19 at 15:34
  • @moosgrn Sure, it's an easy adjustment but we would need to know what we are adjusting for. In other words, the code in my answer is 100% compatible with the Firestore structure in my answer, given that all of the child values are Int's. If your child values are something else, like Strings for example, then you would read them as an Array of Strings instead of Int's. – Jay Aug 09 '19 at 16:30