Browse Source

Merge pull request #1418 from deltachat/speedup-get-msgs

streamline getChatMsgs()
cyBerta 3 năm trước cách đây
mục cha
commit
3263d89a90

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

@@ -142,23 +142,13 @@ public class DcContext {
         return Int(dc_send_videochat_invitation(contextPointer, UInt32(chatId)))
     }
 
-    // TODO: remove count and from parameters if we don't use it
-    public func getMessageIds(chatId: Int, count: Int? = nil, from: Int? = nil) -> [Int] {
+    public func getChatMsgs(chatId: Int) -> [Int] {
         let start = CFAbsoluteTimeGetCurrent()
-        let cMessageIds = getChatMessages(chatId: chatId)
+        let cMessageIds = dc_get_chat_msgs(contextPointer, UInt32(chatId), UInt32(DC_GCM_ADDDAYMARKER), 0)
         let diff = CFAbsoluteTimeGetCurrent() - start
-        logger?.info("⏰ getMessageIds: \(diff) s")
-
-        let ids: [Int]
-        if let from = from {
-            // skip last part
-            ids = DcUtils.copyAndFreeArrayWithOffset(inputArray: cMessageIds, len: count, skipEnd: from)
-        } else if let count = count {
-            // skip first part
-            ids = DcUtils.copyAndFreeArrayWithLen(inputArray: cMessageIds, len: count)
-        } else {
-            ids = DcUtils.copyAndFreeArray(inputArray: cMessageIds)
-        }
+        logger?.info("⏰ getChatMsgs: \(diff) s")
+
+        let ids = DcUtils.copyAndFreeArray(inputArray: cMessageIds)
         return ids
     }
 
@@ -381,10 +371,6 @@ public class DcContext {
             dc_markseen_msgs(contextPointer, ptr.baseAddress, Int32(ptr.count))
         }
     }
-
-    private func getChatMessages(chatId: Int) -> OpaquePointer {
-        return dc_get_chat_msgs(contextPointer, UInt32(chatId), UInt32(DC_GCM_ADDDAYMARKER), 0)
-    }
     
     public func getMsgInfo(msgId: Int) -> String {
         if let cString = dc_get_msg_info(self.contextPointer, UInt32(msgId)) {

+ 0 - 37
DcCore/DcCore/Helper/DcUtils.swift

@@ -58,43 +58,6 @@ public struct DcUtils {
         return acc
     }
 
-    static func copyAndFreeArrayWithLen(inputArray: OpaquePointer?, len: Int = 0) -> [Int] {
-        var acc: [Int] = []
-        let arrayLen = dc_array_get_cnt(inputArray)
-        let start = max(0, arrayLen - len)
-        for i in start ..< arrayLen {
-            let e = dc_array_get_id(inputArray, i)
-            acc.append(Int(e))
-        }
-        dc_array_unref(inputArray)
-
-        return acc
-    }
-
-    static func copyAndFreeArrayWithOffset(inputArray: OpaquePointer?, len: Int? = 0, from: Int = 0, skipEnd: Int = 0) -> [Int] {
-        let lenArray = dc_array_get_cnt(inputArray)
-        let length = len ?? lenArray
-        if lenArray <= skipEnd || lenArray == 0 {
-            dc_array_unref(inputArray)
-            return []
-        }
-
-        let start = lenArray - 1 - skipEnd
-        let end = max(0, start - length)
-        let finalLen = start - end + (length > 0 ? 0 : 1)
-        var acc: [Int] = [Int](repeating: 0, count: finalLen)
-
-        for i in stride(from: start, to: end, by: -1) {
-            let index = finalLen - (start - i) - 1
-            acc[index] = Int(dc_array_get_id(inputArray, i))
-        }
-
-        dc_array_unref(inputArray)
-        print("got: \(from) \(length) \(lenArray) - \(acc)")
-
-        return acc
-    }
-
     public static func getMimeTypeForPath(path: String) -> String {
         let url = NSURL(fileURLWithPath: path)
         let pathExtension = url.pathExtension

+ 3 - 7
deltachat-ios/Chat/ChatViewController.swift

@@ -331,7 +331,7 @@ class ChatViewController: UITableViewController {
                 else { return }
                 
                 if appDelegate.appIsInForeground() {
-                    self.messageIds = self.getMessageIds()
+                    self.messageIds = self.dcContext.getChatMsgs(chatId: self.chatId)
                     self.reloadData()
                 } else {
                     logger.warning("startTimer() must not be executed in background")
@@ -911,7 +911,7 @@ class ChatViewController: UITableViewController {
 
     @objc
     private func refreshMessages() {
-        self.messageIds = self.getMessageIds()
+        self.messageIds = dcContext.getChatMsgs(chatId: chatId)
         let wasLastSectionVisible = self.isLastRowVisible()
         self.reloadData()
         if wasLastSectionVisible {
@@ -933,7 +933,7 @@ class ChatViewController: UITableViewController {
     private func loadMessages() {
 
         // update message ids
-        var msgIds = self.getMessageIds()
+        var msgIds = dcContext.getChatMsgs(chatId: chatId)
         let freshMsgsCount = self.dcContext.getUnreadMessages(chatId: self.chatId)
         if freshMsgsCount > 0 && msgIds.count >= freshMsgsCount {
             let index = msgIds.count - freshMsgsCount
@@ -1038,10 +1038,6 @@ class ChatViewController: UITableViewController {
             emptyStateView.isHidden = true
         }
     }
-    
-    private func getMessageIds() -> [Int] {
-        return dcContext.getMessageIds(chatId: chatId)
-    }
 
     @objc private func saveDraft() {
         draft.save(context: dcContext)