Proactive Database Initialization - Database ready from SDK startup
Showing
2 changed files
with
103 additions
and
4 deletions
... | @@ -1236,6 +1236,87 @@ public func requestRaw(_ endpoint: Endpoint) async throws -> [String: Any] { | ... | @@ -1236,6 +1236,87 @@ public func requestRaw(_ endpoint: Endpoint) async throws -> [String: Any] { |
1236 | **Result**: ✅ **Universal fix for all context response parsing issues** - getCampaigns and all other context endpoints now work correctly | 1236 | **Result**: ✅ **Universal fix for all context response parsing issues** - getCampaigns and all other context endpoints now work correctly |
1237 | 1237 | ||
1238 | --- | 1238 | --- |
1239 | + | ||
1240 | +#### **Issue #9: Database Initialization Timing (PROACTIVE FIX)** | ||
1241 | +**File**: `SwiftWarplyFramework/SwiftWarplyFramework/Core/WarplySDK.swift` | ||
1242 | +**Problem**: Database was not initialized during SDK setup, causing authentication checks to fail | ||
1243 | +**Impact**: ⚠️ **MEDIUM** - Authentication logic bypassed, users don't get personalized experience | ||
1244 | + | ||
1245 | +**Root Cause**: Database initialization was happening lazily (on first access) rather than proactively during SDK initialization. | ||
1246 | + | ||
1247 | +**Timing Issue**: | ||
1248 | +1. `WarplySDK.initialize()` → Device registration only | ||
1249 | +2. `getCampaigns()` → Authentication check → ❌ Database not ready | ||
1250 | +3. Database initializes lazily → ✅ Too late | ||
1251 | + | ||
1252 | +**Solution**: **Proactive Database Initialization** | ||
1253 | +```swift | ||
1254 | +public func initialize(callback: ((Bool) -> Void)? = nil) { | ||
1255 | + // ... existing validation ... | ||
1256 | + | ||
1257 | + // Initialize database and register device during initialization | ||
1258 | + Task { | ||
1259 | + do { | ||
1260 | + // 1. Initialize database FIRST to ensure it's ready for authentication checks | ||
1261 | + try await initializeDatabaseProactively() | ||
1262 | + | ||
1263 | + // 2. Then perform device registration | ||
1264 | + try await performDeviceRegistration() | ||
1265 | + | ||
1266 | + await MainActor.run { | ||
1267 | + print("✅ [WarplySDK] SDK initialization completed successfully") | ||
1268 | + callback?(true) | ||
1269 | + } | ||
1270 | + } catch { | ||
1271 | + // ... error handling ... | ||
1272 | + } | ||
1273 | + } | ||
1274 | +} | ||
1275 | + | ||
1276 | +/// Initialize database proactively during SDK initialization | ||
1277 | +private func initializeDatabaseProactively() async throws { | ||
1278 | + print("🗄️ [WarplySDK] Initializing database proactively during SDK setup...") | ||
1279 | + | ||
1280 | + do { | ||
1281 | + // Initialize the database with default configuration | ||
1282 | + try await DatabaseManager.shared.initializeDatabase() | ||
1283 | + print("✅ [WarplySDK] Database initialized successfully during SDK setup") | ||
1284 | + } catch { | ||
1285 | + print("❌ [WarplySDK] Database initialization failed during SDK setup: \(error)") | ||
1286 | + throw error | ||
1287 | + } | ||
1288 | +} | ||
1289 | +``` | ||
1290 | + | ||
1291 | +**Expected Results After Fix**: | ||
1292 | + | ||
1293 | +**SDK Initialization logs should show**: | ||
1294 | +``` | ||
1295 | +🏭 [WarplyConfiguration] Production configuration loaded | ||
1296 | +✅ [WarplySDK] Stored appUuid in UserDefaults: f83dfde1145e4c2da69793abb2f579af | ||
1297 | +🗄️ [WarplySDK] Initializing database proactively during SDK setup... | ||
1298 | +✅ [WarplySDK] Database initialized successfully during SDK setup | ||
1299 | +🔄 [WarplySDK] Performing automatic device registration... | ||
1300 | +[... registration ...] | ||
1301 | +✅ [WarplySDK] SDK initialization completed successfully | ||
1302 | +``` | ||
1303 | + | ||
1304 | +**getCampaigns logs should show**: | ||
1305 | +``` | ||
1306 | +🔍 [DatabaseManager] Retrieving TokenModel synchronously from database | ||
1307 | +✅ [DatabaseManager] Database ready - checking for tokens | ||
1308 | +ℹ️ [WarplySDK] User not authenticated - returning all basic campaigns without coupon filtering | ||
1309 | +``` | ||
1310 | + | ||
1311 | +**Key Benefits**: | ||
1312 | +1. **Proper Initialization Order** - Database ready before any authentication checks | ||
1313 | +2. **Better User Experience** - Authenticated users get personalized campaigns + coupon filtering | ||
1314 | +3. **Consistent Behavior** - No more timing-dependent authentication failures | ||
1315 | +4. **Future-Proof** - All future authentication-dependent features will work correctly | ||
1316 | + | ||
1317 | +**Result**: ✅ **Database ready from SDK initialization** - authentication logic works properly, users get appropriate campaign experience | ||
1318 | + | ||
1319 | +--- | ||
1239 | ### **🔧 Additional Safety Fixes - June 27, 2025 (Session 2)** | 1320 | ### **🔧 Additional Safety Fixes - June 27, 2025 (Session 2)** |
1240 | 1321 | ||
1241 | After the initial legacy credential removal, a comprehensive search revealed **3 additional problematic checks** that could still cause issues: | 1322 | After the initial legacy credential removal, a comprehensive search revealed **3 additional problematic checks** that could still cause issues: | ... | ... |
... | @@ -390,9 +390,13 @@ public final class WarplySDK { | ... | @@ -390,9 +390,13 @@ public final class WarplySDK { |
390 | UserDefaults.standard.set(storage.appUuid, forKey: "appUuidUD") | 390 | UserDefaults.standard.set(storage.appUuid, forKey: "appUuidUD") |
391 | print("✅ [WarplySDK] Stored appUuid in UserDefaults: \(storage.appUuid)") | 391 | print("✅ [WarplySDK] Stored appUuid in UserDefaults: \(storage.appUuid)") |
392 | 392 | ||
393 | - // Automatically register device during initialization | 393 | + // Initialize database and register device during initialization |
394 | Task { | 394 | Task { |
395 | do { | 395 | do { |
396 | + // 1. Initialize database FIRST to ensure it's ready for authentication checks | ||
397 | + try await initializeDatabaseProactively() | ||
398 | + | ||
399 | + // 2. Then perform device registration | ||
396 | try await performDeviceRegistration() | 400 | try await performDeviceRegistration() |
397 | 401 | ||
398 | await MainActor.run { | 402 | await MainActor.run { |
... | @@ -401,9 +405,9 @@ public final class WarplySDK { | ... | @@ -401,9 +405,9 @@ public final class WarplySDK { |
401 | } | 405 | } |
402 | } catch { | 406 | } catch { |
403 | await MainActor.run { | 407 | await MainActor.run { |
404 | - print("⚠️ [WarplySDK] SDK initialization completed with registration warning: \(error.localizedDescription)") | 408 | + print("⚠️ [WarplySDK] SDK initialization completed with warning: \(error.localizedDescription)") |
405 | - // Still consider initialization successful even if registration fails | 409 | + // Still consider initialization successful even if some steps fail |
406 | - // The SDK can function without registration, but some features may be limited | 410 | + // The SDK can function with reduced functionality |
407 | callback?(true) | 411 | callback?(true) |
408 | } | 412 | } |
409 | } | 413 | } |
... | @@ -435,6 +439,20 @@ public final class WarplySDK { | ... | @@ -435,6 +439,20 @@ public final class WarplySDK { |
435 | return result | 439 | return result |
436 | } | 440 | } |
437 | 441 | ||
442 | + /// Initialize database proactively during SDK initialization | ||
443 | + private func initializeDatabaseProactively() async throws { | ||
444 | + print("🗄️ [WarplySDK] Initializing database proactively during SDK setup...") | ||
445 | + | ||
446 | + do { | ||
447 | + // Initialize the database with default configuration | ||
448 | + try await DatabaseManager.shared.initializeDatabase() | ||
449 | + print("✅ [WarplySDK] Database initialized successfully during SDK setup") | ||
450 | + } catch { | ||
451 | + print("❌ [WarplySDK] Database initialization failed during SDK setup: \(error)") | ||
452 | + throw error | ||
453 | + } | ||
454 | + } | ||
455 | + | ||
438 | /// Perform device registration during initialization | 456 | /// Perform device registration during initialization |
439 | private func performDeviceRegistration() async throws { | 457 | private func performDeviceRegistration() async throws { |
440 | // Legacy credentials are deprecated - always proceed with registration if needed | 458 | // Legacy credentials are deprecated - always proceed with registration if needed | ... | ... |
-
Please register or login to post a comment