How do you add a storage Reference in swift for Fi

2019-08-17 12:14发布

问题:

How do you create a field of type storage reference in the swift dictionary format using swift. I keep on getting an error saying that FIRStorageReference. Thank you in advance for your help. This is the code below, photoRef is a storage reference:

let newFirePhotoRef = newUserRef.collection("Photos").document()
photoRef = photoRef.child(newFirePhotoRef.documentID)

newFirePhotoRef.setData([
  "Date":Date(),
  "Photo Reference in Storage": photoRef 
])

This is the error I receive:

Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: FIRStorageReference (found in field Photo Reference in Storage)'

I would like to create this field in swift

回答1:

You can't store objects of type FIRStorageReference in Firestore. Instead, convert the FIRStorageReference object to a string, and store the string. You could use some combination of bucket and fullPath to build a string that suits your needs.



回答2:

let db = Firestore.firestore()
//this is to store reference 
let ref =  db.document("listing/v4/content/\(key)")

let dataToWriteInFireStore :[String:Any] = [
   "_ref": ref,
   "id": 4,
   "timestamp": Int(Date().millisecondsSince1970)
]

db.collection("user_favs/v4/\(FIRUserID)").document("user_fav").setData(dataToWiteInFireStore) {
    err in
    if let err = err {
       print("Error writing document: \(err)")
    } else {
       print("Document successfully written!")
    }

   }