There is also an explanation in English.
import AsyncFirebaseAuth
import FirebaseAuth
let auth = Auth.auth()
Task {
// Example of using stateDidChangeAsync() function
for await user in auth.stateDidChangeAsync() {
if let user = user {
print("User is signed in: \(user.uid)")
} else {
print("User is signed out")
}
}
}import AsyncFirebaseDatabase
import FirebaseDatabase
let databaseRef = Database.database().reference()
// Example of using async(eventType:) function
Task {
let query = databaseRef.child("some/path")
for await snapshot in query.async(eventType: .value) {
print("Received snapshot: \(snapshot)")
}
}
// Example of using observeSingleEvent(_:) function
Task {
let query = databaseRef.child("some/other/path")
do {
let snapshot = try await query.observeSingleEvent(.value)
print("Single event snapshot: \(snapshot)")
} catch {
print("Error observing single event: \(error)")
}
}import AsyncFirebaseFirestore
import FirebaseFirestore
let firestore = Firestore.firestore()
let query = firestore.collection("your_collection_name")
// Example of using async() function
Task {
for await snapshot in query.async() {
print("Received snapshot: \(snapshot)")
}
}
// Example of using async(as:documentSnapshotMapper:querySnapshotMapper:) function
Task {
struct YourModel: Decodable {
let id: String
let name: String
}
let documentMapper: (DocumentSnapshot) throws -> YourModel? = { document in
guard let data = document.data() else { return nil }
return YourModel(id: document.documentID, name: data["name"] as? String ?? "")
}
let queryMapper: (QuerySnapshot, (DocumentSnapshot) throws -> YourModel?) -> [YourModel] = { querySnapshot, documentMapper in
return querySnapshot.documents.compactMap { try? documentMapper($0) }
}
for await models in query.async(as: YourModel.self, documentSnapshotMapper: documentMapper, querySnapshotMapper: queryMapper) {
for model in models {
print("Received model: \(model)")
}
}
}import AsyncFirebaseFirestore
import FirebaseFirestore
// Example of using async() function with DocumentReference
let documentRef = firestore.collection("your_collection_name").document("your_document_id")
Task {
for await snapshot in documentRef.async() {
print("Received document snapshot: \(snapshot)")
}
}
// Example of using async(as:documentSnapshotMapper:) function with DocumentReference
struct YourModel: Decodable {
let id: String
let name: String
}
let documentMapper: (DocumentSnapshot) throws -> YourModel? = { document in
guard let data = document.data() else { return nil }
return YourModel(id: document.documentID, name: data["name"] as? String ?? "")
}
Task {
for await model in documentRef.async(as: YourModel.self, documentSnapshotMapper: documentMapper) {
if let model = model {
print("Received model: \(model)")
}
}
}import AsyncFirebaseStorage
import FirebaseStorage
// Example of using asyncStream(status:) function with StorageObservableTask
let storageRef = Storage.storage().reference().child("your_file_path")
let uploadTask = storageRef.putData(yourData, metadata: nil)
Task {
for await snapshot in uploadTask.asyncStream(status: .success) {
print("Upload progress: \(snapshot.progress?.fractionCompleted ?? 0)")
}
}dependencies: [
.package(url: "https://github.com/Monsteel/AsyncFirebase.git", .upToNextMajor(from: "0.0.1"))
]| 회사 | 설명 |
|---|---|
![]() |
Firebase 및 Firestore를 사용중인 정육각 런즈 앱에서 사용중입니다. |
개선의 여지가 있는 모든 것들에 대해 열려있습니다.
PullRequest를 통해 기여해주세요. 🙏
AsyncFirebase 는 MIT 라이선스로 이용할 수 있습니다. 자세한 내용은 라이선스 파일을 참조해 주세요.
AsyncFirebase is available under the MIT license. See the LICENSE file for more info.
이영은(Tony) | dev.e0eun@gmail.com
