0

How can i executes all the above taks at one time for improving speed.

self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[0]) 

self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[1]) 

self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[2]) 

self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[3])
  • 1
    Post the contents of `getFullMail` method – mag_zbc Apr 11 '18 at 12:34
  • 1
    Are you fetching data from the network? If so, it is unlikely that anything you do in your app will improve performance – Paulw11 Apr 11 '18 at 12:39
  • I suggest printing the thread ID inside your `for` loop to verify whether you're getting concurrent execution. What to adjust may depend on the answer. – Phillip Mills Apr 11 '18 at 13:35

2 Answers2

1

What you're after is the class function on DispatchQueue concurrentPerform

For example:

DispatchQueue.concurrentPerform(iterations: msgIDBatches.count) { (index) in
    self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[index])    
}

You will obviously need to be careful around calling back on the main queue if you're updating UI and also ensure that passingMsgIdsTofetchMsgss is thread-safe. It is also worth checking using time profiler that this is where the performance bottle-neck actually is.

Another option is OperationQueue, you can add all of your fetches to a queue and concurrently perform them.

Tim
  • 8,932
  • 4
  • 43
  • 64
0

Swift 4.1. First create a concurrent Queue

private let concurrentPhotoQueue = DispatchQueue(label: "App_Name", attributes: .concurrent)

Now dispatch your work to Concurrent Queue

concurrentPhotoQueue.async(flags: .barrier) { [weak self] in
            // 1
            guard let weakSelf = self else {
                return
            }
            // 2 Perform your task here
            weakSelf.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[0])
            weakSelf.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[1])
            weakSelf.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[2])
            weakSelf.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[3])
            // 3
            DispatchQueue.main.async { [weak self] in
                // Update your UI here
            }
        }
Gurjinder Singh
  • 9,221
  • 1
  • 66
  • 58