fix: v0.3.0.3 — batched hashtag snapshot so Recent Tags fills promptly

The user reported the drawer's Recent Tags list eventually rendered,
but only after a couple of minutes. Root cause was inside
NoteRepositoryImpl:

  1. hashtagUsageSnapshot() called noteBox.get(x.noteId) once per
     cross-ref row. Most notes have multiple hashtags, so each note
     was fetched many times — O(M) Box.get calls where M is the xref
     count.

  2. observeHashtagsByRecentUse() called trySend(snapshot()) BEFORE
     setting up the ObjectBox subscription, then also inside the
     observer. The first emission doubled the work; if the first
     trySend ran before the upstream was warm, the data path itself
     was slow.

Two surgical fixes inside NoteRepositoryImpl.kt:

  - hashtagUsageSnapshot(): batch-load notes by id (one Box.get per
    distinct noteId, not per xref) and tags by id (one Box.get per
    distinct tagId). Net reads drop from (M + N) to
    (distinct noteIds + distinct tagIds) — typically a 5–20×
    reduction.

  - observeHashtagsByRecentUse(): rely solely on the ObjectBox
    observer's first fire (ObjectBox delivers the current query
    state immediately on subscribe). Drop the manual pre-subscribe
    trySend.

VERSION
  - app/build.gradle.kts: versionCode 7 → 8, versionName '0.3.0.2' →
    '0.3.0.3'.
  - README.md changelog: new 0.3.0.3 row.
  - Incido Development Plan.txt §10: new 0.3.0.3 row.

VERIFIED
  - ./gradlew assembleDebug assembleRelease both green.
  - versionCode=8, versionName='0.3.0.3'.
  - No INTERNET permission.
  - Release signed (v2 scheme verified).
This commit is contained in:
Incido Developer committed 2026-08-15 03:18:29 +12:00
1 parent a4651b3d63
commit 5b82d2a9fe
4 files changed
+38 -14

No files matched your search

@@ -78,8 +78,13 @@ class NoteRepositoryImpl(
}.flowOn(Dispatchers.Default)
override fun observeHashtagsByRecentUse(): Flow<List<HashtagUsage>> = callbackFlow {
trySend(hashtagUsageSnapshot())
val sub = noteBox.query().build().subscribe().observer { trySend(hashtagUsageSnapshot()) }
// ObjectBox fires its observer with the full current query state
// immediately on subscribe, so the observer alone is the source
// of truth — no manual trySend that would race the subscription
// and double the work of the snapshot.
val sub = noteBox.query().build().subscribe().observer { _ ->
trySend(hashtagUsageSnapshot())
}
awaitClose { sub.cancel() }
}.flowOn(Dispatchers.Default)
@@ -281,22 +286,39 @@ class NoteRepositoryImpl(
val xrefs: List<NoteHashtagCrossRef> = xrefBox.all
if (xrefs.isEmpty()) return emptyList()
// For each hashtagId, accumulate (lastUsedAt, count).
data class Agg(var lastUsedAt: Long, var count: Int)
val aggregate = HashMap<Long, Agg>(xrefs.size)
// 1) Batch-load each unique noteId once (was once per xref, which
// was O(M) Box.get calls for a notes-with-multiple-tags world).
val noteIds: Set<Long> = xrefs.mapTo(HashSet(xrefs.size)) { it.noteId }
val noteById: Map<Long, NoteEntity> = noteIds.mapNotNull { id ->
noteBox.get(id)?.let { id to it }
}.toMap()
// 2) Aggregate per hashtagId.
val aggregate: HashMap<Long, LongArray> = HashMap(xrefs.size)
for (x in xrefs) {
val note: NoteEntity = noteBox.get(x.noteId) ?: continue
val note: NoteEntity = noteById[x.noteId] ?: continue
val cur = aggregate[x.hashtagId]
if (cur == null) {
aggregate[x.hashtagId] = Agg(note.updatedAt, 1)
aggregate[x.hashtagId] = longArrayOf(note.updatedAt, 1L)
} else {
if (note.updatedAt > cur.lastUsedAt) cur.lastUsedAt = note.updatedAt
cur.count += 1
if (note.updatedAt > cur[0]) cur[0] = note.updatedAt
cur[1] = cur[1] + 1
}
}
return aggregate.mapNotNull { (tagId, agg) ->
val tag: HashtagEntity = tagBox.get(tagId) ?: return@mapNotNull null
HashtagUsage(name = tag.name, noteCount = agg.count, lastUsedAt = agg.lastUsedAt)
// 3) Batch-load each unique tagId once.
val tagIds: List<Long> = aggregate.keys.toList()
val tagById: Map<Long, HashtagEntity> = tagIds.mapNotNull { id ->
tagBox.get(id)?.let { id to it }
}.toMap()
return aggregate.mapNotNull { (tagId, arr) ->
val tag: HashtagEntity = tagById[tagId] ?: return@mapNotNull null
HashtagUsage(
name = tag.name,
noteCount = arr[1].toInt(),
lastUsedAt = arr[0],
)
}.sortedWith(
compareByDescending<HashtagUsage> { it.lastUsedAt }
.thenByDescending { it.noteCount }