【问题标题】:Advice on structuring an app关于构建应用程序的建议
【发布时间】:2018-02-24 10:27:16
【问题描述】:

如果我正确地构建我的应用程序,我并不肯定。我第一次使用结构,我正在尝试保存用户的位置并使用 Firebase 创建用户。我只想按位置显示所有用户。 Global.refString 是从一个用 6 个不同位置硬编码的 UIPicker 设置的。 loadUserInfo() 没有返回任何信息。我第一次尝试它时它有效,但重新打开并关闭应用程序后它总是返回空。我不确定每次打开应用程序时是否会保存结构。我应该使用不同的方法来完成这些任务吗?

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!
 }

【问题讨论】:

  • 将用户模型设为单例,然后创建用户类型数组的全局变量,然后获取值并保存在该数组中。这就是方法。如果这不是您的答案,请提供您的代码的更多详细信息,以便我们为您提供帮助。
  • 从用户类型的全局变量中,您可以填充您的 uipickerview。

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


【解决方案1】:

试试:

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)
        }
}

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2011-06-10
    • 2022-12-22
    • 2010-09-17
    • 1970-01-01
    相关资源
    最近更新 更多