I have some code here:
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
let types: [FeedTimeline] = [.special, .pr, .pickup, .lastPost]
var operations: [Operation] = []
for type in types {
let operation = BlockOperation {
print("Working")
self.getArticles(of: type, page: 1, completion: { (articles) in
print("Fetched")
})
}
operation.completionBlock = {
print("Done")
}
queue.addOperation(operation)
After I ran the code above, I get the result below:
Working
Done
Working
Working
Done
Done
Working
Done
After a while I got 4 times of "Fetched". Why is that? How to make completionBlock
only run if the API request (getArticles) done.
What I want is: working
-> fetched
-> done
-> working
.... so on