"IOS" proxy pass-through value and block code transfer value

Source: Internet
Author: User

The main thread and child threads often need to pass data, between different classes and between different controllers.

And often need to listen to the completion of an action, and then to do the corresponding events. (The agent is a one-to-one relationship).


First, the agent value

A proxy is a design pattern. There are a lot of iOS. You can pass a value by customizing the proxy.

See my article http://blog.csdn.net/xn4545945/article/details/31036523

In iOS, there are 3 steps to be implemented by both the principal and the delegated party. (See code comment)

The following code: the UI needs to be updated after the simulation download is complete. Use proxies to pass values.


XNUpload.h

#import <FOUNDATION/FOUNDATION.H>//1. Defining the Proxy protocol @protocol xnuploaddelegate <nsobject>-(void) Downloadfinshed: (NSString *) param; @end @interface xnupload:nsobject//2. Define Proxy Properties @property (nonatomic, assign) ID < Xnuploaddelegate> delegate;-(void) download; @end

Xnupload.m

#import "XNUpload.h" @implementation xnupload/** *  after the simulation download is complete, you need to update the UI */-(void) Download {NSLog (@ "Downloading ...."); Sleep (3.0 ); NSLog (@ "Download done!"); /3. Updating the UI in the main thread requires passing parameters. Notifies the main thread that the download is complete. (Invoke Proxy method) [Self.delegate downloadfinshed:@ "Download complete!"];} @end

Xnviewcontroller.m

#import "XNViewController.h" #import "XNUpload.h" @interface Xnviewcontroller () <XNUploadDelegate>//1. Abide by the agent agreement @end@implementation xnviewcontroller-(void) viewdidload {[Super viewdidload]; Xnupload *upload = [[Xnupload alloc] init]; [UPLOAD download];   Downloaded the//2. Set proxy upload.delegate = self;} 3. Implement the Proxy method. This means that the download is complete.-(void) downloadfinshed: (NSString *) param {NSLog (@ "%@", param);} @end

second, block code transfer value

Block code is more convenient to pass values, and the code is more compact. Block code is more common in multithreading.

Use note:1.When not setBlockandIncomingNilwhen,will report the wild handsexc Badaccess. (Solve:Judge,if (xx) {..})2.Blockinexecution should go back to the main threadto updateUI,into the insideDispatchA bit.3.Apple recommends that all theBlock Code DefinitionAllDon'tplaced inheader Filein.(Solve:directly in the use of block codeto an anonymous.justOK)*Benefits:can beAvoidDefine your own block code name with Apple built -inConflicting names.
The code is as follows:XNUpload.h
#import <Foundation/Foundation.h> @interface xnupload:nsobject-(void) Download: (Void (^) (nsstring *param)) completion; @end

Xnupload.m

#import "XNUpload.h" @implementation xnupload/** * The  UI needs to be updated after the simulation download is complete. Use block code. */-(void) Download: (Void (^) (NSString * param)) Completion {//1. Define the block code in the parameter NSLog (@ "Downloading ...."); Sleep (3.0); NSLog (@ "Download done!"); /update UI in the main thread, you need to pass parameters. Notifies the main thread that the download is complete. (use block Code) if (completion) {//This allows the caller to not care about threading issues Dispatch_async (Dispatch_get_main_queue (), ^{    completion (@ "Download done!");  2. Execute block code});}} @end

Xnviewcontroller.m

#import "XNViewController.h" #import "XNUpload.h" @interface Xnviewcontroller () @end @implementation xnviewcontroller- (void) Viewdidload {[Super viewdidload]; Xnupload *upload = [[Xnupload alloc] init];//call method directly [upload download: ^ (nsstring *param) {    NSLog (@ "%@", [Nsthread cu Rrentthread]);    NSLog (@ "%@", param);}];} @end

Block code and Proxy comparison, easy to use a lot, very convenient.


Reprint Please specify source:http://blog.csdn.net/xn4545945


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.