RxSwift将可观测阵列与对象阵列相结合
我正在尝试使用RxSwift从Firebase获取数据。我正在使用来执行API调用 因此,我的数据库如下所示: 集合类别(它有属性:title、about等)内有另一个集合,名为清单。要获取清单,我需要使用类别集合的RxSwift将可观测阵列与对象阵列相结合,swift,firebase,observable,rx-swift,Swift,Firebase,Observable,Rx Swift,我正在尝试使用RxSwift从Firebase获取数据。我正在使用来执行API调用 因此,我的数据库如下所示: 集合类别(它有属性:title、about等)内有另一个集合,名为清单。要获取清单,我需要使用类别集合的documentId。这是两个不同的API调用,但我想合并结果 这就是我到目前为止所做的: func fetchCategories() -> Observable<[ManifestCategory]> { let ref = self.d
documentId
。这是两个不同的API调用,但我想合并结果
这就是我到目前为止所做的:
func fetchCategories() -> Observable<[ManifestCategory]> {
let ref = self.db.collection(FirebaseCollection.manifestCategories.collectionPath)
return ref.rx.getDocuments().map({ snapshot in
return snapshot.documents.map({ doc in
var category = ManifestCategory.init(JSON: doc.data())
category?.documentId = doc.documentID
return category
}).compactMap({ $0 })
})
}
func fetchManifests(categoryId: String) -> Observable<[Manifest]> {
let ref = self.db.collection(FirebaseCollection.manifests(categoryId: categoryId).collectionPath)
return ref.rx.getDocuments().map({ snapshot in
return snapshot.documents.map({ doc in
var manifest = Manifest.init(JSON: doc.data())
manifest?.documentId = doc.documentID
return manifest
}).compactMap({ $0 })
})
}
func fetchCategories()->可观察{
let ref=self.db.collection(FirebaseCollection.manifestCategories.collectionPath)
返回ref.rx.getDocuments()
返回snapshot.documents.map({doc in
var category=ManifestCategory.init(JSON:doc.data())
类别?.documentId=doc.documentId
退货类别
}).compactMap({$0})
})
}
func fetchManifests(categoryId:String)->可观察{
let ref=self.db.collection(FirebaseCollection.manifests(categoryId:categoryId.collectionPath)
返回ref.rx.getDocuments()
返回snapshot.documents.map({doc in
var manifest=manifest.init(JSON:doc.data())
清单?.documentId=doc.documentId
返回舱单
}).compactMap({$0})
})
}
是否有任何方法可以将清单数组放入类别对象中
谢谢大家! 您应该尝试以下方法:
func fetchCategories() -> Observable<[ManifestCategory]> {
let ref = self.db.collection(FirebaseCollection.manifestCategories.collectionPath)
return ref.rx.getDocuments()
.map { snapshot in
return snapshot.documents
.map { doc in
var category = ManifestCategory.init(JSON: doc.data())
category?.documentId = doc.documentID
return category
}
.compactMap { $0 }
}
.flatMapLatest { [weak self] categories -> Observable<[ManifestCategory]> in
guard let self = self else {
return .empty()
}
let observables = categories.map { category -> ([Manifest], String) in
self.fetchManifests(categoryId: category.documentId)
.map { ($0, category.documentId) }
}
return Observable.zip(observables)
.map { tuple -> [ManifestCategory] in
tuple.compactMap { manifests, id in
if var category = categories.first(where: { $0.documentId == id }) {
category.manifests = manifests
return category
}
return nil
}
}
}
}
func fetchCategories()->可观察{
let ref=self.db.collection(FirebaseCollection.manifestCategories.collectionPath)
返回ref.rx.getDocuments()
.map{中的快照
返回快照文件
.map{doc在
var category=ManifestCategory.init(JSON:doc.data())
类别?.documentId=doc.documentId
退货类别
}
.compactMap{$0}
}
.flatmap最新{[弱自我]类别->在
保护自己=保护自己{
return.empty()
}
让observables=categories.map{category->([Manifest],String)在
self.fetchManifests(categoryId:category.documentId)
.map{($0,category.documentId)}
}
return Observable.zip(可观测)
.map{tuple->[ManifestCategory]中的
tuple.compactMap{清单,id在
如果var category=categories.first(其中:{$0.documentId==id}){
category.manifests=清单
退货类别
}
归零
}
}
}
}