diff --git a/apps/ios/Shared/Model/SimpleXAPI.swift b/apps/ios/Shared/Model/SimpleXAPI.swift index e67585453d..72aa164135 100644 --- a/apps/ios/Shared/Model/SimpleXAPI.swift +++ b/apps/ios/Shared/Model/SimpleXAPI.swift @@ -117,7 +117,6 @@ func chatRecvMsg() async -> ChatResponse? { } func apiGetActiveUser() throws -> User? { - let _ = getChatCtrl() let r = chatSendCmdSync(.showActiveUser) switch r { case let .activeUser(user): return user @@ -725,13 +724,12 @@ func apiUpdateGroup(_ groupId: Int64, _ groupProfile: GroupProfile) async throws func initializeChat(start: Bool, dbKey: String? = nil) throws { logger.debug("initializeChat") let m = ChatModel.shared - (m.chatDbEncrypted, m.chatDbStatus) = migrateChatDatabase(dbKey) + (m.chatDbEncrypted, m.chatDbStatus) = chatMigrateInit(dbKey) if m.chatDbStatus != .ok { return } // If we migrated successfully means previous re-encryption process on database level finished successfully too if encryptionStartedDefault.get() { encryptionStartedDefault.set(false) } - let _ = getChatCtrl(dbKey) try apiSetFilesFolder(filesFolder: getAppFilesDirectory().path) try apiSetIncognito(incognito: incognitoGroupDefault.get()) m.chatInitialized = true diff --git a/apps/ios/Shared/Views/Database/DatabaseErrorView.swift b/apps/ios/Shared/Views/Database/DatabaseErrorView.swift index 32b0d87353..5c6451e50b 100644 --- a/apps/ios/Shared/Views/Database/DatabaseErrorView.swift +++ b/apps/ios/Shared/Views/Database/DatabaseErrorView.swift @@ -91,6 +91,7 @@ struct DatabaseErrorView: View { private func runChat() { do { + resetChatCtrl() try initializeChat(start: m.v3DBMigration.startChat, dbKey: dbKey) if let s = m.chatDbStatus { status = s diff --git a/apps/ios/SimpleX NSE/NotificationService.swift b/apps/ios/SimpleX NSE/NotificationService.swift index 0e8ebd852c..1684a4b9e7 100644 --- a/apps/ios/SimpleX NSE/NotificationService.swift +++ b/apps/ios/SimpleX NSE/NotificationService.swift @@ -158,7 +158,7 @@ var chatStarted = false func startChat() -> DBMigrationResult? { hs_init(0, nil) if chatStarted { return .ok } - let (_, dbStatus) = migrateChatDatabase() + let (_, dbStatus) = chatMigrateInit() if dbStatus != .ok { return dbStatus } if let user = apiGetActiveUser() { logger.debug("active user \(String(describing: user))") @@ -230,7 +230,6 @@ func receivedMsgNtf(_ res: ChatResponse) async -> (String, UNMutableNotification } func apiGetActiveUser() -> User? { - let _ = getChatCtrl() let r = sendSimpleXCmd(.showActiveUser) logger.debug("apiGetActiveUser sendSimpleXCmd responce: \(String(describing: r))") switch r { diff --git a/apps/ios/SimpleXChat/API.swift b/apps/ios/SimpleXChat/API.swift index 92d2a5e8aa..5737101110 100644 --- a/apps/ios/SimpleXChat/API.swift +++ b/apps/ios/SimpleXChat/API.swift @@ -14,19 +14,12 @@ private var migrationResult: (Bool, DBMigrationResult)? public func getChatCtrl(_ useKey: String? = nil) -> chat_ctrl { if let controller = chatController { return controller } - let dbPath = getAppDatabasePath().path - let dbKey = useKey ?? getDatabaseKey() ?? "" - logger.debug("getChatCtrl DB path: \(dbPath)") - var cPath = dbPath.cString(using: .utf8)! - var cKey = dbKey.cString(using: .utf8)! - chatController = chat_init_key(&cPath, &cKey) - logger.debug("getChatCtrl: chat_init_key") - return chatController! + fatalError("chat controller not initialized") } -public func migrateChatDatabase(_ useKey: String? = nil) -> (Bool, DBMigrationResult) { +public func chatMigrateInit(_ useKey: String? = nil) -> (Bool, DBMigrationResult) { if let res = migrationResult { return res } - logger.debug("migrateChatDatabase \(storeDBPassphraseGroupDefault.get())") + logger.debug("chatMigrateInit \(storeDBPassphraseGroupDefault.get())") let dbPath = getAppDatabasePath().path var dbKey = "" let useKeychain = storeDBPassphraseGroupDefault.get() @@ -40,11 +33,12 @@ public func migrateChatDatabase(_ useKey: String? = nil) -> (Bool, DBMigrationRe dbKey = key } } - logger.debug("migrateChatDatabase DB path: \(dbPath)") -// logger.debug("migrateChatDatabase DB key: \(dbKey)") + logger.debug("chatMigrateInit DB path: \(dbPath)") + logger.debug("chatMigrateInit DB key: \(dbKey)") var cPath = dbPath.cString(using: .utf8)! var cKey = dbKey.cString(using: .utf8)! - let cjson = chat_migrate_db(&cPath, &cKey)! + // the last parameter of chat_migrate_init is used to return the pointer to chat controller + let cjson = chat_migrate_init(&cPath, &cKey, &chatController)! let dbRes = dbMigrationResult(fromCString(cjson)) let encrypted = dbKey != "" let keychainErr = dbRes == .ok && useKeychain && encrypted && !setDatabaseKey(dbKey) diff --git a/apps/ios/SimpleXChat/SimpleX.h b/apps/ios/SimpleXChat/SimpleX.h index 1b2f5d821b..edd9d46bbc 100644 --- a/apps/ios/SimpleXChat/SimpleX.h +++ b/apps/ios/SimpleXChat/SimpleX.h @@ -15,8 +15,8 @@ extern void hs_init(int argc, char **argv[]); typedef void* chat_ctrl; -extern char *chat_migrate_db(char *path, char *key); -extern chat_ctrl chat_init_key(char *path, char *key); +// the last parameter is used to return the pointer to chat controller +extern char *chat_migrate_init(char *path, char *key, chat_ctrl *ctrl); extern char *chat_send_cmd(chat_ctrl ctl, char *cmd); extern char *chat_recv_msg(chat_ctrl ctl); extern char *chat_recv_msg_wait(chat_ctrl ctl, int wait);