forked from upstream-mirrors/quik
Load maxValue in KeyManager lazily
This commit is contained in:
@@ -26,18 +26,14 @@ import javax.inject.Singleton
|
|||||||
@Singleton
|
@Singleton
|
||||||
class KeyManagerImpl @Inject constructor() : KeyManager {
|
class KeyManagerImpl @Inject constructor() : KeyManager {
|
||||||
|
|
||||||
private var maxValue: Long = 0L
|
private var initialized = false
|
||||||
|
private var maxValue: Long = 0
|
||||||
init {
|
|
||||||
val realm = Realm.getDefaultInstance()
|
|
||||||
maxValue = realm.where(Message::class.java).max("id")?.toLong() ?: 0L
|
|
||||||
realm.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be called when a new sync is being started
|
* Should be called when a new sync is being started
|
||||||
*/
|
*/
|
||||||
override fun reset() {
|
override fun reset() {
|
||||||
|
initialized = true
|
||||||
maxValue = 0L
|
maxValue = 0L
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,6 +41,13 @@ class KeyManagerImpl @Inject constructor() : KeyManager {
|
|||||||
* Returns a valid ID that can be used to store a new message
|
* Returns a valid ID that can be used to store a new message
|
||||||
*/
|
*/
|
||||||
override fun newId(): Long {
|
override fun newId(): Long {
|
||||||
|
if (!initialized) {
|
||||||
|
maxValue = Realm.getDefaultInstance().use { realm ->
|
||||||
|
realm.where(Message::class.java).max("id")?.toLong() ?: 0L
|
||||||
|
}
|
||||||
|
initialized = true
|
||||||
|
}
|
||||||
|
|
||||||
maxValue++
|
maxValue++
|
||||||
return maxValue
|
return maxValue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user