Skip to content

Instantly share code, notes, and snippets.

@robertvunabandi
Created November 18, 2025 05:06
Show Gist options
  • Select an option

  • Save robertvunabandi/ce24634019b08081c01d68b26e7dd9eb to your computer and use it in GitHub Desktop.

Select an option

Save robertvunabandi/ce24634019b08081c01d68b26e7dd9eb to your computer and use it in GitHub Desktop.
//
// AuthorsDataFetcher.swift
// Reading Journal
//
//
import Foundation
final class AuthorsDataFetcher: DataFetcher {
typealias Store = AuthorsDataStore
static let Enum = DataType.authors
private var store: Store
required init(store: Store) {
self.store = store
}
func fetchAll() async throws {
let authors = try await NR.Author.GetAll().fetch()
await MainActor.run { [weak self] in
guard let self else { return }
store.add(authors: authors)
}
}
func add(name: String) async throws -> MAuthor {
let author = try await NR.Author.Add(name: name).fetch()
await MainActor.run { [weak self] in
guard let self else { return }
store.add(author: author)
}
return author
}
func update(authorId: String, name: String) async throws {
let updatedAuthor = try await NR.Author
.Update(authorId: authorId, name: name)
.fetch()
await MainActor.run { [weak self] in
guard let self else { return }
store.update(author: updatedAuthor)
}
}
func delete(author: MAuthor) async throws -> NR.CommonTypes.DeleteResponse {
let response = try await NR.Author.Delete(authorId: author.id).fetch()
if response.deleted {
await MainActor.run { [weak self] in
guard let self else { return }
store.remove(author: author)
}
}
return response
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment