瀏覽代碼

relplace semaphore by usleep as using a semaphore sometimes suspends the app

B. Petersen 3 年之前
父節點
當前提交
6833460352
共有 3 個文件被更改,包括 10 次插入6 次删除
  1. 1 1
      DcCore/DcCore/DC/Wrapper.swift
  2. 2 2
      DcCore/DcCore/DC/events.swift
  3. 7 3
      deltachat-ios/AppDelegate.swift

+ 1 - 1
DcCore/DcCore/DC/Wrapper.swift

@@ -9,7 +9,7 @@ public class DcAccounts {
     let applicationGroupIdentifier = "group.chat.delta.ios"
     var accountsPointer: OpaquePointer?
     public var logger: Logger?
-    public var fetchSemaphore: DispatchSemaphore?
+    public var doExitPerformFetch: Bool = true
 
     public init() {
     }

+ 2 - 2
DcCore/DcCore/DC/events.swift

@@ -210,8 +210,8 @@ public class DcEventHandler {
             }
 
         case DC_EVENT_CONNECTIVITY_CHANGED:
-            if let sem = dcAccounts.fetchSemaphore, dcAccounts.isAllWorkDone() {
-                sem.signal()
+            if !dcAccounts.doExitPerformFetch && dcAccounts.isAllWorkDone() {
+                dcAccounts.doExitPerformFetch = true
             }
             if dcContext.id != dcAccounts.getSelected().id {
                 return

+ 7 - 3
deltachat-ios/AppDelegate.swift

@@ -473,6 +473,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
             guard let self = self else { completionHandler(.failed); return }
 
             // we're in background, run IO for a little time
+            self.dcAccounts.doExitPerformFetch = false
             self.dcAccounts.startIo()
             self.dcAccounts.maybeNetwork()
             self.pushToDebugArray("2")
@@ -481,9 +482,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
             // create a new semaphore to make sure the received DC_CONNECTIVITY_CONNECTED really belongs to maybeNetwork() from above
             // (maybeNetwork() sets connectivity to DC_CONNECTIVITY_CONNECTING, when fetch is done, we're back at DC_CONNECTIVITY_CONNECTED)
-            self.dcAccounts.fetchSemaphore = DispatchSemaphore(value: 0)
-            _ = self.dcAccounts.fetchSemaphore?.wait(timeout: .now() + 20)
-            self.dcAccounts.fetchSemaphore = nil
+            for _ in 0..<20 {
+                usleep(500_000)
+                if self.dcAccounts.doExitPerformFetch {
+                    break
+                }
+            }
 
             // TOCHECK: it seems, we are not always reaching this point in code,
             // semaphore?.wait() does not always exit after the given timeout and the app gets suspended -