Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to call setData(from: ) in the Cloud Firestore ios API. How can i fix it?

Working in Swift..

I'm trying to call .setData(from: ) to write a Codable struct to a document in the cloud firestore database as outlined in the Firebase docs here to:

https://firebase.google.com/docs/firestore/manage-data/add-data#custom_objects

However, I'm getting the error: "Argument labels '(from:)' do not match any available overloads"

The odd thing is I was able to build and run on the simulator once, and successfully posted a document, but now obviously the compiler is calling this error and causing the build to fail. relevant code below:

the line that is causing the build to fail:

do {
      try collectionRef.document(lensSet.id.uuidString).setData(from: lensSet)

    } catch let error {
      print(error)
    }

The Codable Struct:

struct LensSet: Codable, Identifiable {
  var id: UUID

  // Coding KEYS
  private enum CodingKeys: String, CodingKey {
      case id
  }
}

It seems like the function they are saying to call in the docs maybe doesn't exist? Any help appreciated!

like image 522
spenser Avatar asked Jan 01 '23 04:01

spenser


1 Answers

The documentation isn't exactly wrong, it's just incomplete in one section.

EDIT: for TL;DR

See the guide from Peter Friese: Mapping Firestore Data in Swift

For the longer explanation

Firebase added an extension to make crafting custom obects easier but didn't mention that extension in the documentation. (I assume its an extension)

The documentation on Custom Objects is an example of the code that needs the extension

Simple fix by adding this to your podfile

pod 'FirebaseFirestoreSwift'

and then do a pod update either in terminal or cocoapods.

Then in your Class just update the imports to include that

import Cocoa
import FirebaseCore
import FirebaseFirestore
import FirebaseFirestoreSwift

For reference the code in the docs was this

try db.collection("cities").document("LA").setData(from: city)

and this

let docRef = db.collection("cities").document("LA")

 docRef.getDocument { (document, error) in
    let result = Result {
    try document.flatMap {
       try $0.data(as: City.self)
    }
 }

and this line

.data(as: City.self)

and

.setData(from:

were the 'troublemakers' that needed the FirebaseFirestoreSwift

like image 117
Jay Avatar answered Jan 18 '23 22:01

Jay