iphone - 使用 RxSwift 返回 DispatchGroup 之后的对象

标签 iphone swift grand-central-dispatch rx-swift dispatch-queue

我正在使用 DispatchGroup 从 3 个不同的 API 下载数据,完成后我想从我的函数返回新创建的合并对象。现在,虽然 DispatchGroup 工作正常并且我正在获取数据,但我无法将其返回到调用函数。以下是我的功能:

func getHowToInfo(materialNo: String) -> Observable<HowToInfo> {
    return Observable.create{ observer in
        let dispatchGroup = DispatchGroup()

        _ = self.getMaterialInfo(materialNo: materialNo).subscribe(onNext:{ material in

            let howto = HowToInfo(videos: [], documents: [], applications: [])

            if (material.documentTargetId?.count)! > 0 {
                dispatchGroup.enter()
                _ = self.materialRepo?.API1(targetIDs: material.documentTargetId!).subscribe(onNext:{documents in
                    howto.documents = documents
                    dispatchGroup.leave()
                }, onError: { (error) in
                    dispatchGroup.leave()
                })
            }
            if (material.applicationDescription?.count)! > 0 {
                dispatchGroup.enter()
                _ = self.materialRepo?.API2(materialNo: materialNo).subscribe(onNext:{applications in
                    howto.applications = applications
                    dispatchGroup.leave()
                }, onError: { (error) in
                    dispatchGroup.leave()
                })
            }
            if ((material.videoApplicationTargetId?.count) != nil && (material.videoApplicationTargetId?.count)! > 0) {
                dispatchGroup.enter()
                _ = self.materialRepo?.API3(targetIDs: material.videoApplicationTargetId!).subscribe(onNext:{videos in
                    howto.videos = videos
                    dispatchGroup.leave()
                }, onError: { (error) in
                    dispatchGroup.leave()
                })
            }else if ((material.videoSupportTargetId?.count) != nil && (material.videoSupportTargetId?.count)! > 0) {
                dispatchGroup.enter()
                _ = self.materialRepo?.API4(targetIDs: material.videoSupportTargetId!).subscribe(onNext:{videos in
                    howto.videos = videos
                    dispatchGroup.leave()
                }, onError: { (error) in
                    dispatchGroup.leave()
                })
            }

            dispatchGroup.notify(queue: .main, execute: {
                print("All functions complete 👍")
                observer.onNext(howto)
                observer.onCompleted()
            })
        })
        return Disposables.create()
    }
}

调用函数:

func loadHowToUseList(materialNo: String){
    self.serviceMaterial.getHowToInfo(materialNo: materialNo).subscribe({
        howToUse in
        print(howToUse)
    }).disposed(by: DisposeBag())
}

我无法在上面的订阅方法中获取我的对象,它永远不会运行。

最佳答案

尝试添加

dispatchGroup.wait()

在你的台词之后

        dispatchGroup.notify(queue: .main, execute: {
            print("All functions complete 👍")
            observer.onNext(howto)
            observer.onCompleted()
        })

而且,为什么不直接使用 Rx 运算符本身呢?

其中每一个都可以是一个observer.onNext,然后你尝试观察这个可观察对象的三个事件,并且不需要onCompleted

关于iphone - 使用 RxSwift 返回 DispatchGroup 之后的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50199020/

相关文章:

html - 自动缩放 iOS 7 中的方向更改

ios swift - 将uiimage存储在文档目录和核心数据中的路径

objective-c - dispatch_semaphore_t 重用-我在这里缺少什么?

iphone - 最好使用全局或自定义命名的 GCD 队列?

iphone - iPhone 3G 上的坐标全错了?这可能是你的编译器

iphone - 如何制作可在 iOS 5 至 7 上运行的 armv7 arm64 fat 二进制文件?

iphone - iPhone 中的 Mach 异常

iphone - 为什么 SWIFT 打印“可选(...)

ios - 显示您关注的用户的帖子 - swift 和 firebase

iphone - NSMutableURLRequest 和 NSURLConnection 无法在 GCDdispatch_async 中工作?