Skip to content

Commit

Permalink
Load maxValue in KeyManager lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
moezbhatti committed Jun 9, 2018
1 parent 010fcdc commit d4093cc
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions data/src/main/java/manager/KeyManagerImpl.kt
Expand Up @@ -26,25 +26,28 @@ import javax.inject.Singleton
@Singleton
class KeyManagerImpl @Inject constructor() : KeyManager {

private var maxValue: Long = 0L

init {
val realm = Realm.getDefaultInstance()
maxValue = realm.where(Message::class.java).max("id")?.toLong() ?: 0L
realm.close()
}
private var initialized = false
private var maxValue: Long = 0

/**
* Should be called when a new sync is being started
*/
override fun reset() {
initialized = true
maxValue = 0L
}

/**
* Returns a valid ID that can be used to store a new message
*/
override fun newId(): Long {
if (!initialized) {
maxValue = Realm.getDefaultInstance().use { realm ->
realm.where(Message::class.java).max("id")?.toLong() ?: 0L
}
initialized = true
}

maxValue++
return maxValue
}
Expand Down

0 comments on commit d4093cc

Please sign in to comment.