#Day29 of #100DaysOfCode
Today was full of meetings… I could not get a lot done and the day did not end with a happy note. I spent time solving the error I was stuck at for a while…
I got some suggestions from a Senior Developer, but I could not implement them in the app until late evening and the code did not work. Realm Queries is a little harder to understand I will understand them soon…
I made some changes in the Add Book Fragment, I was suggested not to use transactions in Read queries, so I removed transaction code from all of them.
private fun openDialogBox(nameList: ArrayList<Author>) {
val builder: AlertDialog.Builder = AlertDialog.Builder(requireContext())
builder.setTitle("Select Author/s")
val selectedAuthors = BooleanArray(nameList.size)
val stringAuthorList = nameList.map{it.name}.toTypedArray()
builder.setMultiChoiceItems(stringAuthorList, selectedAuthors) { dialog, which, isChecked ->
if (isChecked) {
//when checkbox selected, add position
selectedItems.add(which)
} else if (selectedItems.contains(which)) {
//when checkbox unselected
//remove pos from list
selectedItems.remove(which)
}
}
builder.setPositiveButton("OK") { dialog, which ->
selectedItems.forEach{
Timber.d("Authors, ${stringAuthorList[it]}")
nameList.forEach {author ->
if(stringAuthorList[it] == author.name){
bookObject.authors.add(author)
}
}
// realmClass.executeTransactionAsync({ realm ->
// realm.where(Author::class.java).equalTo("name", nameList[it]).findAll()
// .map{addAuthor ->
// bookObject.authors.add(addAuthor)
// }
// }, {
// Timber.d("Author added successfully")
// }, { throwable ->
// Timber.d("Error adding the author %s", throwable.localizedMessage)
// })
}
}
.setNegativeButton("Cancel", DialogInterface.OnClickListener{ dialog, id ->
dialog.dismiss()
})
builder.create().show()
}
I didn’t feel like deleting the code, so I commented it I changed the way I was processing the selected Authors, I used copyFromRealm
to get the AuthorList in form of ArrayList that I passed to openDialogBox
The loadAuthors
is called when I click on Text Button to add Book Author, it reads the list of authors in the database and displays it on a dialog for the user to select shown in above code.
private fun loadAuthors() {
var nameList = ArrayList<Author>()
realmClass.executeTransactionAsync({
val authorList = it.where(Author::class.java).sort("name").findAll()
nameList = it.copyFromRealm(authorList) as ArrayList<Author>
// authorList.toTypedArray().map { obj ->
// nameList.add(obj.name)
// }
}, {
if(nameList.size>0) openDialogBox(nameList)
else {
Toast.makeText(context, "Author List is empty, please add Author Name first", Toast.LENGTH_LONG).show()
}
}, {
Timber.d("Error happened while reading Author List %s", it.localizedMessage)
Toast.makeText(context, "Error happened while reading List, ${it.localizedMessage}", Toast.LENGTH_LONG).show()
})
}
Once the user selects the author index, it searches the list for a name that was selected and saves that to the bookObject.
When submit button is pressed, click listeners get called and save the book to realm database. The last part is giving errors and I have no clue how to solve, so I will continue tomorrow.
2022-03-07 22:42:42.517 14699-14699/? D/AddBookFragment: Error adding the bookObject to Database 'BookRealm' has a primary key, use 'createObject(Class<E>, Object)' instead.
I changed the code after above error to following
addBinding!!.buttonAddBook.setOnClickListener{
realmClass.executeTransactionAsync ({realm ->
val booktoAdd = realm.createObject(BookRealm::class.java, ObjectId())
booktoAdd.name = bookObject.name
booktoAdd.isRead = bookObject.isRead
booktoAdd.authors = realm.copyToRealm(bookObject.authors) as RealmList<Author>
}, {
Timber.d("Book Object Added Successfuly")
}, {throwError ->
Timber.d("Error adding the bookObject to Database %s", throwError.localizedMessage)
})
}
And this gives me a different error that I cannot solve
2022-03-07 23:42:34.721 5257-5257/? D/AddBookFragment: Error adding the bookObject to Database Attempting to create an object of type 'Author' with an existing primary key value '621a992338d6fd61a1126414'.
My Add BookFragment screen is as below:
Until Tomorrow…