GCD vs performSelectorInBackground/performSelectorOnMainThread
I am new in ios development. I have following questions:
- When we use GCD(dispatch_group_async, dispatch_async(dispatch_get_main_queue()…) and when we use performSelectorInBackground/performSelectorOnMainThread?
What’s the differences between those two.
- UICollectionView - Downloading images and effective reloading of data, like Instagram
- Fixed / Float view in UIScrollView with AutoLayout
- Triggering a UIButton's method when user drags finger into button's area?
- this class is not key value coding-compliant for the key XXXXXX
- Add an animation image per image inside a UIAlertController SWIFT
- What is the iOS API for scanning available wi-fi networks
I know when we use performSelectorInBackground, we create a new NSThread. But isn’t the same when we use dispatch_group_async? Because if we create more than one dispatch_group_async, it means we need to submit more than one blocks in queue. And those blocks might run on different queues. Therefore, when we create more than one dispatch_group_async, does it means we create a new thread? (because blocks may run on different queues) (I kind of confused about NSThread and block queue…..)
- How to create and save EKCalendar on ios 6
- How to delete all cookies of UIWebView?
- Class X is implemented in both <framework> and <application> one of the two will be used, which one is undefined
- AVAudioPlayer no longer working in Swift 2.0 / Xcode 7 beta
- How to add annotation in Mapview with image and label in IPhone?
- How to implement Task completion
2 Solutions Collect From Internet About “GCD vs performSelectorInBackground/performSelectorOnMainThread”
When to use
Never. Do not use this method. It spawns an unbounded number of threads. Even before GCD was available, this was a horrible method.
When to use
Meh… Never, but just because it’s inconvenient. There’s nothing deeply wrong with this method. It’s just not as useful as
The difference between GCD and the old
performSelector… methods (and
NSThread in general) is that GCD manages a thread pool for you. In general, you should avoid manual threading in Cocoa. Instead, use
NSOperationQueue or GCD (
dispatch methods). They provide a more useful queue abstraction rather than forcing you to manually manage threads.
Be sure to read over Apple’s Migrating Away from Threads for more info.
Actually after iOS 4.0 I can’t find any single reason to use performSelectorInBackground/onMainThread. If you need to do something in background, use GCD( or, better,
NSOperationQueue which is built on top of GCD since 4.0 and gives greater flexibility with little overhead), but be sure not to create retain cycles when using blocks.
- How do you pass objects between View Controllers in Objective-C?
- this certificate was signed by an unknown authority
- How to bold some words in my UITextView using NSMutableAttributedString?
- Converting NSString into uint8_t
- Strange behaviour on table view with core data
- Difference between internal and external testers in iTunes Testflight beta testing?
- Converting int to NSInteger
- How do I check if a string contains another string in Objective-C?
- iOS Tests/Specs TDD/BDD and Integration & Acceptance Testing
- How does String.Index work in Swift
- How to customize UISlider Value in Swift
- how to detect iphone's VPN connectivity?
- UISystemGateGestureRecognizer and delayed taps near bottom of screen
- Communication between Controllers in iOS
- Odd property declaration syntax containing angular brackets <>