-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to convert dictionary of object_id : object to array of objects? #46
Comments
Here is an example for Cloud Firestore: Fetch Collection of "MyData" reference.getDocuments { (querySnapshot, error) in
if let error = error {
print(error.localizedDescription)
} else {
if let documents = querySnapshot?.documents {
let myDataArray = documents.map({ (snapshot) -> MyData in
let myData = try! FirestoreDecoder().decode(MyData.self, from: snapshot.data())
return myData
})
completion(myDataArray) // This is your array of MyData objects
}
}
} Another Example let batch = Firestore.firestore().batch()
for myData in arrayOfMyData {
let reference = db.collection("myData").document()
let myDataDictionary = try! FirestoreEncoder().encode(myData)
batch.setData(myDataDictionary, forDocument: reference)
}
batch.commit { (error) in
if let error = error {
print(error.localizedDescription)
} else {
completion("Upload successful")
}
} There are two problems in your code: 1. Your myData.id is not part of the JSON Object itself. I would put the documentID into the JSON object like this: {
"id1": {
"documentID: String #id1"
"name":"name1",
"connections": {
"id1": 1,
"id2": 1,
}
},
"id2": {
"documentID: String #id2"
"name":"name2",
"connections": {
"id1": 1,
"id2": 0,
}
}
} This makes it easier for FirestoreDecoder to map the documentID to the MyData struct. 2. I would consider putting the connections inside subcollections. If you want to add id3 or id1000 to document1, you have to fetch document1 with all the connections inside an array and add id1000 to it. This can cause problems in scalability the more connections you have inside one document. Why CodableFirestore / CodableFirebase Consider you have a struct like this struct Person: Codable {
var name: Name? // Firstname, Lastname, Salutation
var address: Address? // Street, PostalCode, Country etc.
var contactDetails: ContactDetails? // Phone, Mobile, etc.
var foodPreferences: FoodPreferences? // ...
....
....
} To encode for firebase / firestore or decode from it, you have to write a lot of boilerplate code. With CodableFirestore / CodableFirebase you can avoid encoding / decoding boilerplate code. That is why I am using it. |
Sorry, I should make it clear - I am using Firebase Realtime Database, not Firestore. |
Ok someone else can get this up for you, I never used FRD. Regardless of the data structure, encoding and decoding with CodableFirebase works identical for FRD, so maybe my post can help you to implement it for FRD. |
For now I've just created method for converting dictionary of keys:objects to array of objects:
Then use it like that:
|
Let's say we have structure like this:
I want to map it to structure like this:
As you see id is on upper level while name and connections are on lower level.
Should I manually convert that dictionary to array before every parsing?
Can I somehow setup "pre-parser" which in case of converting
Dictionary
toArray[MyData]
will flatten fromDictionary
toArray[Dictionary]
?Also as side question.. since snapshot returns
Dictionary[String: Any]
it seems you can decode it using regular defaultJSONDecoder
(or can't you?) - what's the advantage of using this library? Probably I am missing a point here :)The text was updated successfully, but these errors were encountered: