- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个社交媒体应用程序,并在此过程中尝试在我的数据库中创建一个 friend 列表。我对数据库的结构有大致的了解,但在实际代码中需要帮助,因为我对 Swift 开发还很陌生。
这是我当前的数据库结构:
users
uid_0
name: "John"
email: "john@gmail.com"
uid_1
name: "Doe"
email: "doe@gmail.com"
uid_2
name: "User"
email: "user@gmail.com"
这是我的注册 View Controller 代码
import Foundation
import Firebase
import SwiftKeychainWrapper
class SignUpVC: UIViewController {
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var passField: UITextField!
@IBOutlet weak var cPassField: UITextField!
@IBOutlet weak var SignUpButton: UIButton!
var userUid: String!
var email: String!
var username: String!
var password: String!
var cPassword: String!
var friends: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
// Do any additional setup after loading the view.
}
override func viewDidDisappear(_ animated: Bool) {
if let _ = KeychainWrapper.standard.string(forKey: "uid") {
performSegue(withIdentifier: "toLogin", sender: nil)
}
}
func printAlert(Header: String, Message: String) {
let alert = UIAlertController(title: Header, message: Message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alert, animated: true)
}
@IBAction func createAccount (_ sender: AnyObject) {
if password != cPassword {
self.printAlert(Header: "Error", Message: "Passwords Do Not Match")
}
else{
guard let email = emailField.text, let password = passField.text, let username = usernameField.text else {
print("Form is not valid")
return
}
Auth.auth().createUser(withEmail: email, password: password, completion: { (res, error) in
if let error = error {
print(error)
if let error = AuthErrorCode(rawValue: error._code) {
switch error {
case .invalidEmail:
self.printAlert(Header: "Error", Message: "invalid email")
case .emailAlreadyInUse:
self.printAlert(Header: "Error", Message: "in use")
default:
self.printAlert(Header: "Error", Message: "Create User Error")
}
}
return
}
guard let uid = res?.user.uid else {
return
}
//successfully authenticated user
let ref = Database.database().reference(fromURL: "https://messagingdemo-af075.firebaseio.com/")
let usersReference = ref.child("users").child(uid)
let values = ["username": username, "email": email, "password": password]
usersReference.child("friends").setValue(self.friends)
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if let err = err {
print(err)
return
}
self.performSegue(withIdentifier: "toHome", sender: nil)
})
})
}
}
}我正在尝试实现如下所示的数据库结构:
users
uid_1
name: "John"
email: "john@gmail.com"
friends
uid_2: = true
uid_3: = false
uid_2
name: "Doe"
email: "doe@gmail.com"
uid_3
name: "User"
email: "user@gmail.com"
我假设当新用户注册时,我必须将每个创建的用户 ID 添加到每个用户的好友列表中。但是,我不知道如何以编程方式快速编写该代码,希望得到一些帮助。
最佳答案
您应该通过在 View Controller 中编写的代码段来使用代表用户的模型类。
例如(查看 Swift Structures and Classes 文档):
class User {
var uID: String!
var email: String!
var name: String!
var password: String!
var friends: [String] = []
}
然后在 View Controller 中初始化User
列表。
var users: [User]
因此,User 模型(在您的数据库中)看起来几乎相同,除了属性 friends
之外。由于它的类型为 [String]
(字符串列表),因此它应该如下所示:
我们假设 John 有 friend “user2”、“user4”和“user9”。
uID : "uID_1"
email : "john@mail.com"
name : "John"
password : "..."
friends : ["uID_2", "uID_4", "uID_9"]
我不会详细介绍,但是:
您还可以采用另一种方式,将好友列表与用户分开。
"uID_1" : ["uID_2", "uID_4", "uID_9"]
"uID_2" : ["uID_1", "uID_6"]
...
...只是说有很多方法可以做到...
关于ios - Firebase-Swift : How to add every created user to a friends list when created?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55805775/
我是一名优秀的程序员,十分优秀!