0

我正在按照本教程https://developer.android.com/guide/topics/search/appsearch#groovy
使用 AppSearch 制作示例应用程序。但是,当我得到架构部分时,我对“期货”有未知的引用。我试图寻找与 Android 的 Futures concurrent 相关的任何依赖项,我发现了 3 个依赖项,但它并没有解决对“Futures”的未知引用。我也不知道“mExecutor”应该是什么,因为由于未知的引用,我看不到“Futures”类及其功能。

这是我的依赖项:

def appsearch_version = "1.0.0-alpha03"
    implementation "androidx.concurrent:concurrent-listenablefuture-callback:1.0.0-beta01"
    implementation "androidx.concurrent:concurrent-listenablefuture:1.0.0-beta01"
    implementation "androidx.concurrent:concurrent-futures-ktx:1.1.0"
    implementation "androidx.appsearch:appsearch:$appsearch_version"
    // Use kapt instead of annotationProcessor if writing Kotlin classes
    annotationProcessor "androidx.appsearch:appsearch-compiler:$appsearch_version"

    implementation "androidx.appsearch:appsearch-local-storage:$appsearch_version"
    // PlatformStorage is compatible with Android 12+ devices, and offers additional features
    // to LocalStorage.
    implementation "androidx.appsearch:appsearch-platform-storage:$appsearch_version"

这是我的代码(与开发人员指南中的完全相同):

    val sessionFuture = LocalStorage.createSearchSession(
                LocalStorage.SearchContext.Builder(this, /*databaseName=*/"notes_app")
                    .build()
            )
            val setSchemaRequest = SetSchemaRequest.Builder().addDocumentClasses(Note::class.java)
                .build()
            val setSchemaFuture = Futures.transformAsync(//Futures unknown reference
                sessionFuture,
                { session ->
                    session?.setSchema(setSchemaRequest)
                }, mExecutor//?
            )

val putRequest = PutDocumentsRequest.Builder().addDocuments(note).build()
            val putFuture = Futures.transformAsync(
                sessionFuture,
                { session ->
                    session?.put(putRequest)
                }, mExecutor
            )
            Futures.addCallback(//Futures unknown reference
                putFuture,
                object : FutureCallback<AppSearchBatchResult<String, Void>?{//FutureCallback unknown reference
                    override fun onSuccess(result: AppSearchBatchResult<String, Void>?) {

                        // Gets map of successful results from Id to Void
                        val successfulResults = result?.successes

                        // Gets map of failed results from Id to AppSearchResult
                        val failedResults = result?.failures
                    }

                    override fun onFailure(t: Throwable) {
                        Log.e(TAG, "Failed to put documents.", t)
                    }
                },
                mExecutor//?
            )

如果您对我需要实现的导入有任何想法,或者有任何方法可以使用 kotlin 协程实现相同的目标?

4

0 回答 0