关于构建应用程序的建议

时间:2018-02-24 10:27:17

标签: ios swift firebase struct firebase-realtime-database

如果我正确构建我的应用程序,我不是肯定的。我是第一次使用结构,我正在尝试从用户保存位置并使用Firebase创建用户。我想只按位置显示所有用户。 loadUserInfo()从UIPicker设置,该UIPicker使用6个不同的位置进行硬编码。 private func saveUserInfo(firstLastName: String, user: User!, location: String, biography: String, password: String, phoneNumber: String){ let locationRef = Global.refString let userInfo = ["firstLastName": firstLastName, "email": user.email!, "password": password, "location": location, "phoneNumber": phoneNumber, "biography": biography, "uid": user.uid, "photoURL": String(describing: user.photoURL!)] as [String : Any] let userRef = dataBaseRef.child(locationRef!).child(user.uid) userRef.setValue(userInfo) { (error, ref) in if error == nil{ print("USER SAVED") self.logIn(email: user.email!, password: password) }else{ print(error?.localizedDescription) } } } func loadUserInfo(){ let locationRef = Global.refString let userRef = dataBaseRef.child(locationRef!).child(Auth.auth().currentUser!.uid) userRef.observe(.value, with: { (snapshot) in let user = Users(snapshot: snapshot) if let username = user.name{ self.nameLabel.text = username } if let number = user.phoneNumber{ self.phone = Int(number) } if let userLocation = user.location{ self.bioLabel.text = userLocation } self.storageRef.storage.reference(forURL: imageOld).getData(maxSize: 10 * 1024 * 1024, completion: { (imgData, error) in if error == nil { DispatchQueue.main.async { if let data = imgData { self.avatar.image = UIImage(data: data) } } }else { print(error!.localizedDescription) } } )} }) { (error) in print(error.localizedDescription) } } } struct Global { static var Location : String! static var usersListSent = [String]() static var refString: String! } 未返回任何信息。它是我第一次尝试它,但重新打开并关闭应用程序后,它总是返回空。我不确定每次打开应用程序时是否会保存结构。我应该使用不同的方法来完成这些任务。

{{1}}

1 个答案:

答案 0 :(得分:0)

尝试:

Database.database().reference().child(Global.refString).child(id).observeSingleEvent(of: .value) { (snapshot) in 
    if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
        for snap in snapshots {
            print(snap)
        }
}