- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用后端提供的链接下载文件。要下载文件,使用返回 Progress() 对象的异步 API。问题是 FlatMap 无法从 Publisher<[Link], Error>
映射至 Publisher<[File], Error>
.我要解决的另一个问题是一旦 Progress.fractionCompleted 等于 1.0,就摆脱可取消并以某种方式转换 Progress 信息文件路径。
现在我尝试使用 map
功能。请参阅 Playground 中的代码:
import UIKit
import Combine
var progress = Progress()
extension ProgressUserInfoKey {
public static var destinationURL = ProgressUserInfoKey("destinationURL")
}
func download(from urlRequest: URLRequest, to destinationURL: URL) -> AnyPublisher<Progress, Error> {
return Future<Progress, Error> { promise in
progress = Progress(totalUnitCount: 1)
progress.setUserInfoObject(destinationURL.absoluteString,
forKey: ProgressUserInfoKey.destinationURL)
promise(.success(progress))
// Simulate async API
DispatchQueue.main.async {
progress.completedUnitCount = 1
}
}.eraseToAnyPublisher()
}
struct Link: Decodable {
let url: String
}
func getLinks() -> AnyPublisher<[Link], Error> {
return URLSession.shared.dataTaskPublisher(for: URL(string: "https://backend.com")!)
.map { $0.data }
.decode(type: [Link].self, decoder: JSONDecoder())
.eraseToAnyPublisher()
}
struct File {
let url: URL
let size: UInt32
}
private func destinationUrl(_ fromUrl: String?) -> URL {
guard let path = fromUrl else {
return URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
}
return URL(fileURLWithPath: path)
}
/// 2) How to get rid of this state and transoft Progress into filePath directly (using matp(transform: ? )
var cancellableSet = Set<AnyCancellable>()
func getFiles() -> AnyPublisher<[File], Error> {
getLinks()
.flatMap { (links) -> AnyPublisher<[File], Error> in
let sequence = Sequence<[AnyPublisher<File, Error>], Error>(sequence: links.map {
download(from: URLRequest(url: $0), to: destinationUrl(UUID().uuidString))
.sink { progress in
progress.publisher(for: \.fractionCompleted).sink { progressValue in
if progressValue == 1.0 {
let filePath: String = progress.userInfo[ProgressUserInfoKey.destinationURL] as? String ?? ""
/// 1) How to return publisher here
///return Publisher(File(url: URL(string: filePath)!, size: 0))
}
}
.store(in: &cancellableSet)
}
.store(in: &cancellableSet)
} )
return sequence.flatMap { $0 }.collect().eraseToAnyPublisher()
}
}
我期待代码编译成功并且函数 getFiles
返回 AnyPublisher<[File], Error>
.
目前得到的错误代码如下:
Cannot convert return expression of type 'Publishers.FlatMap<AnyPublisher<[File], Error>, AnyPublisher<[Link], Error>>' to return type 'AnyPublisher<[File], Error>'
最佳答案
问题已通过使用 Publishers.Sequence() 扩展并避免调用函数 sink() 得到解决。我还使用 flatMap() 来转换输出值。可以在此处找到以这种方式演示的代码:Combine publishers together
关于 swift 结合。如何转变出版商值(value)观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58251867/
在尝试为 ContT monad 转换器建立一些直觉时,我(也许并不奇怪)发现自己很困惑。问题在于 shiftT 操作,它似乎没有做任何有用的事情。 首先是一个简单的例子,说明如何使用它 shiftT
我有 Item 1 Item 2 我想要切换 var elements = document.getElementsByName('R
执行此操作的最快方法是什么?左边括号中的变量返回 bool 值,它们表示窗口大小范围。 (例如,o1281 为 1281 及以上屏幕返回 true,o1025 为 1025 及以上屏幕返回 true,
我对编程还很陌生,但我的任务是维护一些由前员工创建的应用程序。我有一个 ?: 声明现在需要处理的不仅仅是真或假声明,但我不确定如何去做。有问题的代码是: MailDomainContext m
为什么 GMT-0400 在 1989 年之前转变为 GMT-0500? > new Date('1989-04-02T23:01:14.52Z') Sun Apr 02 1989 19:01:14
我们有一堆代理对(或 2 字节 utf8?)字符,例如 ��,这是作为 2 个字符存储为 UTF8 的祈祷 watch 情符号。在浏览器中呈现时,此字符串呈现为两个 ?? 例子:�� 我需要使用 ph
我知道我可以将字符串格式的 unixTime 转换为本地时间的毫秒val currentTimeMillis = serverTimeDateFormat.parse(iso8601).time 我想
我是一名优秀的程序员,十分优秀!