実現したいこと
前提
https://firebase.google.com/docs/firestore/solutions/swift-codable-data-mapping?hl=ja#handling_errors
firestoreのドキュメントで理解できない記述を見つけたため質問させていただきたいです。
collectionRef.addDocument(from: newColor)の前に try がついていますが、これはなぜ記述しなければいけないのでしょうか。そしてどこにthrowsがあるのでしょうか。
swift
1 func addColorEntry() { 2 let collectionRef = db.collection("colors") 3 do { 4 let newDocReference = try collectionRef.addDocument(from: newColor) 5 print("ColorEntry stored with new document reference: \(newDocReference)") 6 } 7 catch { 8 print(error) 9 } 10 }
こちらはfirestoreドキュメントの別のサンプルコードですが、このコードではtryをつけずにaddDocumentメソッドを呼び出していて、違いがわかりません。
do,try,catch,throwsの基本的な文法は一応理解しているつもりです。
わかりづらい文章になってしまっているかもしれませんが、ご教授いただけますと嬉しいです。よろしくお願いいたします。
swift
1// Add a new document with a generated id. 2var ref: DocumentReference? = nil 3ref = db.collection("cities").addDocument(data: [ 4 "name": "Tokyo", 5 "country": "Japan" 6]) { err in 7 if let err = err { 8 print("Error adding document: \(err)") 9 } else { 10 print("Document added with ID: \(ref!.documentID)") 11 } 12}
念の為全てのコードを記載します。
swift
1class MappingColorsViewModel: ObservableObject { 2 @Published var colorEntries = [ColorEntry]() 3 @Published var newColor = ColorEntry.empty 4 @Published var errorMessage: String? 5 6 private var db = Firestore.firestore() 7 private var listenerRegistration: ListenerRegistration? 8 9 public func unsubscribe() { 10 if listenerRegistration != nil { 11 listenerRegistration?.remove() 12 listenerRegistration = nil 13 } 14 } 15 16 func subscribe() { 17 if listenerRegistration == nil { 18 listenerRegistration = db.collection("colors") 19 .addSnapshotListener { [weak self] (querySnapshot, error) in 20 guard let documents = querySnapshot?.documents else { 21 self?.errorMessage = "No documents in 'colors' collection" 22 return 23 } 24 25 self?.colorEntries = documents.compactMap { queryDocumentSnapshot in 26 let result = Result { try queryDocumentSnapshot.data(as: ColorEntry.self) } 27 28 switch result { 29 case .success(let colorEntry): 30 if let colorEntry = colorEntry { 31 // A ColorEntry value was successfully initialized from the DocumentSnapshot. 32 self?.errorMessage = nil 33 return colorEntry 34 } 35 else { 36 // A nil value was successfully initialized from the DocumentSnapshot, 37 // or the DocumentSnapshot was nil. 38 self?.errorMessage = "Document doesn't exist." 39 return nil 40 } 41 case .failure(let error): 42 // A ColorEntry value could not be initialized from the DocumentSnapshot. 43 switch error { 44 case DecodingError.typeMismatch(_, let context): 45 self?.errorMessage = "\(error.localizedDescription): \(context.debugDescription)" 46 case DecodingError.valueNotFound(_, let context): 47 self?.errorMessage = "\(error.localizedDescription): \(context.debugDescription)" 48 case DecodingError.keyNotFound(_, let context): 49 self?.errorMessage = "\(error.localizedDescription): \(context.debugDescription)" 50 case DecodingError.dataCorrupted(let key): 51 self?.errorMessage = "\(error.localizedDescription): \(key)" 52 default: 53 self?.errorMessage = "Error decoding document: \(error.localizedDescription)" 54 } 55 return nil 56 } 57 } 58 } 59 } 60 } 61 62 func addColorEntry() { 63 let collectionRef = db.collection("colors") 64 do { 65 let newDocReference = try collectionRef.addDocument(from: newColor) 66 print("ColorEntry stored with new document reference: \(newDocReference)") 67 } 68 catch { 69 print(error) 70 } 71 } 72}
試したこと
補足情報(FW/ツールのバージョンなど)

回答1件
あなたの回答
tips
プレビュー