gpt4 book ai didi

swift - RxSwift 和 MVVM : observable not executing without binding

转载 作者:行者123 更新时间:2023-11-28 15:19:24 24 4
gpt4 key购买 nike

我是 RxSwift 的新手,正在尝试使用 MVVM 架构实现应用程序。我有 View 模型:

class CategoriesViewModel {

fileprivate let api: APIService
fileprivate let database: DatabaseService

let categories: Results<Category>

// Input
let actionRequest = PublishSubject<Void>()

// Output
let changeset: Observable<(AnyRealmCollection<Category>, RealmChangeset?)>
let apiSuccess: Observable<Void>
let apiFailure: Observable<Error>

init(api: APIService, database: DatabaseService) {
self.api = api
self.database = database

categories = database.realm.objects(Category.self).sorted(byKeyPath: Category.KeyPath.name)

changeset = Observable.changeset(from: categories)

let requestResult = actionRequest
.flatMapLatest { [weak api] _ -> Observable<Event<[Category]>> in
guard let strongAPI = api else {
return Observable.empty()
}

let request = APIService.MappableRequest(Category.self, resource: .categories)
return strongAPI.mappedArrayObservable(from: request).materialize()
}
.shareReplayLatestWhileConnected()

apiSuccess = requestResult
.map { $0.element }
.filterNil()
.flatMapLatest { [weak database] newObjects -> Observable<Void> in
guard let strongDatabase = database else {
return Observable.empty()
}

return strongDatabase.updateObservable(with: newObjects)
}

apiFailure = requestResult
.map { $0.error }
.filterNil()
}

}

我在 View Controller 中有以下 bininds:

   viewModel.apiSuccess
.map { _ in false }
.bind(to: refreshControl.rx.isRefreshing)
.disposed(by: disposeBag)

viewModel.apiFailure
.map { _ in false }
.bind(to: refreshControl.rx.isRefreshing)
.disposed(by: disposeBag)

但是如果我评论绑定(bind),数据库更新部分将停止执行。无论如何我都需要让它执行,而不是在 View 模型中使用处理包。可能吗?

还有一点额外的问题:我应该像在我的代码中那样使用 api/databasereturn Observable.empty()查看模型代码,或者我可以安全地使用 unowned api/unowned database 吗?

谢谢。

更新:

APIService 中可观察到的返回函数:

func mappedArrayObservable<T>(from request: MappableRequest<T>) -> Observable<[T]> {
let jsonArray = SessionManager.jsonArrayObservable(with: request.urlRequest, isSecured: request.isSecured)
return jsonArray.mapResponse(on: mappingSheduler, { Mapper<T>().mapArray(JSONArray: $0) })
}

最佳答案

除非有订阅者准备接收结果,否则工作不会完成。

你的 DatabaseService 需要有一个 dispose 包并订阅 Observable<[Category]> .像这样的东西:

class ProductionDatabase: DatabaseService {

var categoriesUpdated: Observable<Void> { return _categories }

func updateObservable(with categories: Observable<[Category]>) {
categories
.subscribe(onNext: { [weak self] categories in
// store categories an then
self?._categories.onNext()
})
.disposed(by: bag)
}

private let _categories = PublishSubject<Void>()
private let bag = DisposeBag()
}

然后 apiSuccess = database.categoriesUpdateddatabase.updateObservable(with: requestResult.map { $0.element }.filterNil())

关于swift - RxSwift 和 MVVM : observable not executing without binding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46285878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com