@@ -410,31 +410,73 @@ struct AppScene: View {
410410 return String ( data: data, encoding: . utf8)
411411 } ( )
412412
413+ let migrations = MigrationsService . shared
414+
415+ // If migration was already completed, only use VSS backups (RN backups are outdated)
416+ if migrations. isMigrationCompleted {
417+ Logger . info ( " Migration already completed, using VSS backup only (RN backups are outdated) " , context: " AppScene " )
418+ await BackupService . shared. performFullRestoreFromLatestBackup ( )
419+ return
420+ }
421+
413422 // Check for RN backup and get its timestamp
414- let hasRNBackup = await MigrationsService . shared . hasRNRemoteBackup ( mnemonic: mnemonic, passphrase: passphrase)
423+ let hasRNBackup = await migrations . hasRNRemoteBackup ( mnemonic: mnemonic, passphrase: passphrase)
415424 let rnTimestamp : UInt64 ? = await hasRNBackup ? ( try ? RNBackupClient . shared. getLatestBackupTimestamp ( ) ) : nil
416425
417426 // Get VSS backup timestamp
418427 let vssTimestamp = await BackupService . shared. getLatestBackupTime ( )
419428
420429 // Determine which backup is more recent
421430 let shouldRestoreRN : Bool = {
422- guard hasRNBackup else { return false }
423- guard let vss = vssTimestamp, vss > 0 else { return true } // No VSS, use RN
424- guard let rn = rnTimestamp else { return false } // No RN timestamp, use VSS
425- return rn >= vss // RN is same or newer
431+ guard hasRNBackup else {
432+ Logger . debug ( " No RN backup found, using VSS " , context: " AppScene " )
433+ return false
434+ }
435+
436+ // If VSS timestamp is unavailable, prefer VSS anyway (it might exist but timestamp fetch failed)
437+ // Only use RN if we're certain VSS doesn't exist
438+ guard let vss = vssTimestamp, vss > 0 else {
439+ Logger . warn (
440+ " VSS backup timestamp unavailable (nil or 0). Attempting VSS restore first. If it fails, will fall back to RN backup. " ,
441+ context: " AppScene "
442+ )
443+ // Try VSS first, it will handle errors internally
444+ return false
445+ }
446+
447+ guard let rn = rnTimestamp else {
448+ Logger . debug ( " No RN timestamp available, using VSS " , context: " AppScene " )
449+ return false
450+ }
451+
452+ let useRN = rn >= vss
453+ if useRN {
454+ Logger . info ( " RN backup is newer or equal (RN: \( rn) , VSS: \( vss) ), using RN backup " , context: " AppScene " )
455+ } else {
456+ Logger . info ( " VSS backup is newer (RN: \( rn) , VSS: \( vss) ), using VSS backup " , context: " AppScene " )
457+ }
458+ return useRN
426459 } ( )
427460
428461 if shouldRestoreRN {
429462 do {
430- try await MigrationsService . shared . restoreFromRNRemoteBackup ( mnemonic: mnemonic, passphrase: passphrase)
463+ try await migrations . restoreFromRNRemoteBackup ( mnemonic: mnemonic, passphrase: passphrase)
431464 } catch {
432465 Logger . error ( " RN remote backup restore failed: \( error) " , context: " AppScene " )
433466 // Fall back to VSS
467+ Logger . info ( " Falling back to VSS backup after RN restore failure " , context: " AppScene " )
434468 await BackupService . shared. performFullRestoreFromLatestBackup ( )
435469 }
436470 } else {
471+ // Always try VSS first
437472 await BackupService . shared. performFullRestoreFromLatestBackup ( )
473+
474+ // If VSS restore didn't work and RN backup exists, try RN as fallback
475+ // Note: We can't easily detect if VSS restore failed since it doesn't throw,
476+ // but this is a reasonable fallback strategy
477+ if hasRNBackup {
478+ Logger . debug ( " VSS restore attempted. If it failed, user may need to manually restore from RN backup. " , context: " AppScene " )
479+ }
438480 }
439481 }
440482
0 commit comments