无法推断复杂闭包 return 类型;在 RxSwift 中添加显式类型以消除歧义

Unable to infer complex closure return type; add explicit type to disambiguate in RxSwift

我需要打多个电话。

1.删除文件上传

2。图片 1 和服务器 returns URL

3。上传图片 2 和服务器 returns URL

4.创建文档 API 包含 URL 和额外的
参数.

我尝试编写的代码在 RxSwift 和 MVVM 中。

  let resultOfDocumentUpdateWithDelete =
            donepressed
                .filter{ [=11=] }
                .withLatestFrom(self.existingDocumentIDChangedProperty)
                .flatMapLatest {id in
                    let deleted_document = apiClient.deleteDocument(id).asObservable().materialize()
                    let upload_frontImage = deleted_document
                        .withLatestFrom(self.frontImageNameChangedProperty)
                        .flatMapLatest {image in
                            apiClient.uploadImage(image: image!).asObservable().materialize()
                    }
                    let upload_backImage = upload_frontImage
                        .withLatestFrom(self.backImageChangedProperty)
                        .flatMapLatest {image in
                            apiClient.uploadImage(image: image!).asObservable().materialize()
                    }

                    let upload_document = upload_backImage
                        .withLatestFrom(self.parametersChangedProperty)
                        .flatMapLatest {parameters in
                            apiClient.uploadDocument(parameters: parameters)
                    }

                    return upload_document.materialize()
                }
                .share(replay: 1)

确保最后输入了两次服务器响应API,因此所有这些将按顺序调用。

如何在 RxSwift 中做。

这很有趣!这里的要点是,当您有疑问时,请继续创建您自己的操作员。如果后来发现您知道如何使用内置运算符完成这项工作,那么您可以替换您的运算符。自己制作的唯一问题是它们需要更多的测试。

请注意,要使用下面的内容,您必须结合最新的可观察对象,然后结合 flatMap 并将它们的值传递给此函数。

// all possible results from this job.
enum ProcessResult {
    case success
    case deleteFailure(Error)
    case imageFailue(Error)
    case backImageFailure(Error)
    case documentFailure(Error)
}

func uploadContent(apiClient: APIClient, documentID: Int, frontImage: UIImage, backImage: UIImage, parameters: Parameters) -> Single<ProcessResult> {
    // instead of trying to deal with all the materializes, I decided to turn it into a single process.
    return Single.create { observer in

        // each api call happens in turn. Note that there are no roll-back semantics included! You are dealing with a very poorly written server.
        let deleted = apiClient.deleteDocument(id: documentID)
            .asObservable()
            .share()

        let imagesUploaded = deleted
            .flatMap { _ in Observable.zip(apiClient.uploadImage(image: frontImage).asObservable(), apiClient.uploadImage(image: backImage).asObservable()) }
            .share()

        let documentUploaded = imagesUploaded
            .flatMap { arg -> Single<Void> in
                let (frontURL, backURL) = arg
                var updatedParams = parameters
                // add frontURL and backURL to parameters
                return apiClient.uploadDocument(parameters: updatedParams)
            }
            .share()

        let disposable = deleted
            .subscribe(onError: { observer(.success(ProcessResult.deleteFailure([=10=]))) })
        let disposable1 = imagesUploaded
            .subscribe(onError: { observer(.success(ProcessResult.imageFailue([=10=]))) })
        let disposable2 = documentUploaded
            .subscribe(
                onNext: { observer(.success(ProcessResult.success)) },
                onError: { observer(.success(ProcessResult.documentFailure([=10=]))) }
        )

        return Disposables.create([disposable, disposable1, disposable2])
    }
}