I try to initial a value under init() method, but I got this error Escaping closure captures mutating 'self' parameter
It work if I put it under onAppear() but it won't call if the screen is on back-group, so It is not what I needed . Attached my coding ..
struct GameRowForDemo: View {
let game : Game
var user : User?
init(game:Game){
self.game = game
let uid = "123"
User.getById(id: uid).then { user in
self.user = user!
}
}
var body: some View {
Text("Text")
}
}
static func getById(id: String) -> Promise<User?> {
let p = Promise<User?> { (resolve , reject) in
let db = Firestore.firestore()
let ref = db.collection("users").document(id)
ref.getDocument { (snapshot, err) in
if let err = err{
reject(err)
}
guard let dict = snapshot?.data() else {
print("No User found")
resolve(nil)
return
}
do {
let data = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
let group = try JSONDecoder.init().decode(User.self, from: data)
resolve(group)
}catch{
reject(error)
}
}
}
return p
}