数据以与传输SwiftUI不同的顺序保存在Firebase中

问题描述

我在应用程序中将数据保存到Firestore。在代码中,它看起来像这样:

db.collection("flowers")
                        .document()
                        .setData(["Name":self.flowerName,"Cup width":self.cupWidth,"Cup height":self.cupHeight,"stem length":self.stemLength,"Leaf width":self.leafWidth,"Leaf length":self.leafLength])

但是在Firestore中,它看起来像这样:

Cup height: "1"
Cup width: "1"
Leaf length: "1"
Leaf width: "1"
Name: "Rose"
stem length: "1"

我希望将它们以与在应用程序中传输相同的方式保存在数据库中。我猜它们在数据库中按字母顺序排序,但是可以更改吗?我在文档中寻找任何信息,但没有找到任何东西。

解决方法

Firestore以JSON格式存储数据,根据定义,该格式是无序的。因此,将字段传递给数据库的顺序不会影响它们的存储顺序。

Firebase控制台按字母顺序显示字段,尽管我不确定是否可以保证。

当然,您可以按所需顺序在应用程序的UI中显示它们。