Skip to content

Commit 4523825

Browse files
DominicGBauerDominicGBauer
andauthored
fix: null pointer exception when sync has not happened yet (#54)
Co-authored-by: DominicGBauer <[email protected]>
1 parent 01b3119 commit 4523825

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,23 @@ internal class PowerSyncDatabaseImpl(
301301

302302
private suspend fun updateHasSynced() {
303303
// Query the database to see if any data has been synced.
304-
val timestamp = internalDb.getOptional("SELECT powersync_last_synced_at() as synced_at", null) { cursor ->
305-
cursor.getString(0)!!
306-
}
304+
try {
305+
val timestamp = internalDb.getOptional("SELECT powersync_last_synced_at() as synced_at", null) { cursor ->
306+
cursor.getString(0)!!
307+
}
307308

308-
val hasSynced = timestamp != null
309-
if (hasSynced != currentStatus.hasSynced) {
310-
val formattedDateTime = "${timestamp!!.replace(" ","T").toLocalDateTime()}Z"
311-
val lastSyncedAt = Instant.parse(formattedDateTime)
312-
currentStatus.update(hasSynced = hasSynced, lastSyncedAt = lastSyncedAt)
309+
val hasSynced = timestamp != null
310+
if (hasSynced != currentStatus.hasSynced) {
311+
val formattedDateTime = "${timestamp!!.replace(" ","T").toLocalDateTime()}Z"
312+
val lastSyncedAt = Instant.parse(formattedDateTime)
313+
currentStatus.update(hasSynced = hasSynced, lastSyncedAt = lastSyncedAt)
314+
}
315+
} catch (e: Exception) {
316+
if(e is NullPointerException) {
317+
// No data has been synced which results in a null pointer exception
318+
// and can be safely ignored.
319+
return
320+
}
313321
}
314322
}
315323

0 commit comments

Comments
 (0)