iosswiftemailswiftuimailcore2

MailCore2 Crashes on real devices iOS


I am working on an email app using MailCore2 package. It works fine on simulator but it crashes on real devices. I am sharing my code as well, please look into and help me out as I am stuck in this.

private let session: MCOIMAPSession

func fetchEmailsFromServer(folder: String, completion: @escaping (Result<[EmailEntity], Error>) -> Void) {
    // Check if fetching is already in progress
    guard !isFetching else {
        completion(.failure(NSError(domain: "IMAPService", code: -1, userInfo: [NSLocalizedDescriptionKey: "Fetch already in progress"])))
        return
    }
    
    isFetching = true
    print("Fetching emails from folder: \(folder), isFetching: \(isFetching)")
    
    let nextUID = nextEmailUIDs[folder] ?? 1
    print("it comes here")
    
    guard nextUID > 0 else {
        print("Invalid nextUID: \(nextUID)")
        completion(.failure(NSError(domain: "IMAPService", code: -4, userInfo: [NSLocalizedDescriptionKey: "Invalid UID range"])))
        return
    }
    print("it comes here too \(nextUID)")
    

    let requestKind: MCOIMAPMessagesRequestKind = [.headers, .structure, .internalDate, .fullHeaders]
    guard let range = MCOIndexSet(range: MCORange(location: nextUID, length: 100)) else {return}
    print("request kind:  \(requestKind)")
    print("request range:  \(range)")


    // Use session directly since it is non-optional
    guard let fetchOperation = session.fetchMessagesOperation(withFolder: folder, requestKind: requestKind, uids: range) else {
        print("error fetching")
        return
    }
    print("request fetchOperation:  \(fetchOperation.debugDescription)")
    


    fetchOperation.start { error, messages, _ in
        
        
        self.isFetching = false
        print("Fetching emails completed for folder: \(folder), isFetching: \(self.isFetching)")
        print("it comes here")

        if let error = error {
            print("Fetch operation error: \(error.localizedDescription)")
            completion(.failure(error))
            return
        }
        
        guard let messages = messages else {
            print("No messages fetched.")
            completion(.failure(NSError(domain: "IMAPService", code: -2, userInfo: [NSLocalizedDescriptionKey: "Failed to parse messages"])))
            return
        }
        
        var emails: [EmailEntity] = []
        let group = DispatchGroup()
        
        for message in messages {
            group.enter()
            self.fetchEmailDetails(for: message, folder: folder) { result in
                if case .success(let email) = result {
                    emails.append(contentsOf: email.emails)
                }
                group.leave()
            }
        }
        
        group.notify(queue: .main) {
            if let lastMessage = messages.last {
                self.nextEmailUIDs[folder] = UInt64(lastMessage.uid + 1)
            }
            if emails.isEmpty {
                print("No emails parsed successfully.")
                completion(.failure(NSError(domain: "IMAPService", code: -6, userInfo: [NSLocalizedDescriptionKey: "No emails fetched"])))
            } else {
                completion(.success(emails))
            }
        }
    }
}

It works perfectly on simulator but it crashes on real devices.

What i am assuming is MCOIMAPSession is causing that crash on real device.
enter image description here


Solution

  • session.isVoIPEnabled = false
    

    Just use this line. It worked for me.