Преглед изворни кода

do not set DcContext.shared implicitly on access

B. Petersen пре 4 година
родитељ
комит
3a6d5f84ab

+ 4 - 8
DcCore/DcCore/DC/Wrapper.swift

@@ -4,9 +4,10 @@ import AVFoundation
 
 public class DcContext {
 
-    /// TODO: THIS global instance should be replaced in the future, for example for a multi-account scenario,
-    /// where we want to have more than one DcContext.
-    static let dcContext: DcContext = DcContext()
+    /// DcContact.shared can be set to get a global pointer to the current context.
+    /// It is not set implicitly. In general, injection of DcContext is preferred.
+    public static var shared: DcContext = DcContext()
+
     public var logger: Logger?
     var contextPointer: OpaquePointer?
     public var lastErrorString: String?
@@ -22,11 +23,6 @@ public class DcContext {
         contextPointer = nil
     }
 
-    /// Injection of DcContext is preferred over the usage of the shared variable
-    public static var shared: DcContext {
-        return .dcContext
-    }
-
     public func getMessageIds(chatId: Int, count: Int, from: Int?) -> [Int] {
 		let cMessageIds = getChatMessages(chatId: chatId)
 

+ 4 - 3
DcShare/Controller/ShareViewController.swift

@@ -29,7 +29,7 @@ class ShareViewController: SLComposeServiceViewController {
     }
 
     let logger = SimpleLogger()
-    let dcContext = DcContext.shared
+    let dcContext = DcContext()
     var selectedChatId: Int?
     var selectedChat: DcChat?
     let dbHelper = DatabaseHelper()
@@ -60,9 +60,10 @@ class ShareViewController: SLComposeServiceViewController {
     }
 
     override func presentationAnimationDidFinish() {
-        if dbHelper.currentDatabaseLocation == dbHelper.sharedDbFile {
+        if dbHelper.updateSucceeded() {
             dcContext.logger = self.logger
-            dcContext.openDatabase(dbFile: dbHelper.sharedDbFile)
+            dcContext.openDatabase(dbFile: AccountManager().getSelectedAccount())
+            DcContext.shared = dcContext
             isAccountConfigured = dcContext.isConfigured()
             if isAccountConfigured {
                 selectedChatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))

+ 2 - 1
deltachat-ios/AppDelegate.swift

@@ -10,7 +10,7 @@ let logger = SwiftyBeaver.self
 
 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
-    private let dcContext = DcContext.shared
+    private let dcContext = DcContext()
     var appCoordinator: AppCoordinator!
     var relayHelper: RelayHelper!
     var locationManager: LocationManager!
@@ -156,6 +156,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         let databaseLocation = AccountManager().getSelectedAccount()
         logger.info("open: \(databaseLocation)")
         dcContext.openDatabase(dbFile: databaseLocation)
+        DcContext.shared = dcContext
     }
 
     func closeDatabase() {