получить значения из структуры FireStore в Swift - PullRequest
0 голосов
/ 24 октября 2018

Я запрашиваю некоторые данные из своего пожарного магазина и помещаю их в свои Usersdata, но я не знаю, как получить мои значения из Usersdata.

Пожалуйста, помогите мне запросить мои данные!

Это моя структура на примере Firestroe

struct Usersdata {
let uid:String?
let facebook:String?
let google:String?
let name:String?
let age:Int?
let birthday:String?
let smokeage:Int?
let smokeaddiction:Int?
let smokebrand:String?
let gold:Int?
let score:Int?
let fish:Int?
let shit:Int?
let userimage:String?
init?(dictionary: [String: Any]) {
    guard let uid = dictionary["uid"] as? String else { return nil }
    self.uid = uid
    self.facebook = dictionary["facebook"] as? String
    self.google = dictionary["google"] as? String
    self.name = dictionary["name"] as? String
    self.age = dictionary["age"] as? Int
    self.birthday = dictionary["birthday"] as? String
    self.smokeage = dictionary["smokeage"] as? Int
    self.smokeaddiction = dictionary["smokeaddiction"] as? Int
    self.smokebrand = dictionary["smokebrand"] as? String
    self.gold = dictionary["gold"] as? Int
    self.score = dictionary["score"] as? Int
    self.fish = dictionary["fish"] as? Int
    self.shit = dictionary["shit"] as? Int
    self.userimage = dictionary["userimage"] as? String
    }   
}

Это моя функция для запроса данных из Firebase

 func test(schema:String , collection:String , document : String){
    let queryRef = db.collection("Users").document(userID).collection(collection).document(document)
    queryRef.getDocument { (document, error) in
        if let user = document.flatMap({
            $0.data().flatMap({ (data) in
                return Usersdata(dictionary: data)
            })
        }) {
            print("Success \(user)")
        } else {
            print("Document does not exist")
        }
    }
}

1 Ответ

0 голосов
/ 24 октября 2018

Я думаю, вы спрашиваете, как работать со структурой с данными Firebase.Вот решение, которое будет читать известного пользователя, заполнять структуру этими данными, а затем печатать uid и имя.

Предположим, структура

Users
  uid_0
    name: "Henry"

, а затем структура для хранения этогоdata

struct Usersdata {
    let uid:String?
    let user_name:String?
    init(aDoc: DocumentSnapshot) {
        self.uid = aDoc.documentID
        self.user_name = aDoc.get("name") as? String ?? ""
    }
}

и функция для чтения этого пользователя, заполнение структуры и вывод данных из struct

func readAUser() {
    let docRef = self.db.collection("Users").document("uid_0")
    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let aUser = Usersdata(aDoc: document)
            print(aUser.uid, aUser.user_name)
        } else {
            print("Document does not exist")
        }
    }
}

и вывода

uid_0 Henry
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...