alickbass / CodableFirebase

Use Codable with Firebase

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sample code not working, setValue wrong?

hixfield opened this issue · comments

If I use the sample code it crashes with "Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(nodeFrom:priority:) Cannot store object of type _SwiftValue at . Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray."

 struct Test: Encodable {
        var integer = Int(10)
        var string  = String("test")
    }

    func writeToFirebase(uid: String, completion: ( (Error?, DatabaseReference?)->() )? = nil ) {
        do {
            let data = try toFirebaseData() as [AnyHashable: Any]
            log.debug("\(data)")
            
            var t = Test()
            firebaseRef(uid: uid).setValue(try FirebaseEncoder().encode(t)) {
                (error, reference) in
                completion?(error, reference)
            }
        } catch let error {
            completion?(error, nil)
        }
    }

I believe the usage of setValue is wrong, if I change it to updateChildValues is works perfectly.
However, issue is that updateChildeValues will not replace the whole data under the key which I need (and that is why I wanted to use setValue).

Well, Firestore documentation is a bit different.

FirestoreManager.shared.db.collection("data").getDocuments { documents, error in
           if let documents = documents {
               for document in documents.documents {
                   guard let model = try? FirestoreDecoder().decode(Model.self, from: document.data()) else { print("Error"); return }
                   print("Model: \(model.name), \(model.email), \(model.location), \(model.details.dealbreakers)")
               }
           } else {
               print("Document does not exist")
           }
}

Don’t understand can you ellaborate?