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

发布时间:2021-10-21 10:13:20 作者:小新
来源:亿速云 阅读:163

这篇文章主要介绍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 <Foundation/Foundation.h>#import "ImageFetcher.h"

@interface UIImageView (Network) <ImageFetcherDelegate>

       - (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如何使用多线程提升数据并发访问”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. iOS使用多线程提高数据并发访问 之十
  2. iOS如何使用多线程提高数据并发访问

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:如何理解SpringCloud Hystrix源码解析

下一篇:什么是近似算法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》