Skip to content
Snippets Groups Projects
Commit 48479856 authored by Oliver Wiese's avatar Oliver Wiese
Browse files

fix mailhandler: decrease number of fetched mails on first fetch

parent a4205a29
No related branches found
No related tags found
No related merge requests found
...@@ -395,7 +395,7 @@ ...@@ -395,7 +395,7 @@
} }
private func loadMessagesFromServer(_ uids: MCOIndexSet, folderPath: String, maxLoad: Int = MailHandler.MAXMAILS, completionCallback: @escaping ((_ error: MailServerConnectionError?) -> ())) { private func loadMessagesFromServer(_ uids: MCOIndexSet, folderPath: String, maxLoad: Int = MailHandler.MAXMAILS, completionCallback: @escaping ((_ error: MailServerConnectionError?) -> ())) {
guard IMAPSession != nil else { guard let session = IMAPSession else {
completionCallback(MailServerConnectionError.NoData) completionCallback(MailServerConnectionError.NoData)
return return
} }
...@@ -405,7 +405,7 @@ ...@@ -405,7 +405,7 @@
} }
// First fetch -> get flags // First fetch -> get flags
let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue | MCOIMAPMessagesRequestKind.flags.rawValue) let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue | MCOIMAPMessagesRequestKind.flags.rawValue)
let fetchOperation: MCOIMAPFetchMessagesOperation = self.IMAPSession!.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids) let fetchOperation: MCOIMAPFetchMessagesOperation = session.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids)
fetchOperation.extraHeaders = MailHandler.extraHeaders fetchOperation.extraHeaders = MailHandler.extraHeaders
fetchOperation.start{[unowned self] (err, msg, vanished) -> Void in fetchOperation.start{[unowned self] (err, msg, vanished) -> Void in
...@@ -566,27 +566,44 @@ ...@@ -566,27 +566,44 @@
} }
private func initFolder(folderPath: String, completionCallback: @escaping ((MailServerConnectionError?) -> ())) { private func initFolder(folderPath: String, completionCallback: @escaping ((MailServerConnectionError?) -> ())) {
let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue) guard let session = IMAPSession else {
let uids = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX)) self.errorhandling(error: .ConnectionError, originalCall: {self.initFolder(folderPath: folderPath, completionCallback: completionCallback)}, completionCallback: completionCallback)
let toFetchIDs = MCOIndexSet() return
}
let fetchOperation: MCOIMAPFetchMessagesOperation = self.IMAPSession!.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids) session.folderStatusOperation(folderPath)?.start({error, status in
fetchOperation.start {[unowned self] (err, msg, vanished) -> Void in if let error = error {
guard err == nil else { let conerror = MailServerConnectionError.findErrorCode(error: error)
let conerror = MailServerConnectionError.findErrorCode(error: err!)
self.errorhandling(error: conerror, originalCall: {self.initFolder(folderPath: folderPath, completionCallback: completionCallback)}, completionCallback: completionCallback) self.errorhandling(error: conerror, originalCall: {self.initFolder(folderPath: folderPath, completionCallback: completionCallback)}, completionCallback: completionCallback)
return return
} }
if let msgs = msg { guard let status = status else {
FolderRecord.lastDate(folder: folderPath, date: Date())
for message in msgs {
toFetchIDs.add(UInt64(message.uid))
}
self.loadMessagesFromServer(toFetchIDs, folderPath: folderPath, maxLoad: 50, completionCallback: completionCallback)
} else {
completionCallback(nil) completionCallback(nil)
return
} }
}
let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue)
let min = max(status.uidNext - 1000, 1)
let uids = MCOIndexSet(range: MCORangeMake(UInt64(min), UInt64(status.uidNext)))
let toFetchIDs = MCOIndexSet()
let fetchOperation: MCOIMAPFetchMessagesOperation = session.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids)
fetchOperation.start {[unowned self] (err, msg, vanished) -> Void in
guard err == nil else {
let conerror = MailServerConnectionError.findErrorCode(error: err!)
self.errorhandling(error: conerror, originalCall: {self.initFolder(folderPath: folderPath, completionCallback: completionCallback)}, completionCallback: completionCallback)
return
}
if let msgs = msg {
FolderRecord.lastDate(folder: folderPath, date: Date())
for message in msgs {
toFetchIDs.add(UInt64(message.uid))
}
self.loadMessagesFromServer(toFetchIDs, folderPath: folderPath, maxLoad: 50, completionCallback: completionCallback)
} else {
completionCallback(nil)
}
}
})
} }
private func initInbox(completionCallback: @escaping ((MailServerConnectionError?) -> ())) { private func initInbox(completionCallback: @escaping ((MailServerConnectionError?) -> ())) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment