#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