Skip to content

Commit

Permalink
♻️ Update code to match changes in firebase/firebase-ios-sdk#9101
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Friese <[email protected]>
  • Loading branch information
peterfriese committed Feb 18, 2022
1 parent 99c636d commit 22c6c33
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 132 deletions.
50 changes: 18 additions & 32 deletions FirestoreCodableSamples/Screens/MappingArraysScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,24 @@ class MappingArraysViewModel: ObservableObject {
private func fetchBook(documentId: String) {
let docRef = db.collection("books").document(documentId)

docRef.getDocument { document, error in
if let error = error as NSError? {
self.errorMessage = "Error getting document: \(error.localizedDescription)"
}
else {
let result = Result { try document?.data(as: BookWithGenre.self) }
switch result {
case .success(let book):
if let book = book {
// A Book value was successfully initialized from the DocumentSnapshot.
self.book = book
self.errorMessage = nil
}
else {
// A nil value was successfully initialized from the DocumentSnapshot,
// or the DocumentSnapshot was nil.
self.errorMessage = "Document doesn't exist."
}
case .failure(let error):
// A Book value could not be initialized from the DocumentSnapshot.
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
docRef.getDocument(as: BookWithGenre.self) { result in
switch result {
case .success(let book):
self.book = book
self.errorMessage = nil
case .failure(let error):
// A Book value could not be initialized from the DocumentSnapshot.
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,24 @@ class MappingArraysWithNestedTypesViewModel: ObservableObject {
private func fetchBook(documentId: String) {
let docRef = db.collection("books").document(documentId)

docRef.getDocument { document, error in
if let error = error as NSError? {
self.errorMessage = "Error getting document: \(error.localizedDescription)"
}
else {
let result = Result { try document?.data(as: BookWithTags.self) }
switch result {
case .success(let book):
if let book = book {
// A Book value was successfully initialized from the DocumentSnapshot.
self.book = book
self.errorMessage = nil
}
else {
// A nil value was successfully initialized from the DocumentSnapshot,
// or the DocumentSnapshot was nil.
self.errorMessage = "Document doesn't exist."
}
case .failure(let error):
// A Book value could not be initialized from the DocumentSnapshot.
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
docRef.getDocument(as: BookWithTags.self) { result in
switch result {
case .success(let book):
self.book = book
self.errorMessage = nil
case .failure(let error):
// A Book value could not be initialized from the DocumentSnapshot.
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
}
}
Expand Down Expand Up @@ -130,7 +116,7 @@ struct MappingArraysWithNestedTypesScreen: View {
.padding(.horizontal, 4)
.padding(.vertical, 2)
.background(tag.color)
.cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
.cornerRadius(3.0)
}
Button(action: viewModel.addTag) {
Label("Add a tag", systemImage: "plus")
Expand Down
51 changes: 19 additions & 32 deletions FirestoreCodableSamples/Screens/MappingCustomTypesScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,25 @@ class MappingCustomTypesViewModel: ObservableObject {
private func fetchBook(documentId: String) {
let docRef = db.collection("books").document(documentId)

docRef.getDocument { document, error in
if let error = error as NSError? {
self.errorMessage = "Error getting document: \(error.localizedDescription)"
}
else {
let result = Result { try document?.data(as: BookWithCoverImages.self) }
switch result {
case .success(let book):
if let book = book {
// A Book value was successfully initialized from the DocumentSnapshot.
self.book = book
self.errorMessage = nil
}
else {
// A nil value was successfully initialized from the DocumentSnapshot,
// or the DocumentSnapshot was nil.
self.errorMessage = "Document doesn't exist."
}
case .failure(let error):
// A Book value could not be initialized from the DocumentSnapshot.
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
docRef.getDocument(as: BookWithCoverImages.self) { result in
switch result {
case .success(let book):
// A Book value was successfully initialized from the DocumentSnapshot.
self.book = book
self.errorMessage = nil
case .failure(let error):
// A Book value could not be initialized from the DocumentSnapshot.
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
}
}
Expand Down
115 changes: 80 additions & 35 deletions FirestoreCodableSamples/Screens/MappingSimpleTypesScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,98 @@ class MappingSimpleTypesViewModel: ObservableObject {

private var db = Firestore.firestore()

/// Use this to alteratively use async/await or callback-based implementations
let useAsync = false

func fetchAndMap() {
fetchBook(documentId: "hitchhiker")
if useAsync {
Task {
await fetchBookAsync(documentId: "hitchhiker")
}
}
else {
fetchBook(documentId: "hitchhiker")
}
}

func fetchAndMapNonExisting() {
fetchBook(documentId: "does-not-exist")
if useAsync {
Task {
await fetchBookAsync(documentId: "does-not-exist")
}
}
else {
fetchBook(documentId: "does-not-exist")
}
}

func fetchAndTryMappingInvalidData() {
fetchBook(documentId: "invalid-data")
if useAsync {
Task {
await fetchBookAsync(documentId: "invalid-data")
}
}
else {
fetchBook(documentId: "invalid-data")
}
}

/// Alternative implementation that shows how to use async/await to call `getDocument`
/// This needs to be marked as @MainActor so that we can safely access the errorMessage
/// published property when encountering an error.
@MainActor
private func fetchBookAsync(documentId: String) async {
let docRef = db.collection("books").document(documentId)
do {
self.book = try await docRef.getDocument(as: Book.self)
}
catch {
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
}
}

private func fetchBook(documentId: String) {
let docRef = db.collection("books").document(documentId)

docRef.getDocument { document, error in
if let error = error as NSError? {
self.errorMessage = "Error getting document: \(error.localizedDescription)"
}
else {
let result = Result { try document?.data(as: Book.self) }
switch result {
case .success(let book):
if let book = book {
// A Book value was successfully initialized from the DocumentSnapshot.
self.book = book
self.errorMessage = nil
}
else {
// A nil value was successfully initialized from the DocumentSnapshot,
// or the DocumentSnapshot was nil.
self.errorMessage = "Document doesn't exist."
}
case .failure(let error):
// A Book value could not be initialized from the DocumentSnapshot.
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
// If you expect that a document might *not exist*, use an optional type (Book?.self)
// and then perform an `if let book = book` dance to handle this case.
docRef.getDocument(as: Book?.self) { result in
switch result {
case .success(let book):
if let book = book {
// A Book value was successfully initialized from the DocumentSnapshot.
self.book = book
self.errorMessage = nil
}
else {
// A nil value was successfully initialized from the DocumentSnapshot,
// or the DocumentSnapshot was nil.
self.errorMessage = "Document doesn't exist."
}
case .failure(let error):
// A Book value could not be initialized from the DocumentSnapshot.
switch error {
case DecodingError.typeMismatch(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.valueNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.keyNotFound(_, let context):
self.errorMessage = "\(error.localizedDescription): \(context.debugDescription)"
case DecodingError.dataCorrupted(let key):
self.errorMessage = "\(error.localizedDescription): \(key)"
default:
self.errorMessage = "Error decoding document: \(error.localizedDescription)"
}
}
}
Expand Down

0 comments on commit 22c6c33

Please sign in to comment.