gpt4 book ai didi

ios - NSOperation 类运行多个操作

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:05:29 33 4
gpt4 key购买 nike

所以我问了几个关于 UICollectionView 的问题。了解它是如何工作的,我正在尝试实现延迟加载以将 15 个图像加载到 View Controller 上。我找到了很多例子 1 , 2 , 3 ...第一个和第三个示例只处理一个操作,第二个示例我认为根本不使用操作,只使用线程。我的问题是是否可以使用 NSOperation 类并使用/重用操作?我读到你不能重新运行操作,但我认为你可以再次初始化它们。这是我的代码:

View Controller :

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
[layout setItemSize:CGSizeMake(75, 75)];
self.images = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 230, self.view.frame.size.width, 200) collectionViewLayout:layout];
self.images.delegate = self;
self.images.dataSource = self;
[self.images registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.view addSubview:self.images];
self.operation = [[NSOperationQueue alloc]init];
[self.operation addOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"getgallery.php?user=%@", userId] relativeToURL:[NSURL URLWithString:@"http://www.mywebsite.com/"]];
NSData *data = [NSData dataWithContentsOfURL:url];
//datasource for all images
self.imagesGalleryPaths = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[self.images reloadData];
//reload collection view to place placeholders
}];
}];

- (void)viewDidAppear:(BOOL)animated{
//get the visible cells right away
visibleCellPaths = [NSArray new];
visibleCellPaths = self.images.indexPathsForVisibleItems;
self.processedImages = [[NSMutableDictionary alloc]initWithCapacity:visibleCellPaths.count];
}
#pragma mark - collection view
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
int i;
if (self.imagesGalleryPaths.count == 0)
i = 25;
else
i = self.imagesGalleryPaths.count;
return i;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [UICollectionViewCell new];
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.layer.borderWidth = 1;
cell.layer.borderColor = [[UIColor whiteColor]CGColor];
UIImageView *image = [[UIImageView alloc]init];
if (self.imagesGalleryPaths.count != 0) {
if ([visibleCellPaths containsObject:indexPath]) {
[self setUpDownloads:visibleCellPaths];
}
image.image = [UIImage imageNamed:@"default.png"];
image.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
[cell.contentView addSubview:image];
}
return cell;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
visibleCellPaths = [NSArray new];
visibleCellPaths = self.images.indexPathsForVisibleItems;
[self setUpDownloads:visibleCellPaths];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
visibleCellPaths = [NSArray new];
visibleCellPaths = self.images.indexPathsForVisibleItems;
[self setUpDownloads:visibleCellPaths];
}

- (void)setUpDownloads:(NSArray *)visiblePaths{
//I want to pass the visiblePaths to the NSOperation class if visible cells changed
GalleryOps *gallery = [[GalleryOps alloc]init];
//I will use a dictionary to keep track of which indexPaths are being downloaded...
//...so there are no duplicate downloads
}

GalleryOps.m

@implementation GalleryOps

- (void)main{
//would I initialize all the operations here and perform them?
}

显示空的 GalleryOps 类几乎毫无意义,因为我不知道如何使用多个操作对其进行初始化。我知道我必须重写主要方法,一旦我从 URL 获取图像数据,我就需要更新 UI,为此我需要一个委托(delegate)方法……另一件事我还不知道该怎么做但是有很多例子可以说明这一点。我最大的问题是如何将可见单元格传递到此类并运行多个操作?当新的可见单元格出现时,我将进行检查以查看哪些要取消,哪些要保留。这里有什么建议吗?提前致谢!

最佳答案

看着你的proposed solution ,看起来您想推迟使操作可取消的问题。此外,看起来您想延迟缓存的使用(即使它并不比您的 NSMutableDictionary 属性复杂)。

因此,撇开这一点不谈,您修改后的代码示例有两个“全局”问题:

  1. 您可以大大简化图像检索过程。 startOperationForVisibleCells 和两个滚动委托(delegate)的使用不必要地复杂。有一个更简单的模式,您可以在其中停用这三种方法(并实现更好的用户体验)。

  2. 您的 cellForItemForIndexPath 有问题,您正在添加 subview 。问题在于单元格被重用,因此每次重用单元格时,您都会添加更多冗余 subview 。

    你真的应该子类化 UICollectionViewCell(在我下面的例子中是 CustomCell),把单元格的配置,包括 subview 的添加,放在那里。它简化了您的 cellForItemAtIndexPath 并消除了添加额外 subview 的问题。

除了这两个大问题,还有一堆小问题:

  1. 您忽略了为您的操作队列设置 maxConcurrentOperationCount。您确实希望将其设置为 45 以避免操作超时错误。

  2. 您正在使用 NSIndexPath 键入您的 imageGalleryData。问题是如果你删除了一行,你所有的后续索引都会出错。我怀疑这现在不是问题(您可能不希望删除项目),但如果您通过其他内容(例如 URL)键入它,它同样容易,但它更适合 future 。

  3. 我建议将您的操作队列从 operation 重命名为 queue。同样,我将 images(可能被错误地推断为图像数组)中的 UICollectionView 重命名为 collectionView 之类的名称。这是风格上的,如果你不想,你不必这样做,但这是我在下面使用的惯例。

  4. 与其将 NSData 保存在名为 imageGalleryDataNSMutableDictionary 中,不如保存 UIImage 代替。这使您不必在回滚到之前下载的单元格时从 NSData 重新转换为 UIImage(这将使滚动过程更顺畅)。

所以,把所有这些放在一起,你会得到类似的东西:

static NSString * const kCellIdentifier = @"CustomCellIdentifier";

- (void)viewDidLoad
{
[super viewDidLoad];

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
[layout setItemSize:CGSizeMake(75, 75)];

// renamed `images` collection view to `collectionView` to follow common conventions

self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 230, self.view.frame.size.width, 200) collectionViewLayout:layout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;

// you didn't show where you instantiated this in your examples, but I'll do it here

self.imageGalleryData = [NSMutableDictionary dictionary];

// register a custom class, not `UICollectionViewCell`

[self.collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:kCellIdentifier];
[self.view addSubview:self.collectionView];

// (a) change queue variable name;
// (b) set maxConcurrentOperationCount to prevent timeouts

self.queue = [[NSOperationQueue alloc]init];
self.queue.maxConcurrentOperationCount = 5;

[self.queue addOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"getgallery.php?user=%@", userId] relativeToURL:[NSURL URLWithString:@"http://www.mywebsite.com/"]];
NSData *data = [NSData dataWithContentsOfURL:url];
//datasource for all images
self.imagesGalleryPaths = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[self.collectionView reloadData];
}];
}];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.imagesGalleryPaths count]; // just use whatever is the right value here, don't make this unnecessarily smaller
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];

NSString *key = self.imagesGalleryPaths[indexPath.row]; // I don't know whether this was simply array, or some nested structure, so tweak this accordingly
UIImage *image = self.imageGalleryData[key];

if (image) {
cell.imageView.image = image; // if we have image already, just use it
} else {
cell.imageView.image = [UIImage imageNamed:@"profile_default.png"]; // otherwise set the placeholder ...

[self.queue addOperationWithBlock:^{ // ... and initiate the asynchronous retrieval
NSURL *url = [NSURL URLWithString:...]; // build your URL from the `key` as appropriate
NSData *responseData = [NSData dataWithContentsOfURL:url];
if (responseData != nil) {
UIImage *downloadedImage = [UIImage imageWithData:responseData];
if (downloadedImage) {
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
self.imageGalleryData[key] = downloadedImage;
CustomCell *updateCell = (id)[collectionView cellForItemAtIndexPath:indexPath];
if (updateCell) {
updateCell.imageView.image = downloadedImage;
}
}];
}
}
}];
}

return cell;
}

// don't forget to purge your gallery data if you run low in memory

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[self.imageGalleryData removeAllObjects];
}

现在,显然我无权访问您的服务器,所以我无法检查这一点(特别是,我不知道您的 JSON 返回的是完整的 URL 还是仅返回文件名,或者是否有一些嵌套的字典数组)。但我不希望您过于迷失在我的代码细节中,而是查看基本模式:消除可见单元格的循环和对滚动事件的响应,并让 cellForItemAtIndexPath 执行所有的工作都给你。

现在,我介绍的一件事是 CustomCell 的概念,它是一个 UICollectionViewCell 子类,可能如下所示:

//  CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UICollectionViewCell

@property (nonatomic, weak) UIImageView *imageView;

@end

然后将单元格配置和 subview 添加到@implementation:

//  CustomCell.m

#import "CustomCell.h"

@implementation CustomCell

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.borderWidth = 1;
self.layer.borderColor = [[UIColor whiteColor]CGColor];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[self addSubview:imageView];
_imageView = imageView;
}
return self;
}

@end

顺便说一句,我仍然认为,如果你想正确地做到这一点,你应该引用 my other answer .如果你不想迷失在正确实现的那些杂草中,那么只需使用支持异步图像检索、缓存、可见单元格网络请求优先级等的第三方 UIImageView 类别。 ,例如 SDWebImage .

关于ios - NSOperation 类运行多个操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23276228/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com