Use dispatch_async to parse an array at the same time in Swift

I am trying to analyze a photo at the same time using the background stream from GCD. Here is the code I wrote:

dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) {
    for (var i = 0; i < 8; i++)
    {
        let color = self.photoAnalyzer.analyzeColors(imageStrips[i])
        colorList.append(color)
    }
}

To clarify the names of variables, here are their descriptions:

photoAnalyzeris an instance of class I, called Analyzerthat contains all the image processing methods.

analyzeColorsis a method within a class Analyzerthat does most of the analysis and returns a string with the dominant color of the passed image

imageStripsis an array of UIImageparts of the original image

colorListis an array of strings that stores the return values ​​of the method analyzeColorfor each part of the image.

, for imageList. imageStrips , , .

. , , GitHub.

EDIT 8 .

dispatch_apply(8, imageQueue) { numStrips -> Void in
    let color = self.photoAnalyzer.analyzeColors(imageStrips[numStrips])
    colorList.append(color)
}

, 8, , .

+4
2

, , :

  • , , , , . , colorList.append(color) , , , . colorList, colorList[i] = color, . (, , .)

  • , colorList. analyzeColors , colorList, , , .

  • . , 2-4 , , , , concurrency , " " .

:

  • concurrency: , dispatch_apply, , for.

    colorList = [Int](count: 8, repeatedValue: 0)  // I don't know what type this `colorList` array is, so initialize this with whatever type makes sense for your app
    
    let queue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)
    
    let qos_attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0)
    let syncQueue = dispatch_queue_create("com.domain.app.sync", qos_attr)
    
    dispatch_apply(8, queue) { iteration in
        let color = self.photoAnalyzer.analyzeColors(imageStrips[iteration])
        dispatch_sync(syncQueue) {
            colorList[iteration] = color
            return
        }
    }
    
    // you can use `colorList` here
    

    : , dispatch_apply , . , ( ). , , - .

    , dispatch_apply WWDC 2011 Grand Central Dispatch .

  • - , dispatch_group_notify, , , .

    colorList = [Int](count: 8, repeatedValue: 0)  // I don't know what type this `colorList` array is, so initialize this with whatever type makes sense for your app
    
    let group = dispatch_group_create()
    let queue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)
    
    let qos_attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0)
    let syncQueue = dispatch_queue_create("com.domain.app.sync", qos_attr)
    
    for i in 0 ..< 8 {
        dispatch_group_async(group, queue) {
            let color = self.photoAnalyzer.analyzeColors(imageStrips[i])
            dispatch_sync(syncQueue) {
                colorList[i] = color
                return
            }
        }
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue()) {
        // use `colorList` here
    }
    
    // but not here (because the above code is running asynchronously)
    

    , , ( - ).

colorList. . ( ), ( ). , , . , , -. , , .

+4
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) {
    for (var i = 0; i < 8; i++)
    {
 dispatch_async(dispatch_get_main_queue(), ^(){
    //Add method, task you want perform on mainQueue
    //Control UIView, IBOutlet all here
        let color = self.photoAnalyzer.analyzeColors(imageStrips[i])
        colorList.append(color)
    });
    }
}
-1

All Articles