Hi, I´m using Jetpack compose kotlin and Mongo db community I generate a database app service with login with google. I “Device Sync” with development mode. I generated rules , without any schema.
Problem:
Once the user log in ok (the log shows users ok) and try to navigate to homeScreen the app crash with this error:
2024-05-01 12:30:51.200 4281-4281 REALM com.example.myApp D Realm opened: /data/user/0/com.example.myApp/files/mongodb-realm/application-0-xxxx/xxx(codeuserinMongo userAccess)/default.realm
2024-05-01 12:30:51.224 4281-4281 REALM com.example.myApp D Realm closed: RealmImpl[/data/user/0/com.example.myApp/files/mongodb-realm/application-0 xxxx/xxx(codeuserinMongo userAccess)/default.realm}] /data/user/0/com.example.myApp/files/mongodb-realm/application-0-xxxx/xxx(codeuserinMongo userAccess)/default.realm
2024-05-01 12:30:51.225 4281-4281 REALM com.example.myApp D An error happened while trying to reset the realm after opening it for the first time failed. The realm must be manually deleted if initialData
and initialSubscriptions
should run again: java.lang.IllegalStateException: Cannot delete Realm located at ‘/data/user/0/com.example.myApp/files/mongodb-realm/application-0-gierjtt/663182a23ff420cd83e5daee/default.realm’, did you close it before calling ‘deleteRealm’?: RealmCoreException([39]: Cannot delete files of an open Realm: ‘/data/user/0/com.example.myApp/files/mongodb-realm/application-0-xxxx/xxx(codeuserinMongo userAccess)/default.realm’ is still in use.)
2024-05-01 12:30:51.231 4281-4281 AndroidRuntime com.example.myApp E FATAL EXCEPTION: main java.lang.ExceptionInInitializerError
Configurations:
In Mongo:
database:
visitors
reserve default index _id
In my app:
This is my model class in app client android:
class Reserve: RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId.invoke()
var owner_id: String = “”
var cabin: String = “”
@Ignore
var dates: RealmList = realmListOf()
var adults : Int = 1
var children : Int = 0
var babies : Int = 0
var confirmed : Boolean = false
var userM: UserM? = null
var personalData: RealmList = realmListOf()
var timestamp: RealmInstant = RealmInstant.now()
}
class Dates: EmbeddedRealmObject {
var dateIn: String = “”
var dateOut: String =“”
}
class UserM : EmbeddedRealmObject {
var nameCredential: String = “”
var mailCredential: String = “”
var tokenID: String = “”
// var personalData: PersonalData? = null
}
class PersonalData: EmbeddedRealmObject {
var grandName : String =“”
var firstName : String = “”
var dni: String = “..__”
}
This is my MongoDB class implementation:
/*Singleton Instance/
object RealmApp {
private var app: App? = null
fun getInstance(appId: String): App {
if (app == null) {
app = App.create(appId) // normal use
}
return app!!
}
}
object MongoDB : MongoRepository {
/**configuration Realm remote db: */
/** properties: */
private val app = RealmApp.getInstance(APP_ID) //with singleton
private val user = app.currentUser
private lateinit var realm: Realm
// init Block:
init {
Log.d("user in MongoDB","user.id before call configureTheRealm() : ${user?.id}" )
Log.d("user in MongoDB","app.currentUser before call configureTheRealm() ${app.currentUser}" )
configureTheRealm()
}
/**method implementation configuration the sync with user and schema: */
override fun configureTheRealm() {
if (user != null) {
Log.d("user in MongoDB", "user.id after call: ${user.id}") //ok user mongo
Log.d("user in MongoDB", "app.currentUser after call: ${app.currentUser}")
// Create a Realm configuration:
val realmConfig = RealmConfiguration.Builder(
schema = setOf()
)
.deleteRealmIfMigrationNeeded() // This line is optional, use if applicable
.build()
Log.d("Realm logcat", "realmConfig.schema: ${realmConfig.schema.size}")
// Delete the Realm instance
Realm.deleteRealm(realmConfig)
//create a Sync configuration:
val config = SyncConfiguration
.Builder(
user,
setOf(Reserve::class, Dates::class, UserM::class, PersonalData::class)
)
.initialSubscriptions { sub ->
add(query = sub.query<Reserve>(query = "owner_id == $0", user.id))
}
.log(LogLevel.ALL)
.build()
realm = Realm.open(config)
}
}
/** implementation methods to access db: */
override fun getData(): Flow<List<Reserve>> {
return realm.query<Reserve>().asFlow().map { it.list }
}
override fun filterData(name: String): Flow<List<Reserve>> {
return realm.query<Reserve>(query = "cabin CONTAINS[c] $0", name)
.asFlow().map { it.list }
}
override suspend fun insertReserve(reserve: Reserve) {
if (user != null) {
realm.write {
try {
copyToRealm(reserve.apply { owner_id = user.id })
} catch (e: Exception) {
Log.d("MongoRepository", e.message.toString())
}
}
}
}
override suspend fun updateReserve(reserve: Reserve) {
realm.write {
val queriedReserve =
query<Reserve>(query = "_id ==$0", reserve._id)
.first()
.find()
if (queriedReserve != null) {
queriedReserve.cabin = reserve.cabin
} else {
Log.d("MongoRepository", "Queried Reserve does not exist")
}
}
}
override suspend fun deleteReserve(id: ObjectId) {
realm.write {
try {
val reserve = query<Reserve>(query = "_id==$0", id)
.first()
.find()
reserve?.let { delete(it) }
} catch (e: Exception) {
Log.d("MongoRepository", "${e.message}")
}
}
}
}
Note: I use ViewModel and Hilt injection.
Note: the user is log ok, but never get an instance of MongoDB then when called produce this error.
Thanks for any help.