Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SES-1931] - Fix debouncer crash #1492

Merged
merged 1 commit into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.content.Context
import org.session.libsession.utilities.Debouncer
import org.thoughtcrime.securesms.ApplicationContext

class ConversationNotificationDebouncer(private val context: Context) {
class ConversationNotificationDebouncer(private val context: ApplicationContext) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

private val threadIDs = mutableSetOf<Long>()
private val handler = (context.applicationContext as ApplicationContext).conversationListNotificationHandler
private val handler = context.conversationListNotificationHandler
private val debouncer = Debouncer(handler, 100)

companion object {
Expand All @@ -17,20 +17,28 @@ class ConversationNotificationDebouncer(private val context: Context) {
@Synchronized
fun get(context: Context): ConversationNotificationDebouncer {
if (::shared.isInitialized) { return shared }
shared = ConversationNotificationDebouncer(context)
shared = ConversationNotificationDebouncer(context.applicationContext as ApplicationContext)
return shared
}
}

fun notify(threadID: Long) {
threadIDs.add(threadID)
synchronized(threadIDs) {
threadIDs.add(threadID)
}

debouncer.publish { publish() }
}

private fun publish() {
for (threadID in threadIDs.toList()) {
val toNotify = synchronized(threadIDs) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just thinking, you can save the list copy by making it a var:

private fun publish() {
    synchronized(this) {
        threadIDs.also { threadIDs = mutableSetOf() }
    }.forEach {
context.contentResolver.notifyChange(DatabaseContentProviders.Conversation.getUriForThread(it), null)
}

but it needs the same synchronized(this) in notify.

Creating a new instance still needs an allocation, but toList needs an allocation and an O(n) copy. Probably negligible performance, so don't feel obliged to change it, but the syntax is nice-ish too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually went down this path to avoid the copying and using AtomicReference to avoid synchronization but had since decided against it, as I think easier to read code is more important than a few nanoseconds off the hotspot.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also considered using a sorted long[] to avoid boxing but feel really unnecessary here, most of the time you only see a few items in the list really...

val copy = threadIDs.toList()
threadIDs.clear()
copy
}

for (threadID in toNotify) {
context.contentResolver.notifyChange(DatabaseContentProviders.Conversation.getUriForThread(threadID), null)
}
threadIDs.clear()
}
}