Bru*_*uno 1 reactive-programming ios swift rx-swift
我是Reactive Programming的新手,我有一个我不能独自解决的大问题...我需要按顺序上传几个视频资产,但我不知道该怎么做,我有很多PHAssets,我正在尝试遍历每个元素并通过网络发送它。到目前为止,这是我的代码并带有注释:
for item in items {
let fileName = item.media.localIdentifier
//Observable to generate local url to be used to save the compressed video
let compressedVideoOutputUrl = videoHelper.getDocumentsURL().appendingPathComponent(fileName)
//Observable to generate a thumbnail image for the video
let thumbnailObservable = videoHelper.getBase64Thumbnail(myItem: item)
//Observable to request the video from the iPhone library
let videoObservable = videoHelper.requestVideo(myItem: item)
//Compress the video and save it on the previously generated local url
.flatMap { videoHelper.compressVideo(inputURL: $0, outputURL: compressedVideoOutputUrl) }
//Generate the thumbnail and share the video to send over the network
let send = videoObservable.flatMap { _ in thumbnailObservable }
.flatMap { api.uploadSharedFiles(item, filename: fileName, base64String: $0) }
//subscribe the observable
send.subscribe(onNext: { data in
print("- Done chain sharing Video -")
},
onError: { error in
print("Error sharing Video-> \(error.localizedDescription)")
}).disposed(by: actionDisposeBag)
}
Run Code Online (Sandbox Code Playgroud)
如果要在flatMap中一一上传项目,请使用枚举
编辑:当您需要知道元素的索引时,枚举很有用,否则只需flatMap一个参数即可。
Observable.from(items)
.enumerated()
.flatMap() { index, item -> Observable<Item> in
return uploadItem(item)
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2151 次 |
| 最近记录: |