Advertisement
lukibeni

persist

Dec 6th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.70 KB | None | 0 0
  1. val dbVersion = dbVersionRepository.findOne(realm, AppConfig.DB_VERSION_KEY)
  2.         if(dbVersion == null) {
  3.             val result = dbVersionRepository.saveOrUpdate(realm, DbVersion(bo.phoneBookVersion))
  4.             Log.d("TAG", result.toString())
  5.         } else {
  6.             dbVersion.apply {
  7.                 version = bo.phoneBookVersion
  8.                 dbVersionRepository.saveOrUpdate(realm, this)
  9.             }
  10.         }
  11.         if(bo.entryList.isNotEmpty()) {
  12.             when (bo.loadType) {
  13.                 ResponseType.FULL -> {
  14.                     repository.deleteAll(realm)
  15.                     bo.entryList.forEach { realmList.add(entryMapper.toRealmObject(realm, it)) }
  16.                 }
  17.                 ResponseType.PARTIAL -> {
  18.                     bo.entryList.filter { entryBO -> entryBO.action == EntryAction.ADD }
  19.                         .forEach { realmList.add(entryMapper.toRealmObject(realm, it)) }
  20.                     bo.entryList.filter { entryBO -> entryBO.action == EntryAction.MOD }
  21.                         .forEach {
  22.                             val entryToModify = repository.findOne(realm, it.phoneNumber)
  23.                             entryToModify?.address = it.address
  24.                             entryToModify?.division = it.division
  25.                             entryToModify?.emailaddress = it.emailAddress
  26.                             entryToModify?.fullname = it.fullname
  27.                             entryToModify?.picture = it.picture
  28.                         }
  29.                     bo.entryList.filter { entryBO -> entryBO.action == EntryAction.DEL }
  30.                         .forEach { repository.delete(realm, it.phoneNumber) }
  31.                 }
  32.             }
  33.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement