0

我是 Koin 的新手,我正在创建一个对象实例并给它一个 ID 为 SESSION 的范围,所以在我关闭它之后。我希望在下次访问它时为其创建一个新实例。

val InkModule = module {
   scope("SESSION") { providesInkDevice(get(), get()) }
    }

fun providesInkDevice(application: Application, preferences: PreferenceUtility): InkDevice {
    return InkDeviceFactory.createClient(application, Gson().fromJson(preferences.deviceInfo, InkDeviceInfo::class.java)) }

所以现在当我注入一个实例时,我总是创建一个范围

  private val inkDevice: InkDevice by inject()//Lazy injection 

 try {
            scope = getKoin().getScope("SESSION")
        } catch (e: NoScopeFoundException) {
            scope = getKoin().createScope("SESSION")
            Timber.e(e, "Cannot create new scope")
        } 

        inkDevice.connect()//here a new instance is created when we access it first time

现在,一段时间后我想重新创建我的 Object(inkDevice) 的新实例,所以我决定做 scope.close

scope = getKoin().getScope("SESSION")
            inkDevice.dispose()
            preferenceUtility.deviceInfo = ""

            scope = getKoin().createScope("SESSION")

但是在我关闭会话后,下次我访问它时它无法创建新实例。

4

0 回答 0