realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

Realm crashes within bottomsheet #7587

Closed cherimo closed 2 years ago

cherimo commented 2 years ago

I'm currently running into an annoying issue where I don't know if it's a bug or if I've implemented something wrong. This causes my app to crash sporadically with no apparent error message. I've been searching on Stackoverflow and other pages but so far no solution. The error it self to long to post, so I post the first lines if its not a problem.

I am using Android 12 - Pixel 4 with Kotlin 1.5.31 and Realm 10.4.0

Error Message:

    2021-11-06 21:19:55.269 31533-31533/com.example.test A/ple.test: java_vm_ext.cc:577] JNI DETECTED ERROR IN APPLICATION: JNI NewLocalRef called with pending exception java.lang.NullPointerException: 
        java_vm_ext.cc:577] (Throwable with no stack trace)
        java_vm_ext.cc:577] 
        java_vm_ext.cc:577]     in call to NewLocalRef
        java_vm_ext.cc:577]     from void android.os.MessageQueue.nativePollOnce(long, int)
2021-11-06 21:19:55.455 31533-31533/com.example.test A/ple.test: runtime.cc:655] Runtime aborting...
    runtime.cc:655] Dumping all threads without mutator lock held
    runtime.cc:655] All threads:
    runtime.cc:655] DALVIK THREADS (40):
    runtime.cc:655] "main" prio=10 tid=1 Runnable
…

I've been troubleshooting all day today to get to the point that problem lies within BottomSheetDialogFragment and RealmDB. So when I start the function "updateContactChat" everything works fine, no errors. As soon as I open the same BottomSheetDialogFragment again and start the function "updateContactChat" the app crashes completely with the above error.

The is the code.

     class ChatAdd : BottomSheetDialogFragment() {

        private val appRealm = Realm.getInstance(General.secureDB())
        private var binding: FragmentChatsAddBinding? = null

        override fun getTheme(): Int = R.style.BottomSheetDialogTheme
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            super.onCreateView(inflater, container, savedInstanceState)

            binding = FragmentChatsAddBinding.inflate(inflater, container, false)

            return binding!!.root
        }

        override fun onStart() {
            super.onStart()
            BottomSheetBehavior.from(requireView().parent as View).apply {
                state = BottomSheetBehavior.STATE_EXPANDED
            }
        }

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            R.style.DialogAnimation.also { dialog!!.window?.attributes?.windowAnimations = it }

            binding!!.contactListContainer.layoutManager = LinearLayoutManager(activity)
            binding!!.contactListContainer.setHasFixedSize(true)
            binding!!.contactListContainer.addItemDecoration(ContactsList.ItemDecoration())

            showList()
        }

        private fun showList() {

            var contacts: RealmResults<SchemeContacts>? = null

            binding!!.contactListContainer.adapter = ContactsAdapter(contacts, true, object :
                ContactsAdapter.ContactItemClickListener {
                override fun onClick(contact: SchemeContacts?) {

                    contact?.contact_id?.let {
                        CoroutineScope(Dispatchers.IO).launch {
                            try {
                                updateContactChat(it)
                            }catch(e:Exception){
                                Log.d("log", "catch error: "+e)
                            }
                        }
                    }

                }
            })

        }

        private fun updateContactChat(contactId: String) {

            val updateRealm = Realm.getInstance(General.secureDB())

            updateRealm.executeTransaction { bgRealm ->

                val user =
                    bgRealm.where<SchemeContacts>().equalTo("contact_id", contactId).findFirst()!!
                user.contact_chat = true
                user.contact_chatupdated = LocalDateTime.now().toString()
                user.contact_chatnew = 0
                user.contact_chatstatus = 0

            }

            updateRealm.close()
            dialog!!.dismiss()

        }

        override fun onDestroy() {
            super.onDestroy()
            appRealm.close()
            binding = null
        }
    }

I have tried several solutions also with Realm DB but to no avail unfortunately. I hope someone here can point me in the right direction.