(69) using block for message delivery

Source: Internet
Author: User

Message passing between two classes, generally through the proxy or block, the agent is more cumbersome to write, block is simpler, but the block needs to pay special attention to the memory leak problem, note that the self and the block to be a weak reference, the following describes the use of block for message delivery methods.

Let's review the structure of block:

return type (^block name) (parameter 1 type, parameter 2 type ...)

The following is an example of the user login for Xmpp to illustrate the block message delivery method.

The network operation is performed in Appdelegate, and the Login interface view Controller class, because the login button needs to let the network operation class notify the login interface when authorization completes, so define a block in appdelegate and use it as the parameter of the login method. When the login controller clicked on the login button, the login method must be called, because the login method has a parameter is block, so you can use this block to achieve the successful login business. The appdelegate only need to save the block when the login method is called, and then call block after the authorization succeeds, passing the parameters.

The specific implementation is as follows:

① defines a block and the corresponding method within the Appdelegate, which is the class that needs to pass the message to another class:

typedef enum{        xmppresulttypesuccess,    xmppresulttypefailure    }xmppresulttype;typedef void (^ Xmppresultblock) (xmppresulttype type);
/** *  User login */-(void) Xmppuserlogin: (Xmppresultblock) Resultblock;
② implements the appropriate method in the view controller, passing in a block:

It is important to note that because self is used in a block, it causes a strong reference to self from the block, which causes the view controller to fail to destroy after the login, causing a memory leak, so a weak pointer is used to invoke the controller's method .

Appdelegate *app = [UIApplication sharedapplication].delegate;        __weak typeof (self) SELFVC = self; Weak reference controller        [app xmppuserlogin:^ (xmppresulttype type) {        [SELFVC handleresult:type];//Notice that block introduces a strong reference to the controller, The current controller cannot be freed and therefore should be weakly referenced    }];
③ Save the incoming block when implementing the method in Appdelegate:

@interface appdelegate () <xmppstreamdelegate>{    xmppresultblock _resultblock;}
-(void) Xmppuserlogin: (xmppresultblock) resultblock{    [self connecttohost];    _resultblock = Resultblock;}
④ The structure is sent when authorization succeeds, because the block has an enumeration parameter, the value can be passed in:

if (_resultblock) {    _resultblock (xmppresulttypesuccess);}
Because _resultblock saves the block that the view controller has passed in, it implements the contents of the block defined by the view controller at the time of invocation.








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(69) using block for message delivery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.