- 浏览: 89292 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
ma_029619:
的确,打不开
用 UITableView 进行多选的代码例子 -
janedoneway:
不错,学习了,同时问一下,动画结束后同时隐藏图片,要怎么做?你 ...
[iOS]iPhoto的删除动画(转) -
cn23snyga:
博主的POST方法2.1中,怎么是 setHTTPMethod ...
[iOS]网络编程专题:发送Http请求(POST GET)的方法 -
bear1122ccc:
文件坏了。
用 UITableView 进行多选的代码例子
在app store中的很多应用程序非常的笨重,他们有好的界面,但操作性很差,比如说当程序从网上或本地载入数据的时候,界面被冻结了,用户只能等程序完全载入数据之后才能进行操作。
当打开一个应用程序时,iphone会产生一个包含main方法的线程,所用程序中的界面都是运行在这个线程之中的(table views, tab bars, alerts…),有时候我们会用数据填充这些view,现在问 题是如何有效的载入数据,并且用户还能自如的操作程序。方法是启动新的线程,专门用于数据的下载,而主线程不会因为下载数据被阻塞。
不管使用任何编程语言,在实现多线程时都是一件很麻烦的事情。更糟糕的是,一旦出错,这种错误通常相当糟糕。然而,幸运的是apple从os x10.5在这方面做了很多的改进,NSThread的引入,使得开发多线程应用程序容易多了。除此之外,它们还引入了两个全新的类,NSOperation和NSOperationQueue。
接下来我们通过一个实例来剖析如何使用这两个类实现多线程。这里指示展示这两个类的基本用法,当然这不是使用他们的唯一办法。
如果你熟悉java或者它的别的变种语言的话 ,你会发现NSOperation对象很像java.lang.Runnable接口,就像java.lang.Runnable接口那样,NSOperation类也被设计为可扩展的,而且只有一个需要重写的方法。它就是-(void)main。使用NSOperation的最简单的方式就是把一个NSOperation对象加入到NSOperationQueue队列中,一旦这个对象被加入到队列,队列就开始处理这个对象,直到这个对象的所有操作完成。然后它被队列释放。
下面的例子中,使用一个获取网页,并对其解析程NSXMLDocument,最后将解析得到的NSXMLDocument返回给主线程。
PageLoadOperation.h@interface PageLoadOperation : NSOperation {
NSURL *targetURL;}
@property(retain) NSURL *targetURL;
- (id)initWithURL:(NSURL*)url;@end
PageLoadOperation.m
#import "PageLoadOperation.h"#import "AppDelegate.h"@implementation PageLoadOperation@synthesize targetURL;- (id)initWithURL:(NSURL*)url;{
if (![super init]) return nil;
[self setTargetURL:url];
return self;}- (void)dealloc {
[targetURL release], targetURL = nil;
[super dealloc];
}
- (void)main
{
NSString *webpageString = [[[NSString alloc]
initWithContentsOfURL:[self targetURL]] autorelease];
NSError *error = nil;
NSXMLDocument *document = [[NSXMLDocument alloc]
initWithXMLString:webpageString
options:NSXMLDocumentTidyHTML error:&error];
if (!document) {
NSLog(@"%s Error loading document (%@): %@",
_cmd, [[self targetURL] absoluteString], error);
return;
}
[[AppDelegate shared]
performSelectorOnMainThread:@selector(pageLoaded:)
withObject:document waitUntilDone:YES];
[document release];
}
@end
正如我们所看到的那样,这个类相当的简单,在它的init方法中接受一个url并保存起来,当main函数被调用的时候,它使用这个保存的url创建一个字符串,并将这个字符串传递给NSXMLDocumentinit方法。如果加载的xml数据没有出错,数据会被传递给AppDelegate,它处于主线程中。到此,这个线程的任务就完成了。在主线程中注销操作队列的时候,会将这个NSOperation对象释放。
AppDelegate.h
@interface AppDelegate : NSObject {
NSOperationQueue *queue;
}+ (id)shared;- (void)pageLoaded:(NSXMLDocument*)document;@endAppDelegate.m #import "AppDelegate.h"#import "PageLoadOperation.h"@implementation AppDelegate
static AppDelegate *shared;
static NSArray *urlArray;
- (id)init
{
if (shared)
{
[self autorelease];
return shared;
}
if (![super init]) return nil; NSMutableArray *array = [[NSMutableArray alloc] init];[array addObject:@"http://www.google.com"];[array addObject:@"http://www.apple.com"];[array addObject:@"http://www.yahoo.com"];[array addObject:@"http://www.zarrastudios.com"];[array addObject:@"http://www.macosxhints.com"];urlArray = array; queue = [[NSOperationQueue alloc] init];shared = self;return self;
}
• (void)applicationDidFinishLaunching:
(NSNotification *)aNotification
{
for (NSString *urlString in urlArray)
{
NSURL *url =
[NSURL URLWithString:urlString]; PageLoadOperation *plo =
[[PageLoadOperation alloc] initWithURL:url];
[queue addOperation:plo];
[plo release];
}
}
- (void)dealloc
{
[queue release], queue = nil;
[super dealloc];
}
+ (id)shared;
{
if (!shared) {
[[AppDelegate alloc] init];
}
return shared;
}
- (void)pageLoaded:(NSXMLDocument*)document;
{
NSLog(@"%s Do something with the XMLDocument: %@",
_cmd, document);
}
@end
NSOperationQueue的并行控制(NSOperationQueue Concurrency)
在上面这个简单的例子中,我们很难看出这些操作是并行运行的,然而,如果你你的操作花费的时间远远比这里的要长,你将会发现,队列是同时执行这些操作的。幸运的是,如果你想要为队列限制同时只能运行几个操作,你可以使用NSOperationQueue的setMaxConcurrentOperationCount:方法。例如,[queue setMaxConcurrentOperationCount:2];
当打开一个应用程序时,iphone会产生一个包含main方法的线程,所用程序中的界面都是运行在这个线程之中的(table views, tab bars, alerts…),有时候我们会用数据填充这些view,现在问 题是如何有效的载入数据,并且用户还能自如的操作程序。方法是启动新的线程,专门用于数据的下载,而主线程不会因为下载数据被阻塞。
不管使用任何编程语言,在实现多线程时都是一件很麻烦的事情。更糟糕的是,一旦出错,这种错误通常相当糟糕。然而,幸运的是apple从os x10.5在这方面做了很多的改进,NSThread的引入,使得开发多线程应用程序容易多了。除此之外,它们还引入了两个全新的类,NSOperation和NSOperationQueue。
接下来我们通过一个实例来剖析如何使用这两个类实现多线程。这里指示展示这两个类的基本用法,当然这不是使用他们的唯一办法。
如果你熟悉java或者它的别的变种语言的话 ,你会发现NSOperation对象很像java.lang.Runnable接口,就像java.lang.Runnable接口那样,NSOperation类也被设计为可扩展的,而且只有一个需要重写的方法。它就是-(void)main。使用NSOperation的最简单的方式就是把一个NSOperation对象加入到NSOperationQueue队列中,一旦这个对象被加入到队列,队列就开始处理这个对象,直到这个对象的所有操作完成。然后它被队列释放。
下面的例子中,使用一个获取网页,并对其解析程NSXMLDocument,最后将解析得到的NSXMLDocument返回给主线程。
PageLoadOperation.h@interface PageLoadOperation : NSOperation {
NSURL *targetURL;}
@property(retain) NSURL *targetURL;
- (id)initWithURL:(NSURL*)url;@end
PageLoadOperation.m
#import "PageLoadOperation.h"#import "AppDelegate.h"@implementation PageLoadOperation@synthesize targetURL;- (id)initWithURL:(NSURL*)url;{
if (![super init]) return nil;
[self setTargetURL:url];
return self;}- (void)dealloc {
[targetURL release], targetURL = nil;
[super dealloc];
}
- (void)main
{
NSString *webpageString = [[[NSString alloc]
initWithContentsOfURL:[self targetURL]] autorelease];
NSError *error = nil;
NSXMLDocument *document = [[NSXMLDocument alloc]
initWithXMLString:webpageString
options:NSXMLDocumentTidyHTML error:&error];
if (!document) {
NSLog(@"%s Error loading document (%@): %@",
_cmd, [[self targetURL] absoluteString], error);
return;
}
[[AppDelegate shared]
performSelectorOnMainThread:@selector(pageLoaded:)
withObject:document waitUntilDone:YES];
[document release];
}
@end
正如我们所看到的那样,这个类相当的简单,在它的init方法中接受一个url并保存起来,当main函数被调用的时候,它使用这个保存的url创建一个字符串,并将这个字符串传递给NSXMLDocumentinit方法。如果加载的xml数据没有出错,数据会被传递给AppDelegate,它处于主线程中。到此,这个线程的任务就完成了。在主线程中注销操作队列的时候,会将这个NSOperation对象释放。
AppDelegate.h
@interface AppDelegate : NSObject {
NSOperationQueue *queue;
}+ (id)shared;- (void)pageLoaded:(NSXMLDocument*)document;@endAppDelegate.m #import "AppDelegate.h"#import "PageLoadOperation.h"@implementation AppDelegate
static AppDelegate *shared;
static NSArray *urlArray;
- (id)init
{
if (shared)
{
[self autorelease];
return shared;
}
if (![super init]) return nil; NSMutableArray *array = [[NSMutableArray alloc] init];[array addObject:@"http://www.google.com"];[array addObject:@"http://www.apple.com"];[array addObject:@"http://www.yahoo.com"];[array addObject:@"http://www.zarrastudios.com"];[array addObject:@"http://www.macosxhints.com"];urlArray = array; queue = [[NSOperationQueue alloc] init];shared = self;return self;
}
• (void)applicationDidFinishLaunching:
(NSNotification *)aNotification
{
for (NSString *urlString in urlArray)
{
NSURL *url =
[NSURL URLWithString:urlString]; PageLoadOperation *plo =
[[PageLoadOperation alloc] initWithURL:url];
[queue addOperation:plo];
[plo release];
}
}
- (void)dealloc
{
[queue release], queue = nil;
[super dealloc];
}
+ (id)shared;
{
if (!shared) {
[[AppDelegate alloc] init];
}
return shared;
}
- (void)pageLoaded:(NSXMLDocument*)document;
{
NSLog(@"%s Do something with the XMLDocument: %@",
_cmd, document);
}
@end
NSOperationQueue的并行控制(NSOperationQueue Concurrency)
在上面这个简单的例子中,我们很难看出这些操作是并行运行的,然而,如果你你的操作花费的时间远远比这里的要长,你将会发现,队列是同时执行这些操作的。幸运的是,如果你想要为队列限制同时只能运行几个操作,你可以使用NSOperationQueue的setMaxConcurrentOperationCount:方法。例如,[queue setMaxConcurrentOperationCount:2];
发表评论
-
[iOS]iPhoto的删除动画(转)
2012-03-05 09:36 3101iPhoto 中想删除某张照片时,点击删除键,就能看到照 ... -
testBubble_泡泡聊天
2012-02-29 11:38 1103testBubble_zhou_min_泡泡聊天 ... -
BigHeadTip_谷歌地图大头针
2012-02-29 11:34 1575BigHeadTip_zhou_min_谷歌地图大头针 ... -
FFDownloadManager_alertview文件下载器
2012-02-29 11:28 1010FFDownloadManager_zhou_min_aler ... -
苹果官网--iPhone应用程序编程指南
2012-02-29 11:21 970苹果官网-zhou_min-iPhone应用程序编程指南 ... -
iOS之GCD
2012-02-12 21:35 0GCD之dispatch queue iOS中 ... -
循环滚动
2012-02-02 15:19 0循环滚动 -
自定义AlertView(多种色彩)
2012-02-02 14:50 1115自定义AlertView(多种色彩) ... -
用 UITableView 进行多选的代码例子
2012-02-02 14:19 1530用 UITableView 进行多选的代码例子 ... -
QQ风格的UITableView
2012-02-02 14:00 1248QQ风格的UITableView ... -
GLpaint画图
2012-02-01 23:50 1798GLpaint画图 -
滑动弹出菜单的代码例子
2012-02-01 23:31 1124滑动弹出菜单的代码例子可在应用中实现方便美观的按钮收放效果。如 ... -
UIAlertTableView 里进行单选和多选的代码例子
2012-02-01 23:22 1026UIAlertTableView 里进行单选和多选的代码例子 ... -
自定义控件用法和一些常用页面效果
2012-02-01 22:23 972自定义控件用法和一些常用页面效果 原文:http:// ... -
viewDidUnload 和 dealloc 的区别
2012-01-30 10:57 7397对于iphone开发人员来说,内存管理是极为重要的技巧,哪怕程 ... -
第二个iPhone应用程序:“Say Hello”
2012-01-30 10:49 1252项目需求 写一个iPhone应用程序,要求可以输入名字, ... -
[iOS]网页保存为png
2012-01-20 10:48 0保存 -
[iOS]iOS截屏
2012-01-19 17:22 2688UIGraphicsBeginImageContext ... -
[iOS]iOS对PDF加密,解密
2012-01-16 17:34 3114保护PDF内容为了保护PDF ... -
[iOS]AES加密在iOS上面的实现
2012-01-16 13:38 9097Encryption.h文件 // // En ...
相关推荐
配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加到NSOperationQueue中 ...
NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤: (1)先将需要执行的操作封装到一个NSOperation对象中 (2)然后将NSOperation...
ios多线程开发的常用四种方式和基本使用,pthread、NSThread、NSOperation\NSOperationQueue、GCD
#iOS的三种多线程技术 和特性 1.NSThread 2.NSOperation/NSOperationQueue 3.GCD (大中心调度)
对iOS的多线程方式做的总结;Pthreads,NSThread,GCD,NSOperation/NSOperationQueue
在app store中的很多应用程序非常的笨重,他们有好的界面,但操作性很差,比如说当程序从网上或本地载入数据的时候,界面被冻结了,用户只能等...除此之外,它们还引入了两个全新的类,NSOperation和NSOperationQueue。
1.、NSThread2、CocoaNSOperation(IOS多线程编程之NSOperation和NSOperationQueue的使用)3、GCD全称:GrandCentralDispatch(IOS多线程编程之Grand CentralDispatch(GCD)介绍和使用)这三种编程方式从上到下,抽象...
NSOperation和NSOperationQueue 1.NSThread 这是最轻量级的多线程的方法,使用起来最直观的多线程编程方法。但是因为需要自己管理线程的生命周期,线程同步。经常使用NSThread进行调试,在实际项目中不推荐使用。 ...
2、Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的使用)3、GCD 全称:Grand Central Dispatch( iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用) 这三种编程方式从上到下,抽象度...
给大家介绍在IOS中为什么使用多线程及多线程实现的三种方法,基本上使用这三种方法实现多线程(NSThread Grand Centeral Dispatch(GCD) NSOperation和NSOperationQueue),感兴趣的朋友可以参考下本
苹果官方文档的翻译,详细讲解。NSOperation+NSOperationQueue,GCD+dispatch_queue,RunLoop等详细介绍。
leetcode 答案 iOS- 在网上找的一些iOS面试资料 1、2018 iOS面试题---UI相关:事件传递,...iOS面试题-----多线程相关之NSOperation、NSOperationQueue、NSThread+runloop实现常驻线程、加锁 10、2019 iOS面试题-----R
YXQueue适用于iOS的OOP和轻松使用作业队列YXQueue被封装用于NSOperation。 线程的管理器和调用者按YXQueue划分。 使用它,开发人员将不会太在意线程管理,而只是关注如何创建作业和实现委托。建筑学...