我是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)
}
答案 0 :(得分:4)
如果要在flatMap中逐个上传项目,请使用枚举
编辑:枚举在您需要知道元素的索引时很有用,否则只有flatMap
一个参数就可以了。
Observable.from(items)
.enumerated()
.flatMap() { index, item -> Observable<Item> in
return uploadItem(item)
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
答案 1 :(得分:2)
使用集合元素和 .flatMap()对现有代码进行观察 -
const argv = require('minimist')(process.argv.slice(2));
const csv = argv.file;