Information on how SwiftUI saves 2D arrays in Firestore.

Asked 1 years ago, Updated 1 years ago, 307 views

Question details
I would like to create a sample program that saves a two-dimensional array in Firestore and adds a one-dimensional array after saving it, but I can't deal with the following error.Could you give me some advice on how to deal with this?

First, the error statement is as shown in the image below and says "Nested Arrays are not supported".
Does this mean that two-dimensional arrays cannot be saved?
Enter a description of the image here

What I wanted to do with the test code below is as follows.
Save workername: [[sh, sm, eh, em]] in the Save New button
In the Add button, add workername: [sh, sm, eh, em] and
workername: [[sh, sm, eh, em], [sh, sm, eh, em]] data

Thank you for your cooperation.

Test Code

import SwiftUI
import Firebase
import FirebaseFirestoreSwift
import FirebaseFirestore

struct ContentView:View {
    
    @State var name=""
    @State varsh=""
    @State varsm=""
    @Statevareh=""
    @State variable=""
    
    varbody:someView {

        VStack {
            Group {
                TextField("Enter First Name", text:$name)
                TextField ("At S", text: $sh)
                TextField("S min", text:$sm)
                TextField ("At E", text: $eh)
                TextField("E min", text:$em)
            }
            //-------------------------------------------------------------------------
            Button(action: {
                let db = Firestore.firestore()
                db.collection("collectionnameX").document("docentnameX").setData([
                    name: [[sh, sm, eh, em]]
                
                ]) {errin
                    iflet err=err{
                        print("Error writing document:\(err)")
                    } else{
                        print("Document successfully write!")
                    }
                }
            }){
                Text ("Save New")
                    .border(Color.red, width:1)
            }
            //-------------------------------------------------------------------------
            Button(action: {
                let db = Firestore.firestore()
                db.collection("collectionnameX").document("docentnameX").updateData([
                    name —FieldValue.arrayUnion ([sh, sm, eh, em])
                ]) {errin
                    iflet err=err{
                        print("Error writing document:\(err)")
                    } else{
                        print("Document successfully write!")
                    }
                }
            }){
                Text ("Add")
                    .border(Color.red, width:1)
            }
            
        }

    }
}

swift firebase swiftui

2022-09-30 21:56

1 Answers

First, the error statement is as shown in the image below and says "Nested Arrays are not supported."

It seems to be clearly stated in the official Firebase document.

Data Type

You cannot store other array values as elements of an array.

Nested arrays are what you call "two-dimensional arrays," but they cannot be stored.

Even though it is permitted by specification, it is not desirable to arrange data that is determined to be "what number means" in terms of data design.It seems that the arrangement of maps (so-called dictionary types) is allowed, so one way to do this is to use them.

Save New:

db.collection("collectionnameX").document("docmentnameX").setData([[
            name: [["sh":sh, "sm":sm, "eh":eh, "em":em]]
        ]) {errin
            iflet err=err{
                print("Error writing document:\(err)")
            } else{
                print("Document successfully write!")
            }
        }

adding:

db.collection("collectionnameX").document("docmentnameX").updateData([[
            name —FieldValue.arrayUnion([["sh":sh, "sm":sm, "eh":eh, "em":em]])
        ]) {errin
            iflet err=err{
                print("Error writing document:\(err)")
            } else{
                print("Document successfully write!")
            }
        }


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.