(19th) Use block for message transmission and block message transmission

Source: Internet
Author: User

(19th) Use block for message transmission and block message transmission

Messages are transmitted between two classes, generally through a proxy or block. It is troublesome to write the proxy and the block is relatively simple. However, the block must pay special attention to the memory leakage problem, note that there must be weak references between self and block. The following describes how to use block to transmit messages.

Let's first review the block structure:

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

The following uses XMPP user logon as an example to describe how to transmit block messages.

Network operations are performed in AppDelegate. In the logon interface View Controller class, you need to notify the logon interface of network operations when the logon button is clicked. Therefore, define a block in AppDelegate, when the logon controller clicks the logon button, the logon method is called because the logon method has a block parameter, therefore, you can use this block to implement services after successful logon. AppDelegate only needs to save the block when the logon method is called, call the block after the authorization is successful, and pass the parameter.

The specific implementation is as follows:

① Define a block and corresponding methods in AppDelegate, that is, the class for passing messages to another class:

typedef enum{        XMPPResultTypeSuccess,    XMPPResultTypeFailure    }XMPPResultType;typedef void (^XMPPResultBlock)(XMPPResultType type);
/*** User Logon */-(void) xmppUserLogin :( XMPPResultBlock) resultBlock;
② Implement the corresponding method in the View Controller and pass in a block:

It should be noted that because self is used in the block, the block will have a strong reference to self. This strong reference will make the View Controller unable to be destroyed after login, this causes memory leakage. Therefore, a weak pointer is used to call the Controller method.

AppDelegate * app = [UIApplication sharedApplication]. delegate; _ weak typeof (self) selfVc = self; // weak referenced controller [app xmppUserLogin: ^ (XMPPResultType type) {[selfVc handleResult: type]; // note that the block introduces a strong reference to the Controller and cannot release the current controller. Therefore, it should be weak reference}];
③ Save the passed block when implementing the method in AppDelegate:

@interface AppDelegate () <XMPPStreamDelegate>{    XMPPResultBlock _resultBlock;}
- (void)xmppUserLogin:(XMPPResultBlock)resultBlock{    [self connectToHost];    _resultBlock = resultBlock;}
④ Send the structure when the authorization is successful. Because the block has an enumeration parameter, you can pass in the value:

if (_resultBlock) {    _resultBlock(XMPPResultTypeSuccess);}
Because _ resultBlock stores the block imported by the View Controller, the block content defined by the View Controller is implemented during the call.








Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.