user2511882
user2511882

Reputation: 9152

Realm Kotlin save data

I am using Realm with Kotlin for a sample application. However, for whatever reason, the data is not being persisted after calling commitTranscation(). The findAll() method returns size() as 0 all the time.

WRITE

realm.beginTransaction()
  val userImage = UserImages()
  userImage.image = byteArray
  realm.commitTransaction()

READ

val userImage = realm.where(UserImages::class.java).findAll().last()

The above line always returns size() as 0 and crashes the app.

If it helps, the image property is a byte array.

What is the issue here?

Thanks!

Upvotes: 0

Views: 1217

Answers (1)

gpunto
gpunto

Reputation: 2852

You have to use insert to actually save the new object. Something like:

val userImage = UserImages()
userImage.image = byteArray
realm.beginTransaction()
realm.insert(userImage)
realm.commitTransaction()

PS: you can use executeTransaction instead of the begin + end pair. Like this:

realm.executeTransaction {
  realm.insert(userImage)
}

Upvotes: 1

Related Questions