zoukankan      html  css  js  c++  java
  • iphone多线程汇总

    1,NSThread

    - (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath
    {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    UIImage *image = [self getImageForCellAtIndexPath:indexPath];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
    [pool release];
    }

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    [NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];
    }

    2NSOperation

    首先是建立NSOperationQueue和NSOperations。NSOperationQueue会建立一个线程,每个加入到线程operation会有序的执行。

    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc];
    initWithTarget:self
    selector:@selector(doWork:)
    object:someObject];
    [queue addObject:operation];
    [operation release];

    下面是使用NSOperationQueue的过程:

    1. 建立一个NSOperationQueue的对象
    2. 建立一个NSOperation的对象
    3. 将operation加入到NSOperationQueue中
    4. release掉operation

    使用NSOperation有几种,现在介绍最简单的一种NSInvocationOperation,NSInvocationOperation是NSOperation的子类,允许运行在operation中的targer和selector

    3NSURLConnection

    如果你是网络应用使用NSURLConnection的异步代理无意是最简便的解决方案

  • 相关阅读:
    C#.net使用DotNetCharting控件生成报表统计图
    部署DTCMS到Jexus遇到的问题及解决思路--验证码
    部署DTCMS到Jexus遇到的问题及解决思路---部署
    LNMP环境搭建
    LAMP环境搭建
    Mac开发配置手册
    MAC OS X的命令行技巧
    如何生成SSH key
    ssh-keygen 的 详解
    Mac上Homebrew的使用——Homebrew 使 OS X 更完整
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/2032259.html
Copyright © 2011-2022 走看看