Calling a method on the main thread?

IosObjective CIphoneMultithreadingMethods

Ios Problem Overview


First of all I am writing code for iphone. I need to be able to call a method on the main thread without using performSelectorOnMainThread. The reason that I don't want to use performSelectorOnMainThread is that it causes problem when I am trying to create a mock for unit testing.

[self performSelectorOnMainThread:@Selector(doSomething) withObject:nil];

The problem is that my mock knows how to call doSomething but it doesn't know how to call performSelectorOnMainThread.

So Any solution?

Ios Solutions


Solution 1 - Ios

Objective-C

dispatch_async(dispatch_get_main_queue(), ^{
    [self doSomething];
});

Swift

DispatchQueue.main.async {
    self.doSomething()
}

Legacy Swift

dispatch_async(dispatch_get_main_queue()) {
    self.doSomething()
}

Solution 2 - Ios

There's a saying in software that adding a layer of indirection will fix almost anything.

Have the doSomething method be an indirection shell that only does a performSelectorOnMainThread to call the really_doSomething method to do the actual Something work. Or, if you don't want to change your doSomething method, have the mock test unit call a doSomething_redirect_shell method to do something similar.

Solution 3 - Ios

Here is a better way to do this in Swift:

runThisInMainThread { () -> Void in
    // Run your code
    self.doSomething()
}

func runThisInMainThread(block: dispatch_block_t) {
    dispatch_async(dispatch_get_main_queue(), block)
}

Its included as a standard function in my repo, check it out: https://github.com/goktugyil/EZSwiftExtensions

Solution 4 - Ios

And now in Swift 3:

DispatchQueue.main.async{
   self.doSomething()
}

Solution 5 - Ios

// Draw Line
    func drawPath(from polyStr: String){
        DispatchQueue.main.async {
            let path = GMSPath(fromEncodedPath: polyStr)
            let polyline = GMSPolyline(path: path)
            polyline.strokeWidth = 3.0
            polyline.strokeColor = #colorLiteral(red: 0.05098039216, green: 0.5764705882, blue: 0.2784313725, alpha: 1)
            polyline.map = self.mapVu // Google MapView
        }
        
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaryaxtView Question on Stackoverflow
Solution 1 - IosaryaxtView Answer on Stackoverflow
Solution 2 - Ioshotpaw2View Answer on Stackoverflow
Solution 3 - IosEsqarrouthView Answer on Stackoverflow
Solution 4 - IosRomOneView Answer on Stackoverflow
Solution 5 - IosMuhammad ZeeshanView Answer on Stackoverflow