FirestoreWrapperCombineiOS
is a Swift wrapper for Firebase Firestore that provides an easy way to perform CRUD operations on Firestore using the Combine framework.
You can install FirestoreWrapperCombineiOS
using the Swift Package Manager.
- In Xcode, open your project and navigate to File > Swift Packages > Add Package Dependency.
- Enter the repository URL
https://github.com/vGebs/FirestoreWrapperCombineiOS.git
and click Next. - Select the version you want to install, or leave the default version and click Next.
- In the "Add to Target" section, select the target(s) you want to use
LocalAuthWrapperiOS
in and click Finish.
Please note that this package relies on:
- FirebaseFirestore
- FirebaseFirestoreSwift
Make sure to have these installed!
Additionally, make sure you also have a GoogleService-info.plist added to your project.
Here is an example service class that uses the FirestoreWrapper
class
import Foundation
import Combine
import FirebaseFirestore
import FirebaseFirestoreSwift
import FirestoreWrapperCombineiOS
class UserService {
private let firestore = FirestoreWrapper.shared
static let shared = UserService()
private let collection = "Users"
private init() {}
func createUser(_ user: User) -> AnyPublisher<Void, Error> {
return firestore.create(collection: collection, data: user)
}
func fetchUser(_ docID: String) -> AnyPublisher<User, Error> {
return firestore.read(collection: collection, documentId: docID)
}
func fetchUsers(_ query: Query) -> AnyPublisher<[User], Error> {
return firestore.read(query: query)
}
func updateUser(_ docID: String, _ user: User) -> AnyPublisher<Void, Error> {
return firestore.update(collection: collection, documentId: docID, data: user)
}
func deleteUser(_ docID: String) -> AnyPublisher<Void, Error> {
return firestore.delete(collection: collection, documentId: docID)
}
func listenOnUser(_ docID: String) -> AnyPublisher<(User, DocumentChangeType), Error> {
return firestore.listenByDocument(collection: collection, documentId: docID)
}
func listenOnUsers(_ query: Query) -> AnyPublisher<[(User, DocumentChangeType)], Error> {
return firestore.listenByQuery(query: query)
}
}
Models used with this package need to abide by the FirestoreProtocol
.
Here is an example model that abides by the FirestoreProtocol
import Foundation
import FirebaseFirestoreSwift
import FirestoreWrapperCombineiOS
struct User: Equatable, FirestoreProtocol, Hashable {
@DocumentID var documentID: String?
var name: String
var height: Int
var birthdate: Date?
}
The protocol states that abiding objects must implement a documentID of type String?
For more information on how to use, please reference the SampleUsage folder inside Sources/FirestoreWrapperCombineiOS/SampleUsage
LocalAuthWrapperiOS
is released under the MIT license. See LICENSE for details.
We welcome contributions to LocalAuthWrapperiOS
. If you have a bug fix or a new feature, please open a pull request.