After reading so many posts about parallel and concurrent, I still confuse what is the proper way to fetch data. For example, in my project, I have a button for user to fetch data. My code is something like below.
var array = [Int]()
func fetchData() {
....
....
response(objects: [object], error: NSError?) {
for object in objects {
array.append(object.number) // assume object.number return an Int
}
// confuse here. Should I use async here because I am worry if the user
// click the fetchData button more than one time, the append and make
// function will be happened at the same time. Or, is there anything I
// made a wrong assumption? I guess I need a serial operation. Correct?
dispatch_async(dispatch_get_main_queue()) {
makeCollectionView() // using the data in array
}
}
}
UPDATE
Tried to run this code. 10000-19999 is run after 0-9999. It seems second async will not stop the first async to process its operation.
dispatch_async(dispatch_get_main_queue(), { () -> Void in
for i in 0 ..< 10000 {
print(i)
}
})
dispatch_async(dispatch_get_main_queue(), { () -> Void in
for i in 10000 ..< 20000 {
print(i)
}
})