如何在 android 中关闭或释放 AWSAppSyncClient

How to close or release AWSAppSyncClient in android

我正在使用 AWSAppSyncClient 服务,这是我在应用程序中使用的单一对象

client = AWSAppSyncClient.builder()
        .context(context)
        .awsConfiguration(awsConfiguration)
        .credentialsProvider(
            CognitoCachingCredentialsProvider(
                context,
                ...
            )
        ).build()

而且我在使用 StrictMode 时发现内存泄漏,这是已记录问题的完整堆栈跟踪:

E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
    at dalvik.system.CloseGuard.open(CloseGuard.java:180)
    at android.database.CursorWindow.<init>(CursorWindow.java:111)
    at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:138)
    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)
    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:219)
    at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:258)
    at com.amazonaws.mobileconnectors.appsync.AppSyncMutationSqlCacheOperations.fetchAllRecords(AppSyncMutationSqlCacheOperations.java:102)
    at com.amazonaws.mobileconnectors.appsync.PersistentOfflineMutationManager.fetchPersistentMutationsList(PersistentOfflineMutationManager.java:74)
    at com.amazonaws.mobileconnectors.appsync.PersistentOfflineMutationManager.<init>(PersistentOfflineMutationManager.java:42)
    at com.amazonaws.mobileconnectors.appsync.AppSyncOfflineMutationManager.<init>(AppSyncOfflineMutationManager.java:181)
    at com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient.<init>(AWSAppSyncClient.java:169)
    at com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient.<init>(AWSAppSyncClient.java:72)
    at com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient$Builder.build(AWSAppSyncClient.java:478)
    at com.eleven11.app.elevenlife.client.aws.AppSyncClient.<init>(AppSyncClient.kt:25)
    at com.eleven11.app.elevenlife.client.dependency.ClientModuleKt$clientModule.invoke(ClientModule.kt:37)
    at com.eleven11.app.elevenlife.client.dependency.ClientModuleKt$clientModule.invoke(ClientModule.kt)
    at org.koin.core.instance.holder.InstanceHolder$DefaultImpls.create(InstanceHolder.kt:17)
    at org.koin.core.instance.holder.SingleInstanceHolder.create(SingleInstanceHolder.kt:10)
    at org.koin.core.instance.holder.SingleInstanceHolder.get(SingleInstanceHolder.kt:19)
    at org.koin.core.instance.InstanceFactory.retrieveInstance(InstanceFactory.kt:53)
    at org.koin.core.instance.InstanceRegistry$resolveInstance.invoke(InstanceRegistry.kt:138)
    at org.koin.core.instance.InstanceRegistry$resolveInstance.invoke(InstanceRegistry.kt:39)
    at org.koin.core.stack.ResolutionStack.resolve(ResolutionStack.kt:44)
    at org.koin.core.instance.InstanceRegistry.resolveInstance(InstanceRegistry.kt:137)
    at org.koin.core.instance.InstanceRegistry.access$resolveInstance(InstanceRegistry.kt:39)
    at org.koin.core.instance.InstanceRegistry$proceedResolution$$inlined$synchronized$lambda.invoke(InstanceRegistry.kt:98)
    at org.koin.core.instance.InstanceRegistry$proceedResolution$$inlined$synchronized$lambda.invoke(InstanceRegistry.kt:39)
    at org.koin.core.time.DurationKt.logDuration(Duration.kt:11)
    at org.koin.core.instance.InstanceRegistry$proceedResolution$$inlined$synchronized$lambda.invoke(InstanceRegistry.kt:97)
    at org.koin.core.instance.InstanceRegistry$proceedResolution$$inlined$synchronized$lambda.invoke(InstanceRegistry.kt:39)
    at org.koin.core.time.DurationKt.measureDuration(Duration.kt:19)
    at org.koin.core.instance.InstanceRegistry.proceedResolution(InstanceRegistry.kt:87)
    at org.koin.core.instance.InstanceRegistry.resolve(InstanceRegistry.kt:61)
    at com.eleven11.app.elevenlife.ui.search.SearchFragment.initViewModel(SearchFragment.kt:408)
    at com.eleven11.app.elevenlife.ui.search.SearchFragment.onCreate(SearchFragment.kt:66)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2414)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
    at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)

有关关闭或释放 AWSAppSyncClient 的任何帮助

谢谢!

我必须使用最新更新的appsync版本,我使用的是2.6.+版本,所以我现在使用的是versoin 2.9.0,内存泄漏消失了。