困扰很久的一个 bug, help! - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
acumen
V2EX    iDev

困扰很久的一个 bug, help!

  •  
  •   acumen
    acumen1005 2017-04-18 11:51:26 +08:00 5997 次点击
    这是一个创建于 3103 天前的主题,其中的信息可能已经有所发展或是发生改变。

    关于 UICollectionView 的一个问题。UICollectionView 嵌套在 UITableViewCell 中。问题就是以下一段简单的代码,但是线上 [self.feed.blog.imageInfos objectAtIndex:indexPath.row] 这里经常出现数组越界,很奇怪!! indexPath 的 bound 不是上面的 collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 这个函数约束了吗,为什么会越界。

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return count = self.imageInfos.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { LKMImageInfo *imageInfo = [self.imageInfos objectAtIndex:indexPath.row]; ... return imageCell; } 
    7 条回复    2017-04-29 21:30:13 +08:00
    anerevol
        1
    anerevol  
       2017-04-18 12:39:03 +08:00
    有在其他线程改变 self.imageInfos 的值么 如果有的话,把这个操作放主线程试试
    acumen
        2
    acumen  
    OP
       2017-04-18 15:42:14 +08:00
    @anerevol 都是在主线程操作的,困扰很久在于这个 bug 很难复现,但是线上 crash log 里却很多。。。
    zhongdong
        3
    zhongdong  
       2017-04-18 15:44:46 +08:00
    放在 tableView 的 cell 里,排除一下是不是复用 cell 造成的?有没有及时更新都应 cell 里的 imageInfos ?/div>
    linKnowEasy
        4
    linKnowEasy  
       2017-04-18 16:21:24 +08:00
    1. 确认一下 section 是不是只有一个
    2. self.imageInfos, 哪里被修改了之后。 没有调用 collectionView . reload 代码。
    3. 这个 bug 很难复现, 感觉很大可能就是 修改 imageInfos 值相关代码的线程问题??.
    ps. 暂时只想到了这几种可能。
    HelloiWorld
        5
    HelloiWorld  
       2017-04-18 17:43:40 +08:00
    楼上说的是对的,有遇到类似问题,嵌套时发现它读取的仍然是修改前的数组,当时 count 不为 0 所以进了 cellFor 方法
    我一个过渡的办法是 修改数组或有任何赋值操作前 先将数组清空,然后调用 reloadData 方法, 然后再做修改赋值操作,再调用 reloadData 方法。
    c447279704
        6
    c447279704  
       2017-04-26 10:13:28 +08:00
    numberOf 和 cellfor 之间是很有可能改变 model 的,加个越界保护吧
    BB9z
        7
    BB9z  
       2017-04-29 21:30:13 +08:00
    UICollectionView 发生操作到访问可能 data source 有一个延迟,这点和 UITableView 不同。

    像这样的逻辑,是可能崩溃的。

    UICollectionView *cv = ...;
    [cv insertItemsAtIndexPaths:...];
    [dataSource insertObject:... atIndex:...];

    如果不用 reloadData 强制刷新,需要用 performBatchUpdates:completion: 来保护。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1465 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 16:43 PVG 00:43 LAX 09:43 JFK 12:43
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86