草庐IT

ios - UICollectionView reloadData 看起来很慢/滞后(不是即时的)

coder 2023-10-01 原文

您好:我在我的应用程序中使用了集合 View ,我注意到使用 reloadData 刷新所需的时间比预期的要长。 .我的收藏 View 有 1 section ,我正在用 5 cell 测试它s(每个都有 2 个按钮和一个标签)。我将一些日志放入我的代码中,以显示系统实际需要多长时间来刷新。有趣的是,日志表明它的刷新速度比现在快。例如,在设备上,它最多需要约 0.2 秒(明显),但这里是日志:
0.007s从时间 reloadData被叫到时间cellForItemAtIndexPath第一次被称为
0.002s每个单元格加载和返回
0.041s从时间 reloadData被调用到单元格 #5 返回的时间
cellForItemAtIndexPath 中没有什么特别密集的地方函数(基本上只是在 NSArrayindexPathrow 中找到具有 3 个值的字典)。 然而,即使我删除它并返回一个带有空白按钮的单元格,我也看到了相同的行为。

有没有人知道为什么会发生这种情况?顺便说一下,它只发生在物理设备(iPad Air)上。谢谢!

编辑 #1

根据@brian-nickel 的评论,我使用了 Time Profiler 工具,发现它确实每次都出现峰值 reloadData叫做。这是一个屏幕截图:



@ArtSabintsev,这是围绕 reloadData 的函数调用,然后是 cellForItemAtIndexPath :

//Arrays were just reset, load new data into them
//Loop through each team
for (NSString *team in moveUnitsView.teamsDisplaying) { //CURRENT TEAM WILL COME FIRST

    //Create an array for this team
    NSMutableArray *teamArr = [NSMutableArray new];

    //Loop through all units
    for (int i = [Universal units]; i > 0; i--) {

        //Set the unit type to a string
        NSString *unitType = [Universal unitWithTag:i];

        //Get counts depending on the team
        if ([team isEqualToString:currentTeam.text]) {

            //Get the number of units of this type so that it supports units on transports. If the territory is a sea territory and the current unit is a ground unit, check the units in the transports instead of normal units
            int unitCount = (ter.isSeaTerritory && (i == 1 || i == 2 || i == 8)) ? [self sumOfUnitsInTransportsOfType:unitType onTerritory:ter onTeam:team] : [ter sumOfUnitsOfType:unitType onTeam:team];

            //Get the number of movable units on this territory
            int movableCount = 0;
            if (queue.selectedTerr != nil && queue.selectedTerr != ter) { //This is here to prevent the user from selecting units on another territory while moving units from one territory
                movableCount = 0;
            } else if (ter.isSeaTerritory && (i == 1 || i == 2 || i == 8)) { //Units on transports - can be an enemy territory
                movableCount = [self sumOfUnitsInTransportsOfType:unitType onTerritory:ter onTeam:team];
            } else if ([Universal allianceExistsBetweenTeam:team andTeam:ter.currentOwner] || i == 3 || i == 9) { //Other units - only planes can be on an enemy territory
                movableCount = [ter sumOfMovableUnitsOfType:unitType onTeam:team];
            }

            //See if there are units of this type on this territory on this team
            if (unitCount > 0) {

                //Add data to this team's dictionary
                NSMutableDictionary *unitInfo = [NSMutableDictionary new];
                [unitInfo setObject:@(i) forKey:@"UnitTag"];
                [unitInfo setObject:unitType forKey:@"UnitType"];
                [unitInfo setObject:@(unitCount) forKey:@"Count"];
                [unitInfo setObject:@(movableCount) forKey:@"MovableCount"];
                [unitInfo setObject:team forKey:@"Team"];

                //Add the dictionary
                [teamArr addObject:unitInfo];

                //Increment the counter
                if (unitsOnCT) { //Must check or it could cause a crash
                    *unitsOnCT += 1;
                }
            }
        }
    }

    //Add the team array
    [moveUnitsView.unitData addObject:teamArr];
}

//Reload the data in the collection view
[moveUnitsView.collectionV reloadData];

还有我的 cellForItemAtIndexPath的相关代码:
    //Dequeue a cell
    UnitSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UnitSelectionCell" forIndexPath:indexPath];

    //Get the team array (at the index of the section), then the unit's data (at the index of the row)
    NSMutableDictionary *unitData = (moveUnitsView.unitData[indexPath.section])[indexPath.row];

    //Get values
    int unitTag = [[unitData objectForKey:@"UnitTag"] intValue];
    int count = [[unitData objectForKey:@"Count"] intValue];
    int movableCount = [[unitData objectForKey:@"MovableCount"] intValue];
    NSString *unitType = [unitData objectForKey:@"UnitType"];

    //Set the cell's values
    [cell.upB addTarget:self action:@selector(upMoveUnits:) forControlEvents:UIControlEventTouchUpInside]; [cell.upB setTag:unitTag];
    [cell.iconB setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[Universal imageNameForUnit:unitType team:[unitData objectForKey:@"Team"]] ofType:nil]] forState:UIControlStateNormal];
    [cell.iconB setTitle:[Universal strForExpDisplay:count] forState:UIControlStateNormal];
    [Universal adjustTitlePlacementOfB:cell.iconB autosize:FALSE]; //Don't autosize because this is a collection view
    cell.unitTypeL.text = unitType;
    cell.unitTypeL.adjustsFontSizeToFitWidth = cell.unitTypeL.adjustsLetterSpacingToFitWidth = TRUE;

    //Set fonts
    [Universal setFontForSubviewsOfView:cell];

    //Return the cell
    return cell;

初始化集合 View 时,使用以下方法注册单元格:
[moveUnitsView.collectionV registerNib:[UINib nibWithNibName:@"UnitSelectionCell" bundle:nil] forCellWithReuseIdentifier:@"UnitSelectionCell"];

编辑 #2

@roycable 和@aaron-brager 指出这可能是由于使用 imageWithContentsOfFile: 造成的。 .为了测试这一点,我更改了 cellForItemAtIndexPath对此:
    //Dequeue a cell
    UnitSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UnitSelectionCell" forIndexPath:indexPath];

    //Get the team array (at the index of the section), then the unit's data (at the index of the row)
    NSMutableDictionary *unitData = (moveUnitsView.unitData[indexPath.section])[indexPath.row];

    //Get values
    int unitTag = [[unitData objectForKey:@"UnitTag"] intValue];

    [cell setBackgroundColor:[UIColor redColor]];
    [cell.upB removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
    [cell.upB addTarget:self action:@selector(upMoveUnits:) forControlEvents:UIControlEventTouchUpInside]; [cell.upB setTag:unitTag];

    //Return the cell
    return cell;

奇怪的是,这似乎并没有解决问题。它实际上没有在该功能中执行任何密集型任务,但它似乎仍然滞后(时间分析器似乎证实了这一点)。

响应来自 Universal 的代码请求, 而不是发布代码,我只是总结一下:
+units刚刚返回 17+unitWithTag:使用 switch返回 NSString对应于1-17之间的数字
+allianceExistsBetweenTeam:查看数组是否包含其中一个字符串
+setFontForSubviewsOfView:是一个递归函数,基本上使用 this code

不幸的是,这似乎不是很重要,因为问题仍然存在于过于简单化的 cellForItemAtIndexPath 中。功能。

我还实现了@aaron-brager 的新建议。我删除了 target在添加一个新的之前,我对 Time Profiler 进行了更改。我没有看到任何东西真正弹出......这是屏幕截图。与 UIImage 相关的一切与这个问题无关,就像 NSKeyedArchiver ,所以唯一真正有意义的其他东西是字符串、数组和字典:



非常感谢任何和所有帮助 - 我真的需要解决这个问题(因此是赏金)。谢谢!

编辑 #3 - 确定的解决方案

所以,事实证明问题不在于这两个函数中的任何一个。问题是调用上面的更新函数的函数(我们称之为 Function A )(我们称之为 Function B )。紧随其后 Function AFunction B ,它执行了 CPU 密集型任务。我不知道 reloadDataat least partially asynchronous ,所以我假设 CPU 密集型任务和 reloadData最终争夺 CPU 时间。我通过在 return cell; 之前添加以下内容解决了我的问题:
    if (indexPath.row == [self collectionView:collectionView numberOfItemsInSection:indexPath.section] - 1) {

        [self performSelector:@selector(performMyCPUIntensiveTask:) withObject:myObject afterDelay:0.1];
    }

我希望这会在将来对其他人有所帮助。感谢所有帮助过我的人,我衷心感谢。

最佳答案

当您调用 reloadData 时,请确保您在主线程上.

NSLog("on main thread: %@", [NSThread isMainThread] ? @"YES" : @"NO");

如果不是,则使用 GCD 在主线程上发送消息:
dispatch_async(dispatch_get_main_queue(), ^{
    [moveUnitsView.collectionV reloadData];
});

(对语法不是 100% 确定,我只是在浏览器中输入了这个)

关于ios - UICollectionView reloadData 看起来很慢/滞后(不是即时的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25614210/

有关ios - UICollectionView reloadData 看起来很慢/滞后(不是即时的)的更多相关文章

  1. ruby - 即时确定方法的可见性 - 2

    我正在编写一个方法,它将在一个类中定义一个实例方法;类似于attr_accessor:classFoocustom_method(:foo)end我通过将custom_method函数添加到Module模块并使用define_method定义方法来实现它,效果很好。但我无法弄清楚如何考虑类(class)的可见性属性。例如,在下面的类中classFoocustom_method(:foo)privatecustom_method(:bar)end第一个生成的方法(foo)必须是公共(public)的,第二个(bar)必须是私有(private)的。我怎么做?或者,如何找到调用我的cust

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. 【Java 面试合集】HashMap中为什么引入红黑树,而不是AVL树呢 - 2

    HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  7. ruby-on-rails - 只有当不是 nil 时才执行映射? - 2

    如果names为nil,则以下中断。我怎样才能让这个map只有在它不是nil时才执行?self.topics=names.split(",").mapdo|n|Topic.where(name:n.strip).first_or_create!end 最佳答案 其他几个选项:选项1(在其上执行map时检查split的结果):names_list=names.try(:split,",")self.topics=names_list.mapdo|n|Topic.where(name:n.strip).first_or_create!e

  8. ruby-on-rails - Rails 格式验证——字母数字,但不是纯数字 - 2

    什么是测试格式验证的最佳方法让我们说一个用户名,使用字母数字的正则表达式,但不是纯数字?我一直在我的模型中使用以下验证validates:username,:format=>{:with=>/^[a-z0-9]+[-a-z0-9]*[a-z0-9]+$/i}数字用户名(例如“342”)通过了验证,这是我不想要的。 最佳答案 您想“向前看”一封信:/\A(?=.*[a-z])[a-z\d]+\Z/i 关于ruby-on-rails-Rails格式验证——字母数字,但不是纯数字,我们在Sta

  9. ruby - 强制浏览器下载文件而不是打开文件 - 2

    我要下载http://foobar.com/song.mp3作为song.mp3,而不是让Chrome在其native中打开它浏览器中的播放器。我怎样才能做到这一点? 最佳答案 您只需要确保发送这些header:Content-Disposition:attachment;filename=song.mp3;Content-Type:application/octet-streamContent-Transfer-Encoding:binarysend_file方法为您完成:get'/:file'do|file|file=File.

  10. ruby - 使用访问器方法即时创建对象 - 2

    使用散列定义的访问器方法动态创建对象的最简单方法是什么?例如,如果我有一个散列:{foo:"Foo",bar:"Bar"}我想要一个具有访问器方法foo、foo=、bar和bar=的对象,其初始值分别为"Foo"和"Bar"。我可以想到这样做:moduleObjectWithAccessordefself.newh;Struct.new(*h.keys).new(*h.values)endendo=ObjectWithAccessor.new(foo:"Foo",bar:"Bar")o.foo#=>"Foo"但是,我不需要它们的多个实例具有相同的特定键集,而是希望每次都使用可能不同的键

随机推荐