- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在上传带有改造的文件。想在Kotlin中听听retrofit进度,在ui上用进度条显示。当我对此进行研究时,我在此页面上使用 PublishSubject 在 java 中找到了一个很好的例子。 Is it possible to show progress bar when upload image via Retrofit 2?我想使它适应我自己的代码,但我无法决定在 Kotlin 中我应该使用 Flow 还是 Channel 而不是 PublishSubject。
我的带有 broadcastChannel 的 requestbody 类:
class ProgressRequestBody : RequestBody {
val mFile: File
val ignoreFirstNumberOfWriteToCalls : Int
constructor(mFile: File) : super(){
this.mFile = mFile
ignoreFirstNumberOfWriteToCalls = 0
}
constructor(mFile: File, ignoreFirstNumberOfWriteToCalls : Int) : super(){
this.mFile = mFile
this.ignoreFirstNumberOfWriteToCalls = ignoreFirstNumberOfWriteToCalls
}
var numWriteToCalls = 0
val getProgress = BroadcastChannel<Float>(1)
override fun contentType(): MediaType? {
return "image/*".toMediaTypeOrNull()
}
@Throws(IOException::class)
override fun contentLength(): Long {
return mFile.length()
}
@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
numWriteToCalls++
val fileLength = mFile.length()
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
val `in` = FileInputStream(mFile)
var uploaded: Long = 0
try {
var read: Int
var lastProgressPercentUpdate = 0.0f
read = `in`.read(buffer)
while (read != -1) {
uploaded += read.toLong()
sink.write(buffer, 0, read)
read = `in`.read(buffer)
// when using HttpLoggingInterceptor it calls writeTo and passes data into a local buffer just for logging purposes.
// the second call to write to is the progress we actually want to track
if (numWriteToCalls > ignoreFirstNumberOfWriteToCalls ) {
val progress = (uploaded.toFloat() / fileLength.toFloat()) * 100f
//prevent publishing too many updates, which slows upload, by checking if the upload has progressed by at least 1 percent
if (progress - lastProgressPercentUpdate > 1 || progress == 100f) {
// publish progress
getProgress.send(progress)
lastProgressPercentUpdate = progress
}
}
}
} finally {
`in`.close()
}
}
companion object {
private val DEFAULT_BUFFER_SIZE = 2048
}
}
问题是 broadcastChannel.send(progress)
希望函数暂停,但 RequestBody writeTo
方法不应该暂停。在这种情况下,我很困惑。我应该使用 Flow 还是 BroadCastChannel?你能帮帮我吗?
最佳答案
你应该使用 MutableStateFlow:
val getProgress = MutableStateFlow<Float>(0f) // Initial value is 0
然后:
getProgress.value = progress
关于kotlin - PublishSubject 与 Kotlin 协程(流/ channel ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66736497/
我正在处理这样的情况,我想在这里应用 RxSwift。 我有带有按钮的.xib UIView。 class RightButtonItemView: UIView { @IBOutlet we
我在 Playground 上玩 RxSwift 时遇到了警告。这是完整的警告消息: Synchronization anomaly was detected. - Debugging: To deb
为什么subscribe在这里从不打印任何内容?只是出于好奇。无论如何,这是一个坏习惯,我会改用observeOn,但是我无法弄清楚为什么从来没有达到subscribe ... fun main()
我正在尝试使用 RxJava 创建模型的代理,该代理允许其他人订阅模型中的更改。 这是我的代理的简化版本: class MyModelProxy { private static MyMode
我尝试使用 rx 和 PublishSubject .所以我创建对象: PublishSubject events = PublishSubject.create(); 在我的服务中,我输入了 MyE
PublishSubject 非常适合在观察者和可观察对象之间架起一座桥梁: Observable observable = Observable.just("string"); Pu
我知道,所有类型的 Rx 主题都可以获得订阅中未正确排序的元素,例如。如果我按 1、2、3 的顺序发送三个元素,则可以选择按以下顺序获取它:1、3、2。 我想知道,有没有办法强制发射元素在开始和结束时
我正在努力处理包含 RxSwift 的 PublishSubject 的特定用例。 为了简单起见,省略了不重要的细节。 有一个 MVVM 设置。在 VC 中,我有一个 UIButton,点击它应该发送
我有一个 View Controller ,它打开一个模态视图 Controller 供用户从其库中选择图像。为此,我使用了我编写的 DKImagePickerController 的 Rx 包装器。
我正在努力了解以下黄金法则(如果有的话): When to use BehaviorSubject ? 和 When to use PublishSubject ? 它们之间的区别很明显 There
谁能解释一下为什么 PublishSubject 不能很好地与 firstOrError() 一起工作? 我希望 firstOrError 在创建没有任何值的 PublishSubject 时返回 N
我正在尝试使用 PublishSubject 来转发按钮点击。但是,此 PublishSubject 会在初始化时触发,这会干扰我的逻辑。这就是我正在做的: var buttonClick = Pub
我将 Button pressed 绑定(bind)到路由器中的 PublishSubject,如下所示: hambugerButton .rx_tap .bindTo(router.op
我有一个已注册 doOnSubscribe 和 doOnUnsubscribe 操作的 PublishSubject。如果完成订阅,则不会调用这两个操作。 private PublishSubject
我在学习Viper带 RxSwift . 我想通知我的Presenter那viewDidLoad在我的 ViewController 中被调用. 为此,我有以下内容: class LoginPrese
我正在尝试测试我的 ViewModel 的主要功能。重要的步骤是测试加载状态完成。但可以肯定的是,为了更好的测试,测试所有状态可能会很有趣。 我正在阅读很多关于 RxTest 和 RxBlocking
我需要一种功能,允许将消息异步推送到我的 PublishSubject 并通过 ConnectableObservable 以一定的速度(实际上是一个接一个)处理它们。不幸的是,在底层的Subscri
RxJava 查询 : 你好, 我有一个 PublishSubject subject = PublishSubject.create() ; 我正在订阅上述主题并在此之后进行 API 调用: sub
我在我的项目中使用 ReactiveSwift,我想知道 PublishSubject 的等价物是什么? 例如在 RXSwift 中我们可以这样做: let disposeBag = DisposeB
我正在创建基于 CocoaAsyncSocket 和 STOMP 协议(protocol)的简单消息传递应用程序。所以我创建了主类,它使用 PublishSubject ,因此订阅者可以观察传入的 S
我是一名优秀的程序员,十分优秀!