Commit d8200ebf by huangzhicong

修复storage接口回调没有执行在主线程的问题

parent ccd0ad5f
...@@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers ...@@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragment, proxy){ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragment, proxy){
companion object { companion object {
...@@ -32,12 +33,16 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm ...@@ -32,12 +33,16 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
val storageKey = stringPreferencesKey(key) val storageKey = stringPreferencesKey(key)
maxrocky[storageKey] = value maxrocky[storageKey] = value
val data = StorageResult(key, value) val data = StorageResult(key, value)
proxy.success(STORAGE_METHOD, storageResult, data) withContext(Dispatchers.Main) {
storageResult.reset() proxy.success(STORAGE_METHOD, storageResult, data)
storageResult.reset()
}
} }
} catch (e: Exception) { } catch (e: Exception) {
proxy.failed(STORAGE_METHOD, storageResult, e.message ?: "") withContext(Dispatchers.Main) {
storageResult.reset() proxy.failed(STORAGE_METHOD, storageResult, e.message ?: "")
storageResult.reset()
}
} }
} }
} }
...@@ -48,12 +53,16 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm ...@@ -48,12 +53,16 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
try { try {
context.dataStore.data.map { it[stringPreferencesKey(key)] ?: "" }.collect { storageValue -> context.dataStore.data.map { it[stringPreferencesKey(key)] ?: "" }.collect { storageValue ->
val data = StorageResult(key, storageValue) val data = StorageResult(key, storageValue)
proxy.success(ACQUIRE_METHOD, acquireResult, data) withContext(Dispatchers.Main) {
acquireResult.reset() proxy.success(ACQUIRE_METHOD, acquireResult, data)
acquireResult.reset()
}
} }
} catch (e: Exception) { } catch (e: Exception) {
proxy.failed(ACQUIRE_METHOD, acquireResult, e.message ?: "") withContext(Dispatchers.Main) {
acquireResult.reset() proxy.failed(ACQUIRE_METHOD, acquireResult, e.message ?: "")
acquireResult.reset()
}
} }
} }
} }
...@@ -69,12 +78,16 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm ...@@ -69,12 +78,16 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
} }
val storageValue = maxrocky.remove(storageKey) val storageValue = maxrocky.remove(storageKey)
val data = StorageResult(key, storageValue) val data = StorageResult(key, storageValue)
proxy.success(CLEAR_METHOD, clearResult, data) withContext(Dispatchers.Main) {
clearResult.reset() proxy.success(CLEAR_METHOD, clearResult, data)
clearResult.reset()
}
} }
} catch (e: Exception) { } catch (e: Exception) {
proxy.failed(CLEAR_METHOD, clearResult, e.message ?: "") withContext(Dispatchers.Main) {
clearResult.reset() proxy.failed(CLEAR_METHOD, clearResult, e.message ?: "")
clearResult.reset()
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment