3

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)
    }
})

2 Answers 2

1

to increase performance, anything that involves the UI should run on the main thread. So basically:

dispatch_async(dispatch_get_main_queue()) {
       //anything that involves UI
   }
Sign up to request clarification or add additional context in comments.

Comments

0

GCD provides queues for executing tasks. Queues can be of two types – concurrent or serial. In serial queue tasks execute one at time (in FIFO order), in concurrent queue several tasks at time.

To prevent user fetch data while one fetch task is running, it is needed to not submit fetch task to queue at this moment. No matter what kind of queue is it – concurrent or serial.

var array = [Int]()
var isFethingData = false

func fetchData() {
    if !isFethingData {
        isFethingData = true
        dispatch_async(queueForFetchData) {
            …
            response(objects: [object], error: NSError?) {
                for object in objects {
                    array.append(object.number)
                }

                dispatch_async(dispatch_get_main_queue()) {
                    makeCollectionView()
                }
                isFethingData = false
            }
    }
}

dispatch_async and dispatch_sync is functions which submits tasks to queue. The difference is that dispatch_async return immediately after task has been submitted, but dispatch_sync wait until task complete. For example:

print("\(NSDate()) qq")
dispatch_sync(queue) {
    // … some code which runs for 10 minutes.
    print("\(NSDate()) ee")
}
print("\(NSDate()) ww")
// 2016-08-18 16:02:00 qq
// 2016-08-18 16:12:00 ee
// 2016-08-18 16:12:00 ww


print("\(NSDate()) qq")
dispatch_async(queue) {
    // … some code which runs for 10 minutes.
    print("\(NSDate()) ee")
}
print("\(NSDate()) ww")
// 2016-08-18 16:02:00 qq
// 2016-08-18 16:02:00 ww
// 2016-08-18 16:12:00 ee

3 Comments

Opps. One more question. If I call dispatch_async(queue) {makeCollectionView()} twice, the second makeCollectionView will be run right after the first one or the second one will stop the first one immediately and run? if it will stop the first one immediately, is there any function I can put it as the first task to do in queue?
@PakHoCheung dispatch_async only submit tasks to queue. If you call it twice, then queue will have two tasks more. Main queue is serial queue, so tasks will execute if FIFO orded (in your update first will execute 0..1000 loop and second will 10000..20000). If you call dispatch_async on concurrent queue then tasks will run concurrently (for example dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {})) and you will see print from both loops at same time.
@PakHoCheung, if you interested in manage tasks (execute, stop, cancel), then you better to look at NSOperation and NSOperationQueue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.