Creating a Realm Model Class
@RealmClass
open class Note() : RealmModel {
@PrimaryKey
var id: String = ""
@Required
var title: String? = ""
@Required
var description: String? = ""
}
Adding Data Into Realm
val note = r.createObject(Note::class.java, UUID.randomUUID().toString())
note.title = noteTitle
note.description = noteDescription
note.title = noteTitle
note.description = noteDescription
realm.insertOrUpdate(note)
}
}
“>
fun addNote(noteTitle: String, noteDescription: String) { realm.executeTransaction { r: Realm -> val note = r.createObject(Note::class.java, UUID.randomUUID().toString()) note.title = noteTitle note.description = noteDescription realm.insertOrUpdate(note) } }
Query data from the Realm database
<div class="highlight highlight-source-kotlin position-relative" data-snippet-clipboard-copy-content="private fun getAllNotes(): MutableLiveData<List> {
val list = MutableLiveData<List>()
val notes = realm.where(Note::class.java).findAll()
list.value = notes?.subList(0, notes.size)
return list
}
“>
private fun getAllNotes(): MutableLiveData<List<Note>> { val list = MutableLiveData<List<Note>>() val notes = realm.where(Note::class.java).findAll() list.value = notes?.subList(0, notes.size) return list }