Ver código fonte

use dc_get_last_error() api and avoid potential races (#1386)

bjoern 3 anos atrás
pai
commit
cace4e6d66

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

@@ -94,7 +94,6 @@ public class DcContext {
 
     public var logger: Logger?
     var contextPointer: OpaquePointer?
-    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
 
@@ -113,6 +112,13 @@ public class DcContext {
         return Int(dc_get_id(contextPointer))
     }
 
+    public var lastErrorString: String {
+        guard let cString = dc_get_last_error(contextPointer) else { return "ErrNull" }
+        let swiftString = String(cString: cString)
+        dc_str_unref(cString)
+        return swiftString
+    }
+
     // viewType: one of DC_MSG_*
     public func newMessage(viewType: Int32) -> DcMsg {
         let messagePointer = dc_msg_new(contextPointer, viewType)

+ 0 - 1
DcCore/DcCore/DC/events.swift

@@ -27,7 +27,6 @@ public class DcEventHandler {
 
         if id >= DC_EVENT_ERROR && id <= 499 {
             let s = event.data2String
-            dcContext.lastErrorString = s
             dcContext.logger?.error("event: \(s)")
             return
         }

+ 1 - 2
deltachat-ios/Controller/QrPageController.swift

@@ -287,12 +287,11 @@ extension QrPageController: QrCodeReaderDelegate {
                                       preferredStyle: .alert)
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
         alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
-            self.dcContext.lastErrorString = nil
             let chatId = self.dcContext.joinSecurejoin(qrCode: code)
             if chatId != 0 {
                 self.showChat(chatId: chatId)
             } else {
-                self.showErrorAlert(error: self.dcContext.lastErrorString ?? "ErrJoinNoString")
+                self.showErrorAlert(error: self.dcContext.lastErrorString)
             }
         }))
         present(alert, animated: true, completion: nil)

+ 1 - 1
deltachat-ios/Controller/WelcomeViewController.swift

@@ -156,7 +156,7 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
     }
 
     private func accountCreationErrorAlert() {
-        let title = dcContext.lastErrorString ?? String.localized("error")
+        let title = dcContext.lastErrorString
         let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
         alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default))
         present(alert, animated: true)