Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit 60de3c5

Browse files
authored
Merge pull request #181 from synonymdev/android-rgs-background-thread
fix: android perform rgs download and update on background thread
2 parents 22b58c6 + 62fdc1d commit 60de3c5

2 files changed

Lines changed: 42 additions & 24 deletions

File tree

lib/android/src/main/java/com/reactnativeldk/Helpers.kt

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,39 +215,58 @@ fun URL.downloadFile(destination: String, completion: (error: Error?) -> Unit) {
215215
}
216216

217217
fun RapidGossipSync.downloadAndUpdateGraph(downloadUrl: String, tempStoragePath: String, timestamp: Long, completion: (error: Error?) -> Unit) {
218-
val destinationFile = "$tempStoragePath$timestamp.bin"
218+
Thread(Runnable {
219+
val destinationFile = "$tempStoragePath$timestamp.bin"
219220

220-
URL(downloadUrl + timestamp).downloadFile(destinationFile) {
221-
if (it != null) {
222-
return@downloadFile completion(it)
223-
}
221+
URL(downloadUrl + timestamp).downloadFile(destinationFile) {
222+
if (it != null) {
223+
UiThreadUtil.runOnUiThread {
224+
completion(it)
225+
}
226+
return@downloadFile
227+
}
224228

225-
val res = update_network_graph_no_std(File(destinationFile).readBytes(), Option_u64Z.some((System.currentTimeMillis() / 1000)))
226-
if (!res.is_ok()) {
227-
val error = res as? Result_u32GraphSyncErrorZ.Result_u32GraphSyncErrorZ_Err
229+
val res = update_network_graph_no_std(File(destinationFile).readBytes(), Option_u64Z.some((System.currentTimeMillis() / 1000)))
230+
if (!res.is_ok()) {
231+
val error = res as? Result_u32GraphSyncErrorZ.Result_u32GraphSyncErrorZ_Err
228232

229-
(error?.err as? GraphSyncError.LightningError)?.let { lightningError ->
230-
return@downloadFile completion(Error("Rapid sync LightningError. " + lightningError.lightning_error._err))
231-
}
233+
(error?.err as? GraphSyncError.LightningError)?.let { lightningError ->
234+
UiThreadUtil.runOnUiThread {
235+
completion(Error("Rapid sync LightningError. " + lightningError.lightning_error._err))
236+
}
237+
return@downloadFile
238+
}
232239

233-
(error?.err as? GraphSyncError.DecodeError)?.let { decodeError ->
234-
(decodeError.decode_error as? DecodeError.Io)?.let { decodeIOError ->
235-
return@downloadFile completion(Error("Rapid sync DecodeError. " + decodeIOError.io.ordinal))
240+
(error?.err as? GraphSyncError.DecodeError)?.let { decodeError ->
241+
(decodeError.decode_error as? DecodeError.Io)?.let { decodeIOError ->
242+
UiThreadUtil.runOnUiThread {
243+
completion(Error("Rapid sync DecodeError. " + decodeIOError.io.ordinal))
244+
}
245+
return@downloadFile
246+
}
247+
248+
UiThreadUtil.runOnUiThread {
249+
completion(Error("Rapid sync DecodeError"))
250+
}
251+
return@downloadFile
236252
}
237253

238-
return@downloadFile completion(Error("Rapid sync DecodeError"))
254+
UiThreadUtil.runOnUiThread {
255+
completion(Error("Unknown rapid sync error."))
256+
}
257+
return@downloadFile
239258
}
240259

241-
return@downloadFile completion(Error("Unknown rapid sync error."))
242-
}
260+
val usedFile = File(destinationFile)
261+
if (usedFile.exists()) {
262+
usedFile.delete()
263+
}
243264

244-
val usedFile = File(destinationFile)
245-
if (usedFile.exists()) {
246-
usedFile.delete()
265+
UiThreadUtil.runOnUiThread {
266+
completion(null)
267+
}
247268
}
248-
249-
completion(null)
250-
}
269+
}).start()
251270
}
252271

253272
fun ChannelHandshakeConfig.mergeWithMap(map: ReadableMap?): ChannelHandshakeConfig {

lib/android/src/main/java/com/reactnativeldk/classes/LdkPersister.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class LdkPersister {
3636

3737
return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_Completed
3838
} catch (e: Exception) {
39-
LdkEventEmitter.send(EventTypes.emergency_force_close_channel, body)
4039
return ChannelMonitorUpdateStatus.LDKChannelMonitorUpdateStatus_UnrecoverableError
4140
}
4241
}

0 commit comments

Comments
 (0)