#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

iOS NSOperation 封装 通知实现界面更新的更多相关文章

  1. iOS NSOperation

    iOS NSOperation 一.简介 除了,NSThread和GCD实现多线程,配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOp ...

  2. iOS开发系列文章(持续更新……)

    iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...

  3. iOS push过去的时候界面不能完全退出

    iOS push过去的时候界面不能完全退出 解决方法:设置self.view.backgroundcolor 1. initWithFrame方法是什么?  initWithFrame方法用来初始化并 ...

  4. windows程序消息机制(Winform界面更新有关)

    windows程序消息机制(Winform界面更新有关) 转自:http://www.cnblogs.com/blosaa/archive/2013/05/31/3109586.html 1. Win ...

  5. iOS Notification – 远程通知

    本文讲解iOS的远程通知的基本使用,主要包括远程通知的类型,处理远程通知的场景,以及远程通知相关证书的配置等等. 一.APNs简介 APNs是苹果公司提供的远程通知的服务器,当App处于后台或者没有运 ...

  6. iOS学习——(转)iOS中关于通知的使用

    在移动端开打过程中,经常会用到通知和推送,例如有短信来了需要通知提示,手机横屏了需要通知提示,插上耳机了需要通知提示等等,我们可以根据这些通知采取对应的动作.iOS系统自身定义了很对通知,但是在开发过 ...

  7. Sprint 5 summary: UI 界面更新,Azure端部署和用户反馈分析 12/28/2015

    本次sprint主要完成的任务有对手机APP的UI界面的更新,同时对Azure客户端的部署进行了相应的学习和有关的程序设计.同时对于ALPHA release的用户反馈做出相应的分析以确定接下来工作的 ...

  8. iOS 15 Beta升级卡死在更新进程,无法启动怎么办?

    2021苹果全球开发者大会结束后,大批果粉迫不及待的尝试升级iOS 15测试版本,想第一时间体验新功能. 但是许多用户反馈升级一直卡死在"准备更新"."验证更新" ...

  9. 李洪强iOS开发之通知的使用

    李洪强iOS开发之通知的使用 01 - 在A中发送通知 02 - 在B中监听通知 03 - 在B中通知出发的方法 04 - 在B控制器viewDidLoad调用通知

随机推荐

  1. cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析

    首先,是个敲代码的,基本上都知道程序的入口是main函数,显然,就算在cocos2d-x框架中也一样 我们看看main函数做了什么 #include "main.h" #inclu ...

  2. AOJ DSL_2_C Range Search (kD Tree)

    Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...

  3. ubuntu16.04装MatConvNet

    按matconvnet官网上的步骤来,编译代码的时候会发现编译失败. 参考这条issues 以下是我的解决方案: I use ubuntu16.04 with x64 architecture. I ...

  4. Asynchronous fs.stat.isDirectory()

    function showFile() { for(var i = 0; i< files.length; i++){ var itemFile = files[i]; fs.stat(__di ...

  5. css基于绝对定位的垂直水平居中技术

    翻译:http://blog.csdn.net/freshlover/article/details/11579669 原文:http://coding.smashingmagazine.com/20 ...

  6. js函数传参

    函数传参:重用代码,首先保持html代码相对一致,把核心主程序用函数包起来,把每组不同的值找出来,通过传参的方式减少代码的使用 下面代码是我早期练习的,大家随便看看就好 <!DOCTYPE ht ...

  7. AE套宗获取内环

    主要根据IPolygon4的get_InteriorRingBag方法. IGeometry geo = null; IRing exring = null; (geo as IPolygon2).Q ...

  8. Eclipse DDT

    http://www.eclipse.org/downloads/ https://github.com/DDT-IDE/DDT/blob/latest/documentation/UserGuide ...

  9. 浅谈 PHP 与手机 APP 开发(API 接口开发) -- 转载

    转载自:http://www.thinkphp.cn/topic/5023.html 这个帖子写给不太了解PHP与API开发的人 一.先简单回答两个问题: 1.PHP 可以开发客户端? 答:不可以,因 ...

  10. maven项目常见问题

    问题1:Maven项目,右键-update project后报错如下的解决办法: 1).DescriptionResourcePathLocationType Java compiler level ...