Your code is generally unsafe. Please fix your code, read the documentation, stuff like that.
Also, RealmList expects ? extends RealmModel
, so you need to use T: RealmModel
with out
.
fun <T: RealmModel> RealmList<out T?>.saveAll() {
Realm.getDefaultInstance().use { realm ->
val wasInTransaction = realm.isInTransaction()
try {
if(!wasInTransaction) {
realm.beginTransaction()
}
this.forEach {
item -> item?.let { realm.insert(it) }
}
if(!wasInTransaction) {
realm.commitTransaction()
}
} catch(e: Throwable) {
if(realm.isInTransaction()) {
realm.cancelTransaction()
}
}
}
}