iOS Interview Questions
Multithreading/GCD:
Q. What care you will take when using sync execution in iOS?
Ans: If the order of completion of a task is important to you, you should consider using dispatch.sync.
It's important to know, apple warns that calling dispatch.sync on a current queue, will result in a deadlock. This will cause your app to crash, leaving the user with a bad user experience.
Q. How to avoid deadlocks in iOS ?
Ans:
1. Make sure you are dispatching the main queue asynchronously
2. You can also use NSRecursiveLocks to avoid deadlocks. NSRecursiveLock is a lock that can be acquired multiple times by the same thread without causing a deadlock. The locks and unlocks have to be balanced. When there's a balance between the locks and unlocks, the lock is released for other threads to acquire it.
3. Another option will be to use a Scheduler provided in the Combine Framework. Combine was introduced in WWDC 2019, and as Apple describes it, it handles asynchronous events by combining event processing operators. Dispatch queue conforms to Scheduler, which is why it's useful for solving a deadlock. Scheduler always executes serially and supports consolation. They don't support synchronous execution except for immediate Scheduler, and this helps to avoid deadlocks. They also don't allow tasks to depend on each other.
Q. What are the Challenges of Concurrent Programming?
Ans:
Developers should try to ensure that priority inversions don’t occur in the first place, so the system isn’t forced to attempt a resolution.
Protocol Oriented Programming (POP):
Q. What is not allowed in a protocol?
Ans: default values, computed properly, initializer, and method definitions are not allowed in the protocol.
Q. How to instantiate protocol in swift?
Ans: We can't instantiate protocol. Imagine protocols are adjectives. Movable says you can move it, Red says it has color = "red"... but they don't say what it is. You need a noun. A Red, Movable Car. You can instantiate a Car, even when low on details. You cannot instantiate a Red.
Objective-C:
Answer: Method Swizzling is a dynamic feature that can exchange the implementations of two methods at runtime. This means it can implement some complex functions conveniently, however, it can also be dangerous to use because its scope can change the class of the project and can cause fatal errors for the project if done wrong.
Here are some pitfalls of Method Swizzling. Pay attention to these when you use Method Swizzling in your projects.
• Method swizzling is not atomic
• Changes behavior of the un-owned code
• Possible naming conflicts
• Swizzling changes the method’s arguments
• The order of swizzles matters
• Difficult to understand (looks recursive)
• Difficult to debug
Sign up here with your email
ConversionConversion EmoticonEmoticon