Skip to content

Commit af18b85

Browse files
committed
2nd attempt
1 parent 54028a9 commit af18b85

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

app/src/main/java/org/ole/planet/myplanet/repository/NotificationRepositoryImpl.kt

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,18 @@ class NotificationRepositoryImpl @Inject constructor(
5252
android.util.Log.d("NotificationRepository", "createNotificationsIfMissing: Starting batch create for ${notifications.size} notifications")
5353

5454
executeTransaction { realm ->
55-
// OPTIMIZATION: Query only for the specific relatedIds we're checking
56-
// This is much faster than loading all notifications
57-
val relatedIds = notifications.mapNotNull { it.relatedId }.distinct()
58-
59-
val existingNotifications = if (relatedIds.isNotEmpty()) {
60-
realm.where(RealmNotification::class.java)
61-
.equalTo("userId", actualUserId)
62-
.`in`("relatedId", relatedIds.toTypedArray())
63-
.findAll()
64-
} else {
65-
// If no relatedIds, query for null relatedIds only
66-
realm.where(RealmNotification::class.java)
67-
.equalTo("userId", actualUserId)
68-
.isNull("relatedId")
69-
.findAll()
70-
}
55+
// OPTIMIZATION: Query only UNREAD notifications (old read ones don't matter)
56+
// This is much faster than searching through all historical notifications
57+
val queryStartTime = System.currentTimeMillis()
58+
val existingUnreadNotifications = realm.where(RealmNotification::class.java)
59+
.equalTo("userId", actualUserId)
60+
.equalTo("isRead", false)
61+
.findAll()
7162

72-
android.util.Log.d("NotificationRepository", "createNotificationsIfMissing: Found ${existingNotifications.size} existing notifications")
63+
android.util.Log.d("NotificationRepository", "createNotificationsIfMissing: Found ${existingUnreadNotifications.size} unread notifications in ${System.currentTimeMillis() - queryStartTime}ms")
7364

7465
// Create a set of existing notification keys for O(1) lookup
75-
val existingKeys = existingNotifications.mapNotNull { existing ->
66+
val existingKeys = existingUnreadNotifications.mapNotNull { existing ->
7667
if (existing.relatedId != null) {
7768
"${existing.type}:${existing.relatedId}"
7869
} else {
@@ -82,6 +73,7 @@ class NotificationRepositoryImpl @Inject constructor(
8273

8374
val now = Date()
8475
var createdCount = 0
76+
var skippedCount = 0
8577

8678
// Batch create all missing notifications in a single transaction
8779
notifications.forEach { notification ->
@@ -100,11 +92,13 @@ class NotificationRepositoryImpl @Inject constructor(
10092
this.createdAt = now
10193
}
10294
createdCount++
95+
} else {
96+
skippedCount++
10397
}
10498
}
10599

106100
val endTime = System.currentTimeMillis()
107-
android.util.Log.d("NotificationRepository", "createNotificationsIfMissing: Created $createdCount new notifications in ${endTime - startTime}ms")
101+
android.util.Log.d("NotificationRepository", "createNotificationsIfMissing: Created $createdCount new, skipped $skippedCount existing in ${endTime - startTime}ms total")
108102
}
109103
}
110104

0 commit comments

Comments
 (0)