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 inThe main threadperformThe home side columnIn theviewDidLoadFunction.
    • When performing theblockWhen, because issynchronousSo you need to hold onThe main threadIn theThe home side columnIn actionviewDidLoadFunction, and so onThe home side columnIn theblockInternal code after executionThe main threadIn theThe home side columntheviewDidLoadFunction.
    • So there it isviewDidLoadWaiting for theblockIn the case.
    • blockTo execute, the code must wait for other functions in the queue to finish executing, i.eFirst in first out.
    • So there it isblockWaiting for theviewDidLoadIn the case.
    • The final two functionsWait for each otherCome out causeA deadlock.

  • That’s fine
    • First of all inThe main threadperformThe home side columnIn theviewDidLoadFunction.
    • When performing theblockWhen, because issynchronousSo you need to hold onThe main threadIn theThe home side columnIn actionviewDidLoadFunction, and so onThe home side columnIn theblockInternal code after executionThe main threadIn theThe home side columntheviewDidLoadFunction.
    • So there it isviewDidLoadWaiting for theblockIn the case.
    • blockThe internal code will be inserialQueueThe queue is taken out becauseserialQueueIn theblockIt’s at the top of the list, soblockWill be removed immediately and inThe main threadIn the execution.
    • stayblockIf the command is executed, the command will be executedviewDidLoadRemaining code.

  • Because it isConcurrent 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.
  • ifglobal_queueSwitch toSerial queues, will produceA deadlock.

C. asynchronous serial

  • To perform the firstviewDidLoadAnd then to performblockInside the code.

  • Because child threads are not enabled by defaultrunloop.performSelectorUnable to execute.

2, dispatch_barrier_async

A, how to use GCD to achieve multiple read single write?

  • Use it while readingdispatch_syncBecause of the use ofSynchronous queueThe 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 rewritemainMethod, the underlying control changes the task execution completion status, and the task exit.
  • If I rewrite itstartMethod, self control task status.

A. How does the system remove oneisFinished=YEStheNSOperation?

  • 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 useNSRecursiveLock recursive lockingTo solve the problem.

Summary of multi-threaded interview questions

  • How to useGCDMulti-read single-write?
  • What are the characteristics of several multithreading technologies provided by iOS system?
    • AFNetworking & SDWebImageuseNSOperationBecause thread state can be controlled.
  • How does the system remove oneisFinished=YEStheNSOperation?
  • What locks have you used? How do you use it?

Day twenty: Multithreading is a security hazard