Commit 48479856 authored by Oliver Wiese's avatar Oliver Wiese
Browse files

fix mailhandler: decrease number of fetched mails on first fetch

parent a4205a29
......@@ -395,7 +395,7 @@
}
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)
return
}
......@@ -405,7 +405,7 @@
}
// First fetch -> get flags
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.start{[unowned self] (err, msg, vanished) -> Void in
......@@ -566,27 +566,44 @@
}
private func initFolder(folderPath: String, completionCallback: @escaping ((MailServerConnectionError?) -> ())) {
let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue)
let uids = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
let toFetchIDs = MCOIndexSet()
let fetchOperation: MCOIMAPFetchMessagesOperation = self.IMAPSession!.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!)
guard let session = IMAPSession else {
self.errorhandling(error: .ConnectionError, originalCall: {self.initFolder(folderPath: folderPath, completionCallback: completionCallback)}, completionCallback: completionCallback)
return
}
session.folderStatusOperation(folderPath)?.start({error, status in
if let error = error {
let conerror = MailServerConnectionError.findErrorCode(error: error)
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 {
guard let status = status else {
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?) -> ())) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment