CollectionViewController.m line 439
__50-[CollectionViewController photoLibraryDidChange:]_block_invoke

致命异常:NSInternalInconsistencyException
尝试删除并重新加载相同的索引路径({length = 2,path = 0 – 26007})

- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
    // Call might come on any background queue. Re-dispatch to the main queue to handle it.
    dispatch_async(dispatch_get_main_queue(),^{

        // check if there are changes to the assets (insertions,deletions,updates)
        PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:self.assetsFetchResults];
        if (collectionChanges) {

            // get the new fetch result
            self.assetsFetchResults = [collectionChanges fetchResultAfterChanges];

            UICollectionView *collectionView = self.collectionView;

            if (![collectionChanges hasIncrementalChanges] || [collectionChanges hasMoves]) {
                // we need to reload all if the incremental diffs are not available
                [collectionView reloadData];

            } else {
                // if we have incremental diffs,tell the collection view to animate insertions and deletions
                [collectionView performBatchUpdates:^{
                    NSIndexSet *removedindexes = [collectionChanges removedindexes];
                    if ([removedindexes count]) {
                        [collectionView deleteItemsAtIndexPaths:[removedindexes aapl_indexPathsFromIndexesWithSection:0]];
                    }
                    NSIndexSet *insertedindexes = [collectionChanges insertedindexes];
                    if ([insertedindexes count]) {
                        [collectionView insertItemsAtIndexPaths:[insertedindexes aapl_indexPathsFromIndexesWithSection:0]];
                    }
                    NSIndexSet *changedindexes = [collectionChanges changedindexes];
                    if ([changedindexes count]) {
                        [collectionView reloadItemsAtIndexPaths:[changedindexes aapl_indexPathsFromIndexesWithSection:0]];
                    }
                } completion:NULL];
            }

            [self resetCachedAssets];
        }
    });
}

来源:https://developer.apple.com/devcenter/download.action?path=/wwdc_2014/wwdc_2014_sample_code/exampleappusingphotosframework.zip

我无法复制这个问题.可能是什么问题呢?非常感谢!

解决方法

我今天能够再现这个.为此,您需要:

>打开正在监听更改的应用程序
>打开照片应用程序,将一组照片从iCloud共享相册保存到您的照片库
>转到照片应用程序,删除其中的一些照片
>再次转到iCloud共享相册,并再次保存您删除的一些照片.你会看到这种情况发生.

我发现一个更新的代码似乎在这里更好地处理更新行为:
https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibraryChangeObserver_Protocol/

但是它仍然不会处理这种情况,也不会在要删除的索引更大时(即由于未捕获的异常’NSInternalInconsistencyException’终止应用程序),原因是:尝试从第0部分删除第9项,其中只包含9个更新前的项目).我创建了这个代码更新版本,处理这个更好,迄今为止还没有为我崩溃.

func photoLibraryDidChange(changeInfo: PHChange!) {

    // Photos may call this method on a background queue;
    // switch to the main queue to update the UI.
    dispatch_async(dispatch_get_main_queue()) {


        // Check for changes to the list of assets (insertions,moves,or updates).
        if let collectionChanges = changeInfo.changeDetailsForFetchResult(self.assetsFetchResult) {

            // Get the new fetch result for future change tracking.
            self.assetsFetchResult = collectionChanges.fetchResultAfterChanges

            if collectionChanges.hasIncrementalChanges {

                // Get the changes as lists of index paths for updating the UI.
                var removedpaths: [NSIndexPath]?
                var insertedpaths: [NSIndexPath]?
                var changedpaths: [NSIndexPath]?
                if let removed = collectionChanges.removedindexes {
                    removedpaths = self.indexPathsFromIndexSetWithSection(removed,section: 0)
                }
                if let inserted = collectionChanges.insertedindexes {
                    insertedpaths = self.indexPathsFromIndexSetWithSection(inserted,section: 0)
                }
                if let changed = collectionChanges.changedindexes {
                    changedpaths = self.indexPathsFromIndexSetWithSection(changed,section: 0)
                }
                var shouldReload = false
                if changedpaths != nil && removedpaths != nil{
                    for changedpath in changedpaths!{
                        if contains(removedpaths!,changedpath){
                            shouldReload = true
                            break
                        }
                    }

                }

                if removedpaths?.last?.item >= self.assetsFetchResult.count{
                    shouldReload = true
                }

                if shouldReload{
                    self.collectionView.reloadData()
                }else{
                    // Tell the collection view to animate insertions/deletions/moves
                    // and to refresh any cells that have changed content.
                    self.collectionView.performBatchUpdates(
                        {
                            if let theRemovedpaths = removedpaths {
                                self.collectionView.deleteItemsAtIndexPaths(theRemovedpaths)
                            }
                            if let theInsertedpaths = insertedpaths {
                                self.collectionView.insertItemsAtIndexPaths(theInsertedpaths)
                            }
                            if let theChangedpaths = changedpaths{
                                self.collectionView.reloadItemsAtIndexPaths(theChangedpaths)
                            }
                            if (collectionChanges.hasMoves) {
                                collectionChanges.enumerateMovesWithBlock() { fromIndex,toIndex in
                                    let fromIndexPath = NSIndexPath(forItem: fromIndex,inSection: 0)
                                    let toIndexPath = NSIndexPath(forItem: toIndex,inSection: 0)
                                    self.collectionView.moveItemAtIndexPath(fromIndexPath,toIndexPath: toIndexPath)
                                }
                            }
                        },completion: nil)

                }

            } else {
                // Detailed change information is not available;
                // repopulate the UI from the current fetch result.
                self.collectionView.reloadData()
            }
        }
    }
}

func indexPathsFromIndexSetWithSection(indexSet:NSIndexSet?,section:Int) -> [NSIndexPath]?{
    if indexSet == nil{
        return nil
    }
    var indexPaths:[NSIndexPath] = []

    indexSet?.enumerateIndexesUsingBlock { (index,Bool) -> Void in
        indexPaths.append(NSIndexPath(forItem: index,inSection: section))
    }
    return indexPaths

}

Swift 3 / iOS 10版本:

func photoLibraryDidChange(_ changeInstance: PHChange) {
    guard let collectionView = self.collectionView else {
        return
    }

    // Photos may call this method on a background queue;
    // switch to the main queue to update the UI.
    dispatchQueue.main.async {
        guard let fetchResults = self.fetchResults else {
            collectionView.reloadData()
            return
        }

        // Check for changes to the list of assets (insertions,or updates).
        if let collectionChanges = changeInstance.changeDetails(for: fetchResults) {
            // Get the new fetch result for future change tracking.
            self.fetchResults = collectionChanges.fetchResultAfterChanges

            if collectionChanges.hasIncrementalChanges {
                // Get the changes as lists of index paths for updating the UI.
                var removedpaths: [IndexPath]?
                var insertedpaths: [IndexPath]?
                var changedpaths: [IndexPath]?
                if let removed = collectionChanges.removedindexes {
                    removedpaths = self.indexPaths(from: removed,section: 0)
                }
                if let inserted = collectionChanges.insertedindexes {
                    insertedpaths = self.indexPaths(from:inserted,section: 0)
                }
                if let changed = collectionChanges.changedindexes {
                    changedpaths = self.indexPaths(from: changed,section: 0)
                }
                var shouldReload = false
                if let removedpaths = removedpaths,let changedpaths = changedpaths {
                    for changedpath in changedpaths {
                        if removedpaths.contains(changedpath) {
                            shouldReload = true
                            break
                        }
                    }
                }

                if let item = removedpaths?.last?.item {
                    if item >= fetchResults.count {
                        shouldReload = true
                    }
                }

                if shouldReload {
                    collectionView.reloadData()
                } else {
                    // Tell the collection view to animate insertions/deletions/moves
                    // and to refresh any cells that have changed content.
                    collectionView.performBatchUpdates({
                        if let theRemovedpaths = removedpaths {
                            collectionView.deleteItems(at: theRemovedpaths)
                        }
                        if let theInsertedpaths = insertedpaths {
                            collectionView.insertItems(at: theInsertedpaths)
                        }
                        if let theChangedpaths = changedpaths {
                            collectionView.reloadItems(at: theChangedpaths)
                        }

                        collectionChanges.enumerateMoves { fromIndex,toIndex in
                            collectionView.moveItem(at: IndexPath(item: fromIndex,section: 0),to: IndexPath(item: toIndex,section: 0))
                        }
                    })
                }
            } else {
                // Detailed change information is not available;
                // repopulate the UI from the current fetch result.
                collectionView.reloadData()
            }
        }
    }
}

func indexPaths(from indexSet: IndexSet?,section: Int) -> [IndexPath]? {
    guard let set = indexSet else {
        return nil
    }

    return set.map { (index) -> IndexPath in
        return IndexPath(item: index,section: section)
    }
}

ios – CRASH尝试删除并重新加载相同的索引路径的更多相关文章

  1. ios – CRASH尝试删除并重新加载相同的索引路径

    非常感谢!

  2. Swift - 动态添加删除TableView的单元格以及内部元件-日期控件

    比如我们做一个消息提醒页面,默认页面只显示两个单元格。当点击第二个单元格时,下面会再添加一个单元格放置日期选择控件。而再次点击第二个单元格,日期选择控件又会隐藏。//日期选择器显示状态datePickerVisible:Bool=falseoverridefuncviewDidLoad(){super.viewDidLoad()self.title="添加任务"//去除尾部多余的空行.tableView.tableFooterView=UIView}didReceiveMemoryWarning(){.d

  3. [IOSS]UITableView分组

    [IOSS]UITableView分组DEMO:http://download.csdn.net/detail/u012881779/9233421应用入口(AppDelegate.swift)分组控制器(DMViewController.swift)分组Cell(DMTableViewCell.swift)示意图:

  4. Swift:表格视图单元格单选二

    效果前言前段时间写了一篇博客:表格视图单元格单选(一),实现起来并不复杂,简单易懂。,由于每一组中,单元格内容不一致,因此建议用字典存储。我们可以为它创建一个indexPath属性,在配置表格视图协议方法cellForRowAtIndexPath:时,我们赋值单元格的indexPath属性就OK了。

  5. Swift - 给表格添加编辑功能删除,插入

    overridefuncloadView(){super.loadView()}viewDidLoad(){.viewDidLoad()//初始化数据,这一次数据,我们放在属性列表文件里self.allnames=[0:[](["UILabel标签"ottom:auto!important;font-family:Consolas,"UITableView表格视图"])];print.adHeaders=["常见UIKit控件"ottom:auto!important;font-family:Consol

  6. Swift - 动态添加删除TableView的单元格以及内部元件

    在Swift开发中,我们有时需要动态的添加或删除列表的单元格。而再次点击第二个单元格,日期选择控件又会隐藏。//日期选择器显示状态datePickerVisible:Bool=falseoverridefuncviewDidLoad(){super.viewDidLoad()self.title="添加任务"//去除尾部多余的空行.tableView.tableFooterView=UIView}didReceiveMemoryWarning(){.didReceiveMemoryWarning()}//

  7. UICollectionView 总结

    使用UICollectionView必做的事情首先你的UIViewController要继承UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout。插入、移动、删除cell以及cell总数的问题UICollectionView是在生成cell的时候,先通过numberOfItemsInSection获得cell数量,然后一个一个生成添加在视图中。当我们直接通过insertItemAtInd

  8. swift -- 更改 tableview section header

    functableView(tableView:UITableView,willdisplayHeaderViewview:UIView,forSectionsection:Int){letheaderView=viewas!UITableViewheaderfooterViewheaderView.textLabel?.font=UIFont.systemFontOfSize(9)}

  9. 如何实现可收起和展开的Table Section

    如何实现可收起和展开的TableSection这是一个简单的iOSswift项目,旨在介绍如何实现可收起和展开的tablesection,并且,项目不需要mainstoryboard,XIB,注册nib等,只需要纯的Swfit代码!切换收起和展开的函数如下:注意到我们不是简单的重绘整个section,实际上我们只需要重绘section里的所有cell就好,这样做的好处是避免了sectionheader因重绘时闪烁的效果,最重要是的可以让我们更平滑地处理我们想要的动画效果,例如旋转那个箭头,改变背景颜色等等

  10. storyboard tableview section

    设置一下tableview下的content属性为staticcells,section设置为2,style为group如下图

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部