修改realm事物存储逻辑,解决OOM问题

This commit is contained in:
qiji4215
2023-09-11 15:59:56 +08:00
parent dbe81f5bde
commit 30822c7937
23 changed files with 443 additions and 210 deletions

View File

@@ -1,5 +1,7 @@
package com.navinfo.omqs.ui.activity.login
import android.app.Activity
import android.app.ActivityManager
import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
@@ -9,12 +11,15 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.viewModelScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.navinfo.omqs.R
import com.navinfo.omqs.databinding.ActivityLoginBinding
import com.navinfo.omqs.ui.activity.CheckPermissionsActivity
import com.navinfo.omqs.ui.activity.map.MainActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
/**
* 登陆页面
@@ -72,36 +77,46 @@ class LoginActivity : CheckPermissionsActivity() {
LoginStatus.LOGIN_STATUS_NET_LOADING -> {
loginDialog("验证用户信息...")
}
LoginStatus.LOGIN_STATUS_FOLDER_INIT -> {
loginDialog("检查本地数据...")
}
LoginStatus.LOGIN_STATUS_FOLDER_FAILURE -> {
Toast.makeText(this, "文件夹初始化失败", Toast.LENGTH_SHORT).show()
loginDialog?.dismiss()
loginDialog = null
}
LoginStatus.LOGIN_STATUS_NET_FAILURE -> {
Toast.makeText(this, "网络访问失败", Toast.LENGTH_SHORT).show()
loginDialog?.dismiss()
loginDialog = null
}
LoginStatus.LOGIN_STATUS_SUCCESS -> {
loginDialog?.dismiss()
loginDialog = null
//Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show()
val intent = Intent(this@LoginActivity, MainActivity::class.java)
startActivity(intent)
finish()
}
LoginStatus.LOGIN_STATUS_CANCEL -> {
loginDialog?.dismiss()
loginDialog = null
}
LoginStatus.LOGIN_STATUS_NET_OFFLINE_MAP -> {
loginDialog("检查离线地图...")
}
LoginStatus.LOGIN_STATUS_NET_GET_TASK_LIST -> {
loginDialog("获取任务列表...")
}
else -> {}
}
}
@@ -109,7 +124,6 @@ class LoginActivity : CheckPermissionsActivity() {
private fun initView() {
//登录校验,初始化成功
viewModel.loginStatus.observe(this, loginObserve)
}
/**

File diff suppressed because one or more lines are too long

View File

@@ -51,6 +51,7 @@ import com.navinfo.omqs.util.SoundMeter
import com.navinfo.omqs.util.SpeakMode
import dagger.hilt.android.lifecycle.HiltViewModel
import io.realm.Realm
import io.realm.RealmConfiguration
import io.realm.RealmSet
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
@@ -302,6 +303,9 @@ class MainViewModel @Inject constructor(
}
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
MapParamUtils.setTaskId(sharedPreferences.getInt(Constant.SELECT_TASK_ID, -1))
Constant.currentSelectTaskFolder = File(Constant.USER_DATA_PATH +"/${MapParamUtils.getTaskId()}")
Constant.currentSelectTaskConfig = RealmConfiguration.Builder().directory(Constant.currentSelectTaskFolder).name("OMQS.realm").encryptionKey(Constant.PASSWORD).allowQueriesOnUiThread(true).schemaVersion(2).build()
MapParamUtils.setTaskConfig(Constant.currentSelectTaskConfig)
socketServer = SocketServer(mapController, traceDataBase, sharedPreferences)
}
@@ -316,6 +320,7 @@ class MainViewModel @Inject constructor(
if (res != null) {
currentTaskBean = realm.copyFromRealm(res)
}
realm.close()
}
@@ -337,9 +342,10 @@ class MainViewModel @Inject constructor(
val realm = realmOperateHelper.getRealmDefaultInstance()
realm.executeTransaction {
val objects =
realmOperateHelper.getRealmTools(QsRecordBean::class.java, false).findAll()
realmOperateHelper.getRealmTools(QsRecordBean::class.java).findAll()
list = realm.copyFromRealm(objects)
}
realm.close()
mapController.markerHandle.removeAllQsMarker()
for (item in list) {
mapController.markerHandle.addOrUpdateQsRecordMark(item)
@@ -354,11 +360,11 @@ class MainViewModel @Inject constructor(
var list = mutableListOf<NoteBean>()
val realm = realmOperateHelper.getRealmDefaultInstance()
realm.executeTransaction {
val objects = realmOperateHelper.getRealmTools(NoteBean::class.java, false).findAll()
val objects = realmOperateHelper.getRealmTools(NoteBean::class.java).findAll()
list = realm.copyFromRealm(objects)
}
realm.close()
for (item in list) {
mapController.markerHandle.addOrUpdateNoteMark(item)
@@ -607,10 +613,10 @@ class MainViewModel @Inject constructor(
}
val realm = realmOperateHelper.getRealmDefaultInstance()
val realm = realmOperateHelper.getSelectTaskRealmInstance()
val entity =
realmOperateHelper.getRealmTools(RenderEntity::class.java, true)
realmOperateHelper.getSelectTaskRealmTools(RenderEntity::class.java, true)
.and()
.equalTo("table", DataCodeEnum.OMDB_RESTRICTION.name)
.and()
@@ -620,7 +626,7 @@ class MainViewModel @Inject constructor(
if (entity != null) {
val outLink = entity.properties["linkOut"]
val linkOutEntity =
realmOperateHelper.getRealmTools(RenderEntity::class.java, true)
realmOperateHelper.getSelectTaskRealmTools(RenderEntity::class.java, true)
.and()
.equalTo("table", DataCodeEnum.OMDB_RD_LINK.name).and()
.equalTo(
@@ -633,6 +639,7 @@ class MainViewModel @Inject constructor(
)
}
}
realm.close()
}
liveDataTopSignList.postValue(topSignList.distinctBy { it.name }

View File

@@ -37,6 +37,7 @@ class ConsoleViewModel @Inject constructor() : ViewModel() {
liveDataTaskCount.postValue(count.toInt())
val count2 = realm.where(QsRecordBean::class.java).count()
liveDataEvaluationResultCount.postValue(count2.toInt())
realm.close()
}
}
}

View File

@@ -162,7 +162,6 @@ class EvaluationResultViewModel @Inject constructor(
if (objects != null) {
liveDataTaskBean.postValue(realm.copyFromRealm(objects))
}
//获取当前定位点
val geoPoint = mapController.locationLayerHandler.getCurrentGeoPoint()
//如果不是从面板进来的
@@ -204,6 +203,7 @@ class EvaluationResultViewModel @Inject constructor(
getClassTypeList(bean)
getProblemLinkList()
realm.close()
}
addChatMsgEntity(filePath)
}
@@ -412,6 +412,7 @@ class EvaluationResultViewModel @Inject constructor(
}
mapController.markerHandle.addOrUpdateQsRecordMark(liveDataQsRecordBean.value!!)
liveDataFinish.postValue(true)
realm.close()
}
}
@@ -435,6 +436,7 @@ class EvaluationResultViewModel @Inject constructor(
mapController.markerHandle.removeQsRecordMark(liveDataQsRecordBean.value!!)
mapController.mMapView.vtmMap.updateMap(true)
liveDataFinish.postValue(true)
realm.close()
}
}
})
@@ -495,6 +497,7 @@ class EvaluationResultViewModel @Inject constructor(
} else {
liveDataToastMessage.postValue("数据读取失败")
}
realm.close()
}
}
@@ -665,6 +668,7 @@ class EvaluationResultViewModel @Inject constructor(
if (objects != null) {
liveDataTaskBean.postValue(realm.copyFromRealm(objects))
}
realm.close()
}
} else {
liveDataFinish.postValue(true)

View File

@@ -117,6 +117,7 @@ class NoteViewModel @Inject constructor(
}
mapController.markerHandle.addOrUpdateNoteMark(mNoteBean!!)
liveDataFinish.postValue(true)
realm.close()
}
}
}
@@ -145,6 +146,7 @@ class NoteViewModel @Inject constructor(
}
mapController.markerHandle.removeNoteMark(mNoteBean!!)
liveDataFinish.postValue(true)
realm.close()
}
}
mDialog.setNegativeButton("取消", null)
@@ -168,6 +170,7 @@ class NoteViewModel @Inject constructor(
canvasView.setDrawPathList(list)
}
}
realm.close()
}
}
}

View File

@@ -43,7 +43,6 @@ class PersonalCenterViewModel @Inject constructor(
@RequiresApi(Build.VERSION_CODES.N)
suspend fun obtainOMDBZipData(importOMDBHelper: ImportOMDBHelper) {
Log.d("OMQSApplication", "开始生成数据")
// Realm.getDefaultInstance().beginTransaction()
val gson = Gson()
val hadLinkFile = File(importOMDBHelper.omdbFile.parentFile, "HAD_LINK.txt")
val hadLinkKindFile = File(importOMDBHelper.omdbFile.parentFile, "HAD_LINK_KIND.txt")
@@ -135,14 +134,7 @@ class PersonalCenterViewModel @Inject constructor(
hadSpeedLimitVarFile, gson.toJson(hadSpeedlimitVar) + "\r", true
)
}
// val properties = RealmDictionary<String?>()
// for (entry in map.entries) {
// properties.putIfAbsent(entry.key, entry.value.toString())
// }
// // 将读取到的sqlite数据插入到Realm中
// Realm.getDefaultInstance().insert(OMDBEntity(tableName, properties))
}
}
}
}
ZipUtils.zipFiles(

View File

@@ -32,6 +32,7 @@ class QsRecordListViewModel @Inject constructor(
val realm = Realm.getDefaultInstance()
val objects = realm.where(QsRecordBean::class.java).equalTo("taskId",taskId).findAll()
liveDataQSList.postValue(realm.copyFromRealm(objects))
realm.close()
}
}

View File

@@ -123,6 +123,7 @@ class TaskLinkViewModel @Inject constructor(
val realm = Realm.getDefaultInstance()
val res = realm.where(TaskBean::class.java).equalTo("id", id).findFirst()
liveDataTaskBean.postValue(res?.let { realm.copyFromRealm(it) })
realm.close()
}
}
@@ -234,6 +235,7 @@ class TaskLinkViewModel @Inject constructor(
.putString(Constant.SHARED_SYNC_TASK_LINK_ID, hadLinkDvoBean!!.linkPid)
.apply()
liveDataFinish.postValue(true)
realm.close()
}
}
@@ -302,6 +304,7 @@ class TaskLinkViewModel @Inject constructor(
mapController.measureLayerHandler.initPathLine(hadLinkDvoBean?.geometry!!)
}
}
realm.close()
}
}
@@ -354,6 +357,7 @@ class TaskLinkViewModel @Inject constructor(
mapController.lineHandler.removeTaskLink(hadLinkDvoBean!!.linkPid)
mapController.mMapView.vtmMap.updateMap(true)
liveDataFinish.postValue(true)
realm.close()
}
}
mDialog.setNegativeButton("取消", null)

View File

@@ -29,8 +29,10 @@ import com.navinfo.omqs.ui.dialog.FirstDialog
import com.navinfo.omqs.util.DateTimeUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import io.realm.Realm
import io.realm.RealmConfiguration
import kotlinx.coroutines.*
import org.oscim.core.GeoPoint
import java.io.File
import javax.inject.Inject
@@ -143,6 +145,7 @@ class TaskViewModel @Inject constructor(
mapController.lineHandler.addTaskLink(hadLinkDvoBean)
mapController.layerManagerHandler.updateOMDBVectorTileLayer()
mapController.mMapView.vtmMap.updateMap(true)
realm.close()
}
}
}
@@ -218,6 +221,7 @@ class TaskViewModel @Inject constructor(
}
}
realm.close()
}
getLocalTaskList()
}
@@ -262,6 +266,7 @@ class TaskViewModel @Inject constructor(
FileManager.checkOMDBFileInfo(item)
}
liveDataTaskList.postValue(taskList)
realm.close()
val id = sharedPreferences.getInt(Constant.SELECT_TASK_ID, -1)
if (id > -1) {
for (item in taskList) {
@@ -290,6 +295,9 @@ class TaskViewModel @Inject constructor(
liveDataTaskLinks.value = taskBean.hadLinkDvoList
showTaskLinks(taskBean)
MapParamUtils.setTaskId(taskBean.id)
Constant.currentSelectTaskFolder = File(Constant.USER_DATA_PATH +"/${MapParamUtils.getTaskId()}")
Constant.currentSelectTaskConfig = RealmConfiguration.Builder().directory(Constant.currentSelectTaskFolder).name("OMQS.realm").encryptionKey(Constant.PASSWORD).allowQueriesOnUiThread(true).schemaVersion(2).build()
MapParamUtils.setTaskConfig(Constant.currentSelectTaskConfig)
mapController.layerManagerHandler.updateOMDBVectorTileLayer()
mapController.mMapView.updateMap(true)
@@ -383,6 +391,7 @@ class TaskViewModel @Inject constructor(
realm.executeTransaction { r ->
r.copyToRealmOrUpdate(it)
}
realm.close()
}
}
@@ -399,6 +408,7 @@ class TaskViewModel @Inject constructor(
val list = realm.where(TaskBean::class.java).contains("evaluationTaskName", key).or()
.contains("dataVersion", key).or().contains("cityName", key).findAll()
liveDataTaskList.postValue(realm.copyFromRealm(list))
realm.close()
}
}
@@ -465,6 +475,7 @@ class TaskViewModel @Inject constructor(
}
liveDataTaskList.postValue(taskList)
liveDataCloseTask.postValue(true)
realm.close()
}
}
mDialog.setNegativeButton(
@@ -507,6 +518,7 @@ class TaskViewModel @Inject constructor(
map[taskBean] = true
liveDataTaskUpload.postValue(map)
}
realm.close()
}
}
@@ -655,6 +667,7 @@ class TaskViewModel @Inject constructor(
mapController.lineHandler.removeTaskLink(hadLinkDvoBean.linkPid)
liveDataTaskLinks.postValue(currentSelectTaskBean!!.hadLinkDvoList)
}
realm.close()
}
}
mDialog.setNegativeButton(