Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] - App Crash on 'fetchMessagesByUIDOperationWithFolder:requestKind:uids: #2000

Open
karam28 opened this issue Mar 16, 2024 · 2 comments
Labels

Comments

@karam28
Copy link

karam28 commented Mar 16, 2024

Summary
I'm utilizing MailCore2 via CocoaPods. After copying the Objective-C code, my application crashes upon running. The crash is associated with a deprecated method mentioned in the warning from Xcode: 'fetchMessagesByUIDOperationWithFolder:requestKind:uids:'.

Platform(s)

<iOS>

Happens on Mail Server

<Yahoo>

Piece of code

    MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
    [session setHostname:@"imap.mail.yahoo.com"];
    [session setPort:993];
    [session setUsername:@"[email protected]"];
    [session setPassword:@"xxxx"];
    [session setConnectionType:MCOConnectionTypeTLS];

    MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindHeaders;
    NSString *folder = @"INBOX";
    MCOIndexSet *uids = [MCOIndexSet indexSetWithRange:MCORangeMake(1, UINT64_MAX)];

    MCOIMAPFetchMessagesOperation *fetchOperation = [session fetchMessagesByUIDOperationWithFolder:folder requestKind:requestKind uids:uids];

    [fetchOperation start:^(NSError * error, NSArray * fetchedMessages, MCOIndexSet * vanishedMessages) {
        //We've finished downloading the messages!

        //Let's check if there was an error:
        if(error) {
            NSLog(@"Error downloading message headers:%@", error);
        }

        //And, let's print out the messages...
        NSLog(@"The post man delivereth:%@", fetchedMessages);
    }];

Actual outcome
it should not crash. Please provide the updated method and update that in your documentation also.

Connection Logs



**Expected outcome**

**Link to sample code on GitHub reproducing the issue (a full Xcode project):**
@karam28 karam28 added the bug label Mar 16, 2024
@karam28
Copy link
Author

karam28 commented Mar 16, 2024

I find the updated method I think but it's crashing and no reason mention. here is the crash Thread 5: EXC_BREAKPOINT (code=1, subcode=0x20fc0f374)

MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
    session.hostname = @"imap.mail.yahoo.com";
    session.port = 993;
    session.username = @"[email protected]";
    session.password = @"ybykuvrpfxqswxqo ";
    session.connectionType = MCOConnectionTypeTLS;

    MCOIndexSet *uidSet = [MCOIndexSet indexSetWithRange:MCORangeMake(1,UINT64_MAX)];
  MCOIMAPFetchMessagesOperation * op = [session fetchMessagesOperationWithFolder:@"INBOX"
                                                                       requestKind:MCOIMAPMessagesRequestKindHeaders | MCOIMAPMessagesRequestKindStructure
                                                                              uids:uidSet];
    [op start:^(NSError * error, NSArray * messages, MCOIndexSet * vanishedMessages) {
       for(MCOIMAPMessage * msg in messages) {
         NSLog(@" mails-- %lu: %@", [msg uid], [msg header]);
       }
    }];

@Be-Maps
Copy link

Be-Maps commented Aug 29, 2024

this works for me :
extension MCOIMAPFetchMessagesOperation {

func fetchMessages() async throws -> [Any] {
    typealias PostContinuation = CheckedContinuation <[Any], Error>
    return try await withCheckedThrowingContinuation { [weak self] (continuation:PostContinuation) in
        guard let strongSelf = self else {
            logd("strongSelf Error downloading message UIDs")
            let mHeaders:[Any] = []
            continuation.resume(returning: mHeaders)
            return
        }
        
        strongSelf.start { (error: Error?, headers: [Any]?, indexSet: MCOIndexSet?) -> Void in
            if let error = error {
                logd("Error downloading message UIDs: \(error)")
                let mHeaders:[Any] = []
                continuation.resume(returning: mHeaders)
            } else {
                logd("Successful IMAP messages headers download")
                var mHeaders:[Any] = []
                if (headers != nil) {mHeaders = headers!}
                continuation.resume(returning: mHeaders)
            }
        }
    }
}

}

// ---------- and later:

    //Creating properties
    do {
        let session =  MCOIMAPSession()
        
        if (config.getIMAPSSL().equals("SSL")){
            session.hostname = config.getIMAPServer()
            session.username = config.getUserName()
            session.password = config.getPassword()
            session.port = UInt32.parseUInt32(config.getIMAPport())
            session.allowsFolderConcurrentAccessEnabled = true
            session.connectionType              = MCOConnectionType.TLS
            session.authType                    = MCOAuthType.saslLogin
            session.isCheckCertificateEnabled   = false
            session.isVoIPEnabled               = false
            session.maximumConnections          = 2
            session.timeout                     = App.connectionTimeout
        } else {
            session.hostname = config.getIMAPServer()
            session.username = config.getUserName()
            session.password = config.getPassword()
            session.port = UInt32.parseUInt32(config.getIMAPport())
            session.allowsFolderConcurrentAccessEnabled = true
            session.connectionType              = MCOConnectionType.startTLS
            session.authType                    = MCOAuthType.saslPlain
            session.isCheckCertificateEnabled   = false
            session.isVoIPEnabled               = false
            session.maximumConnections          = 2
            session.timeout                     = App.connectionTimeout
        }

        Log.d(TAG, "MoveAndCheck_ServerAndDeviceRegistrationEmails")
        var folderInbox:MCOIMAPFolderInfo = MCOIMAPFolderInfo()
        if let folderInfoOperation = session.folderInfoOperation("INBOX") {
            folderInbox = try await folderInfoOperation.start()
            logd("Successful IMAP folderInfoOperation")
        }

        let msgCount = folderInbox.messageCount
        if (msgCount<1) {
            Log.d(TAG, "MoveToServers: No messages to move")
            mResponse = "Move OK"
            return mResponse
        }

        let maxUID = folderInbox.uidNext
        var minUID:UInt32 = 0
        if (maxUID>200) {minUID = maxUID - 200}
        else {minUID = 1}
        
        let uids = MCOIndexSet(range: MCORange(location: UInt64(minUID), length: 201))
        
        var mMessages:[MCOIMAPMessage]? = []
        let requestKind: MCOIMAPMessagesRequestKind = [.headers, .headerSubject, .flags]
        
        if let fetchMessagesOperation = session.fetchMessagesOperation(withFolder: "INBOX", requestKind: requestKind , uids: uids) {
            let anyMessages = try await fetchMessagesOperation.fetchMessages()
            if (anyMessages.isEmpty) {
                mResponse = "Move OK"
                return mResponse
            } else {
                logd("Successful IMAP messages headers download")
                mMessages = anyMessages as? [MCOIMAPMessage]
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants