代码之家  ›  专栏  ›  技术社区  ›  Bass

RxJava或RxKotlin中是否有生存期?

  •  0
  • Bass  · 技术社区  · 7 年前

    JetBrains/rd 图书馆有一个概念 Lifetime 它可以被认为是 AutoCloseable IDisposeable (见 here here 更多细节)。

    最简单的用例示例是这样的smth:

    import com.jetbrains.rider.util.lifetime.Lifetime
    import com.jetbrains.rider.util.lifetime.onTermination
    import java.io.FileInputStream
    import java.io.InputStream
    
    class LifetimeExample(lifetime: Lifetime, private val input: InputStream) {
        init {
            lifetime.onTermination {
                input.close()
                println("File closed.")
            }
        }
    
        fun process() {
            println("${input.bufferedReader().readText().length} char(s) read.")
        }
    
        companion object {
            @JvmStatic
            fun main(vararg args: String) {
                Lifetime.using {
                    LifetimeExample(it, FileInputStream("/etc/passwd")).process()
                }
            }
        }
    }
    

    在这两者中有没有类似的概念 RxJava RxKotlin ?

    0 回复  |  直到 7 年前