400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

iOS如何使用多线程提升数据并发访问

这篇文章主要介绍iOS如何使用多线程提升数据并发访问,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联专注于名山企业网站建设,自适应网站建设,商城网站开发。名山网站建设公司,为名山等地区提供建站服务。全流程按需网站策划,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

第一个例子

这个例子演示了IO性能方面的一些问题。稍后我会通过多线程技术来加速代码的执行效率。我的例子很简单:

注意:对于这两种情况,我的例子不会缓存图片,这样是为了让你更清晰的看到这两者的不同。

表格6-1展示的测试结果,是基于Core Animation测试的结果,你可以在一个真实的环境下看到应用的测试结果。

iOS如何使用多线程提升数据并发访问

表格6-1的是在iPhone OS上处理和运行应用中,加载时的fps。你可以看到多线程能够显著的加速加载的过程。在没有使用多线程的情况中,加载过程会阻塞UI,你的应用将会被挂起。

我将会给你看一下源代码,在深入解释概念之前我会做一些简单的解释。Listing 6-1 是第一个测试的原代码。

Listing 6–1. First Benchmark; This Code Runs Inside the UITableViewDataSource’s Method.

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath {

       static NSString *CellIdentifier = @"Cell";

       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

       if (cell == nil) {

               cell = [[UITableViewCell alloc]      initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

       }

       NSURL *p_w_picpathURL = [NSURL URLWithString:[self.p_w_picpathsArray objectAtIndex:indexPath.row]];

       cell.p_w_picpathView.p_w_picpath = [UIImage p_w_picpathWithData:[NSData     dataWithContentsOfURL:p_w_picpathURL]];

   // Configure the cell.

    return cell;

}

Listing 6-2 只是显示了在异步代码中返回图片的通常做法。为了简单起见,事实上异步代码没有在这边显示或讨论。

Listing 6–2. Second Benchmark—Getting the Image Through a Background Thread

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

       static NSString *CellIdentifier = @"Cell";

       ImageCell *cell = (ImageCell *) [UIUtilities getCellWithTableView:tableViewcellIdentifier:CellIdentifiernibName:@"ImageCell"];

       // Configure the cell.

       NSURL *p_w_picpathURL = [NSURL URLWithString:[self.p_w_picpathsArrayobjectAtIndex:indexPath.row]];

       [cell.contentImage displayImageWithURL:p_w_picpathURL];

      return cell;

}

#import #import "ImageFetcher.h"

@interface UIImageView (Network)

       - (void)displayImageWithURL:(NSURL *)url;

@end

#import "UIImageView+Network.h"#import "ImageFetcher.h"

@implementation UIImageView (Network)

- (void)p_w_picpathFetcherFinished:(ImageFetcher *)fetcher {

       self.p_w_picpath = fetcher.p_w_picpath;

}

- (void)displayImageWithURL:(NSURL *)url {

       self.p_w_picpath = nil;
       if (url) {

           // This code will run in the background thread and callback when it retrieves// p_w_picpath
           [ImageFetcher loadImageWithURL:url delegate:self];

       }

}

@end

在第一个测试中,方法的内部返回了table view的cell,我在这里获取了图片。对于这行代码,iOS将会停止,然后等待图片从网络返回。之后,它继续返回cell和把cell显示给UI。这个等待过程使得应用停止,这也就是在第一个测试中你不能快速滚动table view的原因。

对于第二个测试,我使用了异步代码,它是多线程的另一种形式,但是底层库将会为你处理多线程代码。通过这段代码,主进程从等待下载完成的进程中解放出来。因此,在第二个测试中,你可以滚动table view,而没有任何问题。

多线程的好处

在iPhone应用中,你一些情况你应该考虑使用多线程。

以上是“iOS如何使用多线程提升数据并发访问”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


分享题目:iOS如何使用多线程提升数据并发访问
分享路径:http://mzwzsj.com/article/ghgcse.html

其他资讯

让你的专属顾问为你服务