What is a block? Blocks are objects that encapsulate a piece of code that can be executed at any time. A block can be a function argument or a function return value, and can itself take input arguments or return values. Like the proxy protocol, it can also be used for event communication.
What are the advantages of using blocks and a delegate to complete the delegate pattern? Using block to implement delegate mode, its advantage is that the callback block code block is defined inside the delegate object function, making the code more compact; Adaptation objects no longer need to implement a specific protocol, the code is more concise.
GCD and block with dispatch_async series methods, Void dispatch_async(dispatch_queue_t queue, dispatch_block_t block); void dispatch_async(dispatch_queue, dispatch_block_t block); Function: Commits a block to execute asynchronously in the specified queue without blocking the current thread
4. What is the difference between _block and __weak? __block can be used in both ARC and MRC mode, and can modify objects as well as primitive data types. __weak can only be used in ARC mode and can only modify objects, not basic data types. __block objects can be reassigned within blocks, __weak cannot.
5. Block does not allow modification of the value of an external variable, referring to the memory address of the pointer on the stack. All __block does is put the memory address of the “external variable” in the stack into the heap whenever it is observed that the variable is held by the block. In turn, you can modify the values of external variables inside the block.
· When a block references a local variable, the variable is encoded as a constant in the block block. · Local variables need to be decorated with __block to modify local objects within a block block · When referencing a local Objective-C object within a block, the object is retained. · Local variables decorated with __block will not be retained. · When an instance variable is referenced within a block, the instance object is retained.
7. When does a Block have a circular reference and how to solve it? · When an instance variable is referenced in a block, the instance object is retained. A declares that the block is called, B implements the block, and A is called in the implementation.
8. Is there any difference between the behavior and usage of blocks in ARC and traditional MRC? What should I pay attention to? (1) Nulling the block pointer is required before using the block. Not null direct use, once the pointer is null directly crash. _sucBlock = [callbackBlock copy]; //MRC: _sucBlock = [callbackBlock copy]; Non-copy blocks are recycled on the stack. (3) After the block call, the block pointer should be nulled. If it is MRC compilation environment, the block object should be released first. Object A calls block, object B implements block, and calls object A in its implementation, At this point, it is necessary to empty the object A after calling the block (4), and change self to __weak (5) before adding self or member variable to the block by the user. In A multi-threaded environment (when weakSelf in the block may be destructed), it is necessary to turn self into strong pointer. Avoid the self object being destructed when a critical step is run.
9. What should we pay attention to when using GCD and Block? The GCD cannot control the maximum number of concurrent tasks (the system can only decide how many child threads to open). Once a GCD request is sent, it cannot be cancelled directly
Block use caution: Prevent circular references. Stack blocks that use external variables in blocks are read-only copies are automatically released when out of scope. Heap blocks need to be released manually
10. What is the difference between the behavior and usage of blocks in ARC and MRC? The same as the essence of block, both function Pointers using __block can solve the problem of modifying external variables in the block
Different points different ways to resolve circular references using __block in MRC using __weak block in ARC what attributes are used and why?
In ARC, when a Block property is defined, the system automatically copies it, that is, to the heap. Blocks are created on the stack by default and destroyed when they are out of scope. Only using copy can create a heap block that is accessed outside scope
Yue four coming, looking for a job and interview and be on the agenda, in order to solve the friends right now, small make up specially for you recommend a good iOS communication platform, platform partners are very good in the iOS developer, we focus on the technology of sharing and skill of communication, we can discuss on the platform technology, exchange of learning. Welcome to join (want to enter the QQ group 828387065).