浏览代码

close database on suspend, reopen on maybeStartIo()

B. Petersen 4 年之前
父节点
当前提交
9b4e120c5c
共有 2 个文件被更改,包括 17 次插入5 次删除
  1. 15 5
      DcCore/DcCore/DC/Wrapper.swift
  2. 2 0
      deltachat-ios/AppDelegate.swift

+ 15 - 5
DcCore/DcCore/DC/Wrapper.swift

@@ -12,6 +12,7 @@ public class DcContext {
     public var lastErrorString: String?
     public var lastWarningString: String = "" // temporary thing to get a grip on some weird errors
     public var maxConfigureProgress: Int = 0 // temporary thing to get a grip on some weird errors
+    private var dbFile: String?
 
     private init() {
     }
@@ -207,12 +208,18 @@ public class DcContext {
     }
 
     public func openDatabase(dbFile: String) {
-        var version = ""
-        if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
-            version += " " + appVersion
-        }
+        self.dbFile = dbFile
+        openDatabase()
+    }
 
-        contextPointer = dc_context_new("iOS" + version, dbFile, nil)
+    private func openDatabase() {
+        if let dbFile = dbFile {
+            var version = ""
+            if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
+                version += " " + appVersion
+            }
+            contextPointer = dc_context_new("iOS" + version, dbFile, nil)
+        }
     }
 
     public func closeDatabase() {
@@ -221,6 +228,9 @@ public class DcContext {
     }
 
     public func maybeStartIo() {
+        if contextPointer == nil {
+            openDatabase()
+        }
         if isConfigured() {
             dc_start_io(contextPointer)
         }

+ 2 - 0
deltachat-ios/AppDelegate.swift

@@ -202,6 +202,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
             } else if app.backgroundTimeRemaining < 10 {
                 logger.info("⬅️ few background time, \(app.backgroundTimeRemaining), stopping")
                 self.dcContext.stopIo()
+                self.dcContext.closeDatabase()
                 DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                     logger.info("⬅️ few background time, \(app.backgroundTimeRemaining), done")
                     self.unregisterBackgroundTask()
@@ -387,6 +388,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
             }
             if !self.appIsInForeground() {
                 self.dcContext.stopIo()
+                self.dcContext.closeDatabase()
             }
             completionHandler(.newData)
         }