首页 > 解决方案 > 无法根据检查挂钩到另一个 RxSwift 流

问题描述

根据在网络请求之前添加的检查,无法连接到另一个 RxSwift 流。

我正在构建一个 UserProfile 对象,但我想确保 userProfile 对象已完全创建。我有一个正在创建 userProfile 对象的流,但现在我有一个检查。我想挂钩到前一个流,以便在完整的 UserProfile 上我可以执行网络请求。我不确定如何连接到上一个流。updateUserProfile($0) 是我传递 UserProfile 对象以发出网络请求的地方,但我想确保我停在那里。

let userInfoResponse = latestLocallySavedUserProfileDictionary
            .map { (dictionary: [String: String?]) in
               return UserProfile(
                    firstName: dictionary["firstName"] ?? "",
                    lastName: dictionary["lastName"] ?? "",
                    phone: dictionary["phone"] ?? "",
                    company: dictionary["company"] ?? "",
                    organizationType: dictionary["organizationType"] ?? "",
                    jobFunction: dictionary["jobFunction"] ?? "",
                    addressLine1: dictionary["addressLine1"] ?? "",
                    addressLine2: dictionary["addressLine2"] ?? "",
                    city: dictionary["city"] ?? "",
                    state: dictionary["state"] ?? nil,
                    zipCode: dictionary["zipCode"] ?? "",
                    country: dictionary["country"] ??  "",
                    languageCode: dictionary["languageCode"] ?? "",
                    merrillDisclaimerAccepted: false, // ignore for now
                    merrillDisclaimerAcceptedDate: "" // ignore for now
                )
            }

            .flatMapLatest { updateUserProfile($0) }
            .share()

        isUserProfileDictionaryComplete = Observable.merge([
            initialUserProfileCall,
            userInfoResponse
                .map{$0.createDictionary()},
            userProfileLocal
            ])
                .asObservable()
                .map{(profileDictionary) -> Bool in
                    return profileDictionary.values.contains(nil) || profileDictionary.values.contains("")
                }
                .ignore(true)

标签: swiftiorx-swift

解决方案


我不太明白您要做什么,但总的来说,您需要找出所有应该提示网络请求的事件,并将它们放在 flatMapLatest 之前的流中。

UserProfile如果您尝试从latestLocallySavedUserProfileDictionary,构建 a initialUserProfileCalluserProfileLocal那么您可能希望在合并后进行扫描以收集每个字典中的所有位。


推荐阅读