我在集合Goals
中包含有以下字段的Firebase文档:
familySupport: 20
generosity: 0
goalWeek: “04-05-2020"
healthyActivities: 0
id: 85313AC2-6236-4AC3-A519-BA3C0E28B751"
medicalAccess: 0
mentalHealth: 0
mentors: 0
ownerId: “cziIt3FHaSgmLDeDOBjuOOlC8sA3"
positiveFriends: 0
spirituality: 0
我正在构建一个允许用户每周记录几个“健康类别”中的活动的应用程序。 familySupport,慷慨,healthyActivities等的值是Ints,代表给定一周该“类别”中的点数。
我想构建一个查询Firebase的视图,以基于ownerId和GoalWeek检索正确的文档。我建立了一个goalListener类,如下所示:
class GoalsListener: ObservableObject{
@Published var goals: [[String:Any]] = []
var dateSelected = Date()
init(){
downloadGoals()
}
func downloadGoals(){
let formatter = DateFormatter()
formatter.dateFormat = kFORMATDATE
let goalWindow = formatter.string(from: dateSelected.startOfWeek!)
FirebaseReference(.Goals).whereField(kOWNERID, isEqualTo: Auth.auth().currentUser?.uid).whereField(kGOALWEEK, isEqualTo: goalWindow).addSnapshotListener { (snapshot, error) in
guard let snapshot = snapshot else { return }
if(!snapshot.isEmpty){
for snapshot in snapshot.documents{
let currentGoals = snapshot.data() as! [String:Any]
self.goals.append(currentGoals)
}
}
}
}
}
然后,我使用@ObservedObject var goalsListener = GoalsListener()
建立了一个视图。但是,当我尝试在视图内访问goalsListener.goals
时,发现[String:Any]
字典数组为空。
我觉得我从Firebase检索[String:Any]
对象的方法可能并不理想,因此,将对任何有关如何使此方法起作用的建议或更好方法的提示将不胜感激。