我有一个包含消息 ID 的数组。通过将这些 id 传递给一个方法,我可以使用批处理请求从谷歌服务器获取消息/电子邮件。它适用于数百个 id 传递,但问题是当有数千个 Id 时,它需要更多时间来获取消息。所以为了避免这种情况,我决定同时执行任务。我集成的谷歌客户端库是https://github.com/google/google-api-objectivec-client-for-rest/wiki。对于 messagesGet.query 参考是http://cocoadocs.org/docsets/GoogleAPIClientForREST/1.2.0/Classes/GTLRGmailQuery_UsersMessagesGet.html。对于批量请求参考https://developers.google.com/gmail/api/guides/batch。
var messageIDes = [AnyObject]() //143b3e9751e6b3fd,141c2194fb6e4d6d...thousands if Ids
func fetchingMessagesWithIDs()
{
let batchQuery = GTLRBatchQuery()
for eachID in messageIDes{
let query = GTLRGmailQuery_UsersMessagesGet.query(withUserId: "me", identifier: eachID as! String)
query.fields = "id,labelIds, payload(headers, parts )"
batchQuery.addQuery(query)
}
service.executeQuery(batchQuery) { (ticket:GTLRServiceTicket, object:Any?, error:Error?) in
if error == nil{
print("Object is \(String(describing: object))") //Contains Full mail Body
}
}
}
上述过程需要时间来避免我将数组分成大小为 100 的子数组并传递。
extension Array {
func divideBy(by size: Int) -> [[Element]] {
return stride(from: 0, to: self.count, by: size).map {
Array(self[$0..<Swift.min($0 + size, self.count)])
}
}
然后使用 dispatchQueue 并发执行我想获取消息但对结果不满意。
var msgIDBatches = [[String]]()
func DividingMsgIdsandPassing(){
self.msgIDBatches = self.messageIDes.divideBy(by: 100) as! [[String]] //array inside subarray of size 100 elements.
DispatchQueue.concurrentPerform(iterations: msgIDBatches.count) { (index) in
for subArray in msgIDBatches{
let batchQuery = GTLRBatchQuery()
for messageID in subArray {
let query = GTLRGmailQuery_UsersMessagesGet.query(withUserId: "me", identifier: messageID as! String)
query.fields = "id,labelIds, payload(headers, parts )"
batchQuery.addQuery(query)
}
service.executeQuery(batchQuery) { (ticket:GTLRServiceTicket, object:Any?, error:Error?) in
if error == nil{
print("Object is \(String(describing: object))") //Contains Full mail Body
}
}
}
}
}
我是否遵循正确的并发方法。任何其他获取逻辑都会有很大帮助。在这个答案中,有人告诉一个线程用于 ids fetch 和 4 个线程用于消息 fetch。我如何使用 4 个线程来实现它。