Rx Observable从其他Observable获取值

时间:2017-08-02 15:58:57

标签: ios swift mvvm rx-swift

我是RxSwift和MVVM的新手。

我的viewModel有一个名为rx_fetchItems(for:)的方法,可以从后端获取相关内容,并返回Observable<[Item]>

我的目标是提供名为collectionItems的viewModel的可观察属性,其中包含从rx_fetchItems(for:)返回的最后一个发射元素,以向我的collectionView提供数据。

Daniel T提供了我可能使用的解决方案:

protocol ServerAPI {
    func rx_fetchItems(for category: ItemCategory) -> Observable<[Item]>
}

    struct ViewModel {

        let collectionItems: Observable<[Item]>
        let error: Observable<Error>

        init(controlValue: Observable<Int>, api: ServerAPI) {
            let serverItems = controlValue
                .map { ItemCategory(rawValue: $0) }
                .filter { $0 != nil }.map { $0! } // or use a `filterNil` operator if you already have one implemented.
                .flatMap { api.rx_fetchItems(for: $0)
                    .materialize()
                }
                .filter { $0.isCompleted == false }
                .shareReplayLatestWhileConnected()

            collectionItems = serverItems.filter { $0.element != nil }.dematerialize()
            error = serverItems.filter { $0.error != nil }.map { $0.error! }
        }

    }

这里唯一的问题是我当前的ServerAPI又名FirebaseAPI没有这样的协议方法,因为它设计有一个方法来触发所有这样的请求:

class FirebaseAPI {

    private let session: URLSession

    init() {
        self.session = URLSession.shared
    }

    /// Responsible for Making actual API requests & Handling response
    /// Returns an observable object that conforms to JSONable protocol.
    /// Entities that confrom to JSONable just means they can be initialized with json.
    func rx_fireRequest<Entity: JSONable>(_ endpoint: FirebaseEndpoint, ofType _: Entity.Type ) -> Observable<[Entity]> {

        return Observable.create { [weak self] observer in
            self?.session.dataTask(with: endpoint.request, completionHandler: { (data, response, error) in

                /// Parse response from request.
                let parsedResponse = Parser(data: data, response: response, error: error)
                    .parse()

                switch parsedResponse {

                case .error(let error):
                    observer.onError(error)
                    return

                case .success(let data):

                    var entities = [Entity]()

                    switch endpoint.method {

                    /// Flatten JSON strucuture to retrieve a list of entities.
                    /// Denoted by 'GETALL' method.
                    case .GETALL:

                        /// Key (underscored) is unique identifier for each entity, which is not needed here.
                        /// value is k/v pairs of entity attributes.
                        for (_, value) in data {
                            if let value = value as? [String: AnyObject], let entity = Entity(json: value) {
                                entities.append(entity)
                            }
                        }

                        // Need to force downcast for generic type inference.
                        observer.onNext(entities as! [Entity])
                        observer.onCompleted()

                    /// All other methods return JSON that can be used to initialize JSONable entities 
                    default:
                        if let entity = Entity(json: data) {
                        observer.onNext([entity] as! [Entity])
                        observer.onCompleted()
                    } else {
                        observer.onError(NetworkError.initializationFailure)
                        }
                    }
                }
            }).resume()
            return Disposables.create()
        }
    }
}

rx_fireRequest方法最重要的一点是它需要FirebaseEndpoint

/// Conforms to Endpoint protocol in extension, so one of these enum members will be the input for FirebaseAPI's `fireRequest` method.

enum FirebaseEndpoint {

    case saveUser(data: [String: AnyObject])
    case fetchUser(id: String)
    case removeUser(id: String)

    case saveItem(data: [String: AnyObject])
    case fetchItem(id: String)
    case fetchItems
    case removeItem(id: String)

    case saveMessage(data: [String: AnyObject])
    case fetchMessages(chatroomId: String)
    case removeMessage(id: String)

}

为了使用Daniel T的解决方案,我必须将每个枚举案例从FirebaseEndpoint转换为FirebaseAPI内的方法。在每种方法中,调用rx_fireRequest ...如果我是正确的。

如果能够提供更好的服务器API设计,我很想做出这样的改变。所以简单的问题是,这个重构会改进我的整体API设计以及它与ViewModels的交互方式。我意识到这现在正在演变为代码审查。

另外......以下是该协议方法的实现及其帮助:

 func rx_fetchItems(for category: ItemCategory) -> Observable<[Item]>  {
        // fetched items returns all items in database as Observable<[Item]>
        let fetchedItems = client.rx_fireRequest(.fetchItems, ofType: Item.self)
        switch category {
        case .Local:
            let localItems = fetchedItems
            .flatMapLatest { [weak self] (itemList) -> Observable<[Item]> in
                return self!.rx_localItems(items: itemList)
            }

            return localItems

            // TODO: Handle other cases like RecentlyAdded, Trending, etc..
        }
    }

    // Helper method to filter items for only local items nearby user.
    private func rx_localItems(items: [Item]) -> Observable<[Item]> {
        return Observable.create { observable in
            observable.onNext(items.filter { $0.location == "LA" })
            observable.onCompleted()
            return Disposables.create()
        }
    }

如果我对MVVM或RxSwift或API设计的方法是错误的,请做批评。

3 个答案:

答案 0 :(得分:1)

我知道开始理解RxSwift

很难

我喜欢使用SubjectVariable作为 ViewModel ObservableDriver s的输入作为输出的视图模型

这样,您可以将ViewController上发生的操作绑定到ViewModel,处理逻辑并更新输出

以下是重构代码的示例

查看模型

// Inputs
let didSelectItemCategory: PublishSubject<ItemCategory> = .init()

// Outputs
let items: Observable<[Item]>

init() {
    let client = FirebaseAPI()

    let fetchedItems = client.rx_fireRequest(.fetchItems, ofType: Item.self)

    self.items = didSelectItemCategory
        .withLatestFrom(fetchedItems, resultSelector: { itemCategory, fetchedItems in
            switch itemCategory {
            case .Local:
                return fetchedItems.filter { $0.location == "Los Angeles" }
            default: return []
            }
        })
}

的ViewController

segmentedControl.rx.value
    .map(ItemCategory.init(rawValue:))
    .startWith(.Local)
    .bind(to: viewModel.didSelectItemCategory)
    .disposed(by: disposeBag)

viewModel.items
    .subscribe(onNext: { items in
        // Do something
    })
    .disposed(by: disposeBag)

答案 1 :(得分:0)

你在这里遇到了一个棘手的情况,因为你的observable可以抛出一个错误,一旦它确实抛出一个错误,可观察的序列错误就会被抛出,并且不会再发出任何事件。因此,要处理后续网络请求,您必须重新分配采用您当前正在采用的方法。但是,这通常不适合驱动UI元素(如集合视图),因为每次都必须绑定到重新分配的observable。在驱动UI元素时,您应该倾向于保证不会出错的类型(即变量和驱动程序)。您可以将$document = new DOMDocument(); $document->loadXml($xml); $xpath = new DOMXpath($document); foreach ($xpath->evaluate('//emp[@emp_name="john"]/img') as $img) { var_dump($img->textContent); } 设为Observable<[Item]>,然后您可以将该变量的值设置为来自新网络请求的项目数组。您可以使用RxDataSources或类似的东西将此变量安全地绑定到集合视图。然后,您可以为错误消息创建一个单独的变量,假设let items = Variable<[Item]>([]),来自网络请求的错误消息然后您可以将errorMessage字符串绑定到标签或类似的东西以显示您的错误消息

答案 2 :(得分:0)

我认为你遇到的问题是你只能在可观察的范式中走一半而且会让你失望。尝试一直使用它,看看是否有帮助。例如:

protocol ServerAPI {
    func rx_fetchItems(for category: ItemCategory) -> Observable<[Item]>
}

struct ViewModel {

    let collectionItems: Observable<[Item]>
    let error: Observable<Error>

    init(controlValue: Observable<Int>, api: ServerAPI) {
        let serverItems = controlValue
            .map { ItemCategory(rawValue: $0) }
            .filter { $0 != nil }.map { $0! } // or use a `filterNil` operator if you already have one implemented.
            .flatMap { api.rx_fetchItems(for: $0)
                .materialize()
            }
            .filter { $0.isCompleted == false }
            .shareReplayLatestWhileConnected()

        collectionItems = serverItems.filter { $0.element != nil }.dematerialize()
        error = serverItems.filter { $0.error != nil }.map { $0.error! }
    }
}

编辑处理评论中提到的问题。您现在需要传入具有rx_fetchItems(for:)方法的对象。您应该有多个这样的对象:一个指向服务器,另一个指向任何服务器,而是返回固定数据,以便您可以测试任何可能的响应,包括错误。 (视图模型不应直接与服务器通信,但应通过中介进行...

上面的秘诀是materialize运算符,它将错误事件包装到包含错误对象的正常事件中。这样就可以阻止网络错误关闭整个系统。

针对您问题中的更改...您可以简单地使FirebaseAPI符合ServerAPI:

extension FirebaseAPI: ServerAPI {
    func rx_fetchItems(for category: ItemCategory) -> Observable<[Item]>  {
        // fetched items returns all items in database as Observable<[Item]>
        let fetchedItems = self.rx_fireRequest(.fetchItems, ofType: Item.self)
        switch category {
        case .Local:
            let localItems = fetchedItems
                .flatMapLatest { [weak self] (itemList) -> Observable<[Item]> in
                    return self!.rx_localItems(items: itemList)
            }

            return localItems

            // TODO: Handle other cases like RecentlyAdded, Trending, etc..
        }
    }

    // Helper method to filter items for only local items nearby user.
    private func rx_localItems(items: [Item]) -> Observable<[Item]> {
        return Observable.create { observable in
            observable.onNext(items.filter { $0.location == "LA" })
            observable.onCompleted()
            return Disposables.create()
        }
    }
}

此时您应该将ServerAPI的名称更改为FetchItemsAPI