My Github address
Notes on data Structures and Algorithms
Notes for geek Time iOS Developer Class
IOS large factory interview high frequency algorithm summary
Summary of iOS interview materials
GCD
1. Synchronous/asynchronous & Serial/concurrent
A, synchronous serial
B. Cause of deadlock
- Deadlocks are loops of waiting caused by queues, not threads.
- First of all in
The main thread
performThe home side column
In theviewDidLoad
Function. - When performing the
block
When, because issynchronous
So you need to hold onThe main thread
In theThe home side column
In actionviewDidLoad
Function, and so onThe home side column
In theblock
Internal code after executionThe main thread
In theThe home side column
theviewDidLoad
Function. - So there it is
viewDidLoad
Waiting for theblock
In the case. block
To execute, the code must wait for other functions in the queue to finish executing, i.eFirst in first out
.- So there it is
block
Waiting for theviewDidLoad
In the case. - The final two functions
Wait for each other
Come out causeA deadlock
.
- First of all in
- That’s fine
- First of all in
The main thread
performThe home side column
In theviewDidLoad
Function. - When performing the
block
When, because issynchronous
So you need to hold onThe main thread
In theThe home side column
In actionviewDidLoad
Function, and so onThe home side column
In theblock
Internal code after executionThe main thread
In theThe home side column
theviewDidLoad
Function. - So there it is
viewDidLoad
Waiting for theblock
In the case. block
The internal code will be inserialQueue
The queue is taken out becauseserialQueue
In theblock
It’s at the top of the list, soblock
Will be removed immediately and inThe main thread
In the execution.- stay
block
If the command is executed, the command will be executedviewDidLoad
Remaining code.
- First of all in
- Because it is
Concurrent queue
, so the tasks in the runqueue are executed together and do not need to wait for the previous task to complete before executing the next one, so noA deadlock
. - if
global_queue
Switch toSerial queues
, will produceA deadlock
.
C. asynchronous serial
- To perform the first
viewDidLoad
And then to performblock
Inside the code.
- Because child threads are not enabled by default
runloop
.performSelector
Unable to execute.
2, dispatch_barrier_async
A, how to use GCD to achieve multiple read single write?
- Use it while reading
dispatch_sync
Because of the use ofSynchronous queue
The return value can be performed after the assignment is complete.
3, dispatch_group
A. Use GCD to implement concurrent tasks A, B, and C, and execute task D after completion.
NSOperation
1. Advantages and characteristics
- Adding task dependencies
- Task execution status control
- Maximum concurrency
2. Task status
isReady
isExecuting
isFinished
isCancelled
3. Task state control
- If I just rewrite
main
Method, the underlying control changes the task execution completion status, and the task exit. - If I rewrite it
start
Method, self control task status.
A. How does the system remove oneisFinished=YES
theNSOperation
?
- KVO
NSThread
1. NSThread startup process
Multithreading and Locking
- os_unfair_lock
- OSSpinLock
- Loop waiting for a query without releasing the current resource
- For lightweight data access, simple int +1/-1 operations
- dispatch_semaphore
- pthread_mutex
- dispatch_queue(DISPATCH_QUEUE_SERIAL)
- NSLock
- NSCondition
- pthread_mutex(recursive)
- NSRecursiveLock
- NSConditionLock
- @synchronized
- This is usually used when creating singletons to ensure that the creation is unique in a multi-threaded environment.
1. NSLock deadlock problem
- You can use
NSRecursiveLock recursive locking
To solve the problem.
Summary of multi-threaded interview questions
- How to use
GCD
Multi-read single-write? - What are the characteristics of several multithreading technologies provided by iOS system?
AFNetworking
&SDWebImage
useNSOperation
Because thread state can be controlled.
- How does the system remove one
isFinished=YES
theNSOperation
? - What locks have you used? How do you use it?
Day twenty: Multithreading is a security hazard