FIRUser와 FIRDatabaseReference를 취하는 fusing 이니셜 라이저로 클래스를 생성하려고합니다. Firebase 데이터베이스에서 데이터를 다운로드하고 반환되는 것을 기반으로 자체 변수를 설정합니다. 그렇지 않으면 이니셜 라이저가 실패합니다.Firebase Data Pull에서 클래스 초기화하기
데이터가 클로저에 다운로드되지만 다운로드가 발생하지 않은 것처럼 모든 것이 기본값으로 되돌아갑니다.
나는이 서버 로직을 클래스 초기화 자 안에 포함하고 정말로 싶습니다. 어떤 식 으로든 이걸 안전하게 할 수 있을까요? 나는 많은 것을 시도해 왔고 그것을 이해할 수 없다.
init?(from user: FIRUser, withUserReference ref: FIRDatabaseReference){
let userID = user.uid
var init_succeeded = false
//These values don't matter. If the init fails
//It'll return an empty class.
//Yes this is a hack lol
self.incognito = false
self.email = "NO"
self.username = "NOPE"
self.ref = ref
self.fir_user = user
self.mute_all = false
//Getting the information from the database
ref.child(userID).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
//Unpacking user preferences
self.incognito = (value?["incognito"] as? Bool)!
self.mute_all = (value?["mute_all"] as? Bool)!
self.email = (value?["email"] as? String)!
self.username = (value?["username"] as? String)!
init_succeeded = true
}) { (error) in
print("ERROR : \(error.localizedDescription)")
}
if !init_succeeded { return nil }
}
고마워요! - 키넌
도움을 주셔서 감사합니다! 마지막으로 스냅 샷을 클래스 이니셜 라이저에 전달하고 나머지는 처리하도록했습니다. 다시 한 번 감사드립니다! –