FireStore-Swift无法识别返回值是一个查询快照

时间:2019-02-27 01:36:07

标签: swift

我读取了带有QueryDocumentSnapshot返回值的firestore okie,但函数未捕获它。图片数据和data()返回如下。 我不确定问题是否来自时间戳格式。尝试将其设置为可选,但没有帮助...如果您可以看一下并提供建议,那就太好了

以下是使用data()函数进行查询的结果

wow here bug ["owner_key": oQmUMHYAWaek5RkQSe2Ct9pABjl2, "invitation_type": wedding, "updated_date": 2019-02-26 17:00:00 +0000, "is_deleted": 0, "invite_id": DSDDSDDDRDS, "created_date": 2019-02-25 17:00:00 +0000]

以下是Firestore中的数据

enter image description here

此功能可捕获消防站的退货

guard let invite = InvitationModel(document: change.document) else {
        print("bug in coding")
        return
    }

class InvitationModel {

var created_date: Timestamp
var updated_date: Timestamp
var invitation_card: URL? = nil
var invitation_type: String
var is_deleted: Bool
var owner_key: String
var invite_id: String

init?(document: QueryDocumentSnapshot) {
    let data = document.data()

    guard let created_date = data[Constants.invite_record.created_date] as? Timestamp else {
        return nil
    }
    guard let updated_date = data[Constants.invite_record.updated_date] as? Timestamp else {
        return nil
    }
    if let urlString = data[Constants.invite_record.invitation_card] as? String, let url = URL(string: urlString) {
        self.invitation_card = url
    } else {
        return nil
    }
    guard let invitation_type = data[Constants.invite_record.invitation_type] as? String else {
        return nil
    }
    guard let is_deleted = data[Constants.invite_record.is_deleted] as? Bool else {
        return nil
    }
    guard let owner_key = data[Constants.invite_record.owner_key] as? String else {
        return nil
    }

    self.created_date = created_date
    self.updated_date = updated_date
    self.invitation_type = invitation_type
    self.is_deleted = is_deleted
    self.owner_key = owner_key
    self.invite_id = document.documentID

}

}

以下功能可从Firestore成功读取数据。返回上面发布的数据

func loadMyInviting(count: Int, completion: @escaping ([MockInvite]) -> Void ) {
    print("current userid", _currentUser!)
    let inviteRef = db.collection("invitations")
    let query = inviteRef.whereField(Constants.invite_record.owner_key, isEqualTo: _currentUser!).order(by: "created_date", descending: true).limit(to: 10).addSnapshotListener { querySnapshot, error in
        guard let snapshot = querySnapshot else {
            print("Error listening for channel updates: \(error?.localizedDescription ?? "No error")")
            return
        }

        snapshot.documentChanges.forEach { change in
                            self.handleDocumentChange(change)
            let x = change.document.data()
            let y = InviteRecord(json: x)
            print("dhdanh", y)
        }
            completion(self.invitations)
    }

}
private func handleDocumentChange(_ change: DocumentChange) {
    print("wow here bug", change.document.data())
    guard let invite = InvitationModel(document: change.document) else {
        print("bug in coding")
        return
    }
    switch change.type {
    case .added:

        print("add is not supported")

    case .modified:
        print("modify is not supported")
    default:
        break
    }
}

0 个答案:

没有答案