RxSwift和MVVM:observable不执行而不绑定

时间:2017-09-18 18:24:32

标签: swift mvvm rx-swift

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

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()
}

}

我在视图控制器中有以下binginds:

   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)

但是如果我评论绑定,那么部分数据库更新就会停止执行。无论如何我都需要让它执行,而不需要在视图模型中使用dispose bag。有可能吗?

还有一点其他问题:我应该在我的视图模型代码中使用与api / databasereturn Observable.empty()类似的弱强舞,或者我可以使用unowned api / unowned database安全吗?

感谢。

UPD:

APIService中的return observable函数:

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) })
}

1 个答案:

答案 0 :(得分:1)

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

您的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())