使用 RxSwift 循环
Loop with RxSwift
我是响应式编程的新手,我有一个大问题,我无法独自解决...我需要按顺序上传几个视频资产,但我不知道该怎么做,我有一个 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: [=10=], 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: [=10=]) }
//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)
}
从集合元素和 .flatMap() 中观察到它们到您现有的代码 -
Observable
.from(items)
.flatMap { (item) -> Any in
// your code
return send
}
.subscribe( /* your code */ )
.disposed(by: actionDisposeBag)
如果你想在flatMap中一个一个地上传你的项目,那么使用枚举
编辑:当您需要知道元素的索引时, 枚举很有用,否则只需 flatMap
和一个参数即可。
Observable.from(items)
.enumerated()
.flatMap() { index, item -> Observable<Item> in
return uploadItem(item)
}
.subscribe(onNext: { print([=10=]) })
.disposed(by: disposeBag)
我是响应式编程的新手,我有一个大问题,我无法独自解决...我需要按顺序上传几个视频资产,但我不知道该怎么做,我有一个 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: [=10=], 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: [=10=]) }
//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)
}
从集合元素和 .flatMap() 中观察到它们到您现有的代码 -
Observable
.from(items)
.flatMap { (item) -> Any in
// your code
return send
}
.subscribe( /* your code */ )
.disposed(by: actionDisposeBag)
如果你想在flatMap中一个一个地上传你的项目,那么使用枚举
编辑:当您需要知道元素的索引时, 枚举很有用,否则只需 flatMap
和一个参数即可。
Observable.from(items)
.enumerated()
.flatMap() { index, item -> Observable<Item> in
return uploadItem(item)
}
.subscribe(onNext: { print([=10=]) })
.disposed(by: disposeBag)