• iOS NSOperation 封装 通知实现界面更新


    #import <Foundation/Foundation.h>

    #import <UIKit/UIKit.h>

    @interface MYOperation : NSOperation


    @end

    #import "MYOperation.h"


    @implementation MYOperation


    -(void)main

    {

        //不管是ARC还是MRC一定要用autorelease来释放c语言对象

        @autoreleasepool {

            //NSString *image=[self stringEith];

            //发布通知

            //[[NSNotificationCenter defaultCenter] postNotificationName:@"MYOperation" object:image];

            [self stringEith];

            

            [self downLoadImage:@"URL"];

        }

        

        

    }

    //模拟下载 跳到主进程模拟发送通知,通知控制器更新UI

    -(NSString*)stringEith

    {

        NSString*image= @"大坏蛋!!!!";

        [[NSOperationQueue mainQueue]addOperationWithBlock:^{

            [[NSNotificationCenter defaultCenter] postNotificationName:@"MYOperation" object:image];

        }];

        return image;

    }




    //下载

    -(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

    //控制器代码

    #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];

        

        //发送通知的是在子进程,这里接收通知执行方法也是在子进程中执行,,发送通知是在主进程,这里接收通知执行方法也是在主进程中执行,一般情况下更新UI是在主进程中进行,所以这里我们应该默认让接收事件方法在主进程中执行

        需要在拓展类中进行操作

        //注册通知事件

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateImage:) name:@"MYOperation" object:nil];

        

        

        

    }


    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        MYOperation *operation=[[MYOperation alloc]init];

        //监听

        [self.queue addOperation:operation];

        

        

        

    }

    //

    -(void)updateImage:(NSNotification*)notification

    {

        self.str=notification.object;

        NSLog(@"%@ ,%@",self.str,[NSThread currentThread]);

    }



    //注册的通知事件一定要移除

    -(void)dealloc

    {

        [[NSNotificationCenter defaultCenter]removeObserver:self];

    }



    -(NSOperationQueue*)queue

    {

        if (!_queue) {

            _queue=[[NSOperationQueue alloc]init];

            //设置最大线程数

            [_queue setMaxConcurrentOperationCount:6];

        }

        return _queue;

    }


    @end

  • 相关阅读:
    数据类型说明
    python基础之编码的定义和种类
    python基础之pycharm安装
    python基础之windows环境下安装python2和python3
    数据类型之字符串(string)
    数据类型之布尔值(bool/boolen)
    pycharm自动生成头部代码
    数据类型之整型(int)
    python基础篇之数据类型
    jquery事件绑定
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4713721.html
Copyright © 2020-2023  润新知