Skip to content

Commit

Permalink
fix: completion run on main thread (#215)
Browse files Browse the repository at this point in the history
* fix: completion run on main thread

* fix lint

* update changelog
  • Loading branch information
wasnot authored Feb 28, 2023
1 parent 9851565 commit 795a5c5
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 212 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Inboxモジュールを使用する場合のみ設定が必要です。
** 🔨CHANGED**
- identifyイベントのuser_idに明示的に空文字が指定された場合に警告を出力するように変更しました。
- Trackerのイベント送信のコールバックを、iOS SDKに合わせUIスレッドで呼び出すようにしました。

### Variables 2.4.0
** 🔨CHANGED**
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/io/karte/android/tracking/queue/Dispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package io.karte.android.tracking.queue

import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.os.Process
import io.karte.android.KarteApp
import io.karte.android.core.library.ActionModule
Expand Down Expand Up @@ -50,6 +51,7 @@ internal class Dispatcher {
private val thread =
HandlerThread(THREAD_NAME, Process.THREAD_PRIORITY_LOWEST).apply { start() }
private val handler: Handler = Handler(thread.looper)
private val mainHandler = Handler(Looper.getMainLooper())
private val completions = mutableMapOf<Long, TrackCompletion>()
private var isSuspend: Boolean = false
set(value) {
Expand Down Expand Up @@ -87,7 +89,7 @@ internal class Dispatcher {
LOG_TAG,
"Failed to push Event to queue because unretryable event was detected while offline"
)
completion?.onComplete(false)
mainHandler.post { completion?.onComplete(false) }
return
}
val eventInvalidMessages = EventValidator.getInvalidMessages(record.event)
Expand All @@ -99,7 +101,7 @@ internal class Dispatcher {
completion?.let {
if (id == -1L) {
Logger.e(LOG_TAG, "Failed to push Event to queue")
completion.onComplete(false)
mainHandler.post { completion.onComplete(false) }
} else {
completions[id] = it
}
Expand Down Expand Up @@ -198,7 +200,7 @@ internal class Dispatcher {
private fun removeFromQueue(events: List<EventRecord>, isSuccessful: Boolean) {
events.forEach {
DataStore.delete(it)
completions.remove(it.id)?.onComplete(isSuccessful)
completions.remove(it.id)?.let { mainHandler.post { it.onComplete(isSuccessful) } }
}
}

Expand All @@ -221,7 +223,7 @@ internal class Dispatcher {
Logger.w(LOG_TAG, logMessage)
DataStore.delete(it)
}
completions.remove(it.id)?.onComplete(false)
completions.remove(it.id)?.let { mainHandler.post { it.onComplete(false) } }
}
if (minRetryCount > MAX_RETRY_COUNT) return
val retryInterval = retryIntervalMs(minRetryCount)
Expand Down
Loading

0 comments on commit 795a5c5

Please sign in to comment.