草庐IT

ios - NSOperation 类运行多个操作

coder 2024-01-16 原文

所以我问了几个关于 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/

有关ios - NSOperation 类运行多个操作的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  6. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  7. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  8. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  9. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  10. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

随机推荐