IOS nsoperation Package Notification Implementation interface update

Source: Internet
Author: User

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface myoperation:nsoperation


@end

#import "MYOperation.h"


@implementation Myoperation


-(void) Main

{

Whether it's arc or MRC, be sure to use Autorelease to release C-language objects

@autoreleasepool {

NSString *image=[self Stringeith];

Publish Notifications

[[Nsnotificationcenter Defaultcenter] postnotificationname:@ "myoperation" object:image];

[Self stringeith];

[Self downloadimage:@ "URL"];

}

}

Simulate download skip to main process impersonation send notification, notify Controller update UI

-(nsstring*) Stringeith

{

nsstring*image= @ "Big bad!!!!";

[[Nsoperationqueue mainqueue]addoperationwithblock:^{

[[Nsnotificationcenter Defaultcenter] postnotificationname:@ "myoperation" object:image];

}];

return image;

}




Download

-(UIImage *) Downloadimage: (nsstring*) str

{

Nsurl *url=[nsurl URLWITHSTRING:STR];

NSData *data=[nsdata Datawithcontentsofurl:url];

Uiimage*image=[uiimage Imagewithdata:data];

[[Nsoperationqueue mainqueue]addoperationwithblock:^{

[[Nsnotificationcenter Defaultcenter] postnotificationname:@ "myoperation" object:image];

}];

return image;

return image;

}

@end

Controller code

#import "ViewController.h"

#import "MYOperation.h"


@interface Viewcontroller ()


@property (Nonatomic,strong) nsoperationqueue*queue;


@property (Nonatomic,strong) nsstring*str;


@end


@implementation Viewcontroller


-(void) Viewdidload {

[Super Viewdidload];

The notification is sent in the child process, where the receiving notification execution method is also executed in the child process, the sending notification is in the main process, where the notification execution method is also executed in the main process, in general, the update UI is in the main process, so here we should default to let the Receive event method in the main process to execute \

Need to operate in an extended class

Registering Notification Events

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (updateimage:) name:@ "Myoperation" object: NIL];

}


-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

{

Myoperation *operation=[[myoperation Alloc]init];

Listening

[Self.queue addoperation:operation];

}

//

-(void) UpdateImage: (nsnotification*) notification

{

Self.str=notification.object;

NSLog (@ "%@,%@", Self.str,[nsthread CurrentThread]);

}



Registered notification events must be removed

-(void) dealloc

{

[[Nsnotificationcenter defaultcenter]removeobserver:self];

}



-(nsoperationqueue*) queue

{

if (!_queue) {

_queue=[[nsoperationqueue Alloc]init];

Set maximum number of threads

[_queue Setmaxconcurrentoperationcount:6];

}

return _queue;

}


@end

IOS nsoperation Package Notification Implementation interface update

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.