简体   繁体   English

GlobalScope 用于在 Android 中保存文件

[英]GlobalScope for saving files in Android

I have scenarios where I need to save some objects to disk.我有需要将一些对象保存到磁盘的场景。 So far I've been doing this with Java's ExecutorService.到目前为止,我一直在使用 Java 的 ExecutorService 来做这件事。 But I'm now refactoring parts of my code to Kotlin, and was wondering whether I'm handling this right.但是我现在正在将我的部分代码重构为 Kotlin,并且想知道我是否正确处理了这个问题。

So in Java I have a code which goes to my Kotlin functions which are:因此,在 Java 中,我有一个代码用于我的 Kotlin 函数,这些函数是:

// Java code calls this function in KotlinUtils.kt:
fun saveFileInBackground(saveObj: Any, fileName: String) {
    GlobalScope.launch {
        saveFile(...)
    }
}

private suspend fun saveFile(data: ByteArray, ...) {
    withContext(Dispatchers.IO) {
        var file = File(....)
        if (!file.exists()) file.mkdirs()

        file.writeBytes(data)
    }
}

Now I've read this post: https://medium.com/@elizarov/the-reason-to-avoid-globalscope-835337445abc现在我读了这篇文章: https://medium.com/@elizarov/the-reason-to-avoid-globalscope-835337445abc

And I still don't see a reason to avoid GlobalScope in this particular case.在这种特殊情况下,我仍然没有理由避免使用 GlobalScope。 These file writes are something that I need on my app level, and they are not something that could be cancelled anytime after I dispatch those calls.这些文件写入是我在我的应用程序级别上需要的东西,它们不是在我发送这些调用后可以随时取消的东西。

Another option I had in mind it to have the KotlinUtils class implement CoroutineScope and then just call launch instead of GlobalScope.launch .我想到的另一个选择是让 KotlinUtils class 实现 CoroutineScope 然后只调用launch而不是GlobalScope.launch On second thought, that's a bit problematic, because it seems that the KotlinUtil would have to be abstract, which it cannot be (it's a @Singleton class using dagger2)再想一想,这有点问题,因为 KotlinUtil 似乎必须是抽象的,但它不可能是抽象的(它是使用 dagger2 的@Singleton class)

However, I'm fairly new to Kotlin and Coroutines, so perhaps I misunderstood something.但是,我对 Kotlin 和协程还很陌生,所以也许我误解了一些东西。 Any advice would be much appreciated.任何建议将不胜感激。

GlobalScope is a singleton and not associated with any Job. GlobalScope 是一个 singleton,与任何作业无关。 This launches top-level coroutines and highly discouraged to use because if you launch coroutines in the global scope, you lose all the benefits you get from structured concurrency这会启动顶级协程并且非常不鼓励使用,因为如果您在全局 scope 中启动协程,您将失去从结构化并发中获得的所有好处

So if you know this and your task is completely independent of everything else in your application.因此,如果您知道这一点并且您的任务完全独立于应用程序中的其他所有内容。 Hereby independent, I mean, you do not want to cancel currently running tasks even if something goes wrong in other parts of your application.特此独立,我的意思是,即使应用程序的其他部分出现问题,您也不希望取消当前正在运行的任务。 In this case, in my opinion, it is fine to use GlobalScope在这种情况下,我认为可以使用GlobalScope

For ex, if you call saveFileInBackground task in a loop and even though it fails in one of the iterations, you do not want to terminate the loop and want to continue with the rest of your iterations.例如,如果您在循环中调用saveFileInBackground任务,即使它在其中一次迭代中失败,您也不想终止循环并希望继续执行迭代的 rest。

Another thing to keep in mind while using GlobalScope , if you mistakenly write wrong code in GlobalScope , it will stay there and will not get canceled by other parts of application which means your application will never terminate使用GlobalScope时要记住的另一件事是,如果您在GlobalScope中错误地编写了错误的代码,它将保留在那里并且不会被应用程序的其他部分取消,这意味着您的应用程序将永远不会终止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM