NSFetchedResultsController CoreData UICollectionView插入问题

时间:2018-01-18 20:01:31

标签: ios swift core-data uicollectionview nsfetchedresultscontroller

我正在尝试使用NSFetchedResultsController / NSFetchedResultsControllerDelegate更新UICollectionView。 UPDATE UICollectionView有21个单元格,每行有3个单元格。

我的想法是我正在下载一些图像数据,我将它们保存在BackgroundContext上的CoreData上 (backgroundContext = NSManagedObjectContext(concurrencyType:.privateQueueConcurrencyType) backgroundContext.parent = context) context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)

当我下载并保存在backgroundContext上数据时,我正在加载一个带有UICollectionView的新Controller,我正在获取他们将要保存在backgroundContext上的数据,而backgroundContext将会告知新数据的上下文,并使用NSFetchedResultsControllerDelegate函数,我将更新我的UICollectionViewCells。

更新每次从API下载的imageData为21。

保存

stack!.backgroundContext.perform({
                        let photoFrame = PhotoFrame(pin: pin, imageData: imageData as NSData, context: self.stack!.backgroundContext)
                        do{
                            try self.stack!.backgroundContext.save()
                            print("OK Saved Background")
                        }catch{
                            print("Error background saving")
                        }
                    })

一切都很好我得到了数据存储!

NSFetchedResultsControllerDelegate

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        insertIndexPaths = [IndexPath]()
        deleteIndexPaths = [IndexPath]()
        updateIndexPaths = [IndexPath]()

        print("*** controller will change content")
    }

    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {

        switch type{

        case .insert:
            print("Insert an item newIndexPath : \(newIndexPath!)")
            self.insertIndexPaths.append(newIndexPath!)

        case .delete:
            print("Delete an Item")
            deleteIndexPaths.append(indexPath!)

        case .update:
            print("Update an item")
            updateIndexPaths.append(indexPath!)

        case .move:
            print("move")


        }
    }

    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {


        collectionView.performBatchUpdates({
            for indexPath in self.insertIndexPaths {

                print("Inserting FetchedObjects at: \(indexPath)")
                self.collectionView.insertItems(at: [indexPath])

            }

            for indexPath in self.deleteIndexPaths {
                self.collectionView.deleteItems(at: [indexPath])
            }

            for indexPath in self.updateIndexPaths{
                self.collectionView.reloadItems(at: [indexPath])
            }

        }, completion: {finished in
            print("Finished")

        })
    }

我正在使用此功能加载单元格

ConfigureCell

cell.activityIndicator.startAnimating()
        cell.collectionImageView.image = UIImage(named: "placeholder")


        if frc!.fetchedObjects!.count > 0 {
            if cell.collectionImageView.image == UIImage(named: "placeholder") {
                print("IndexPath: \(indexPath)")
                print("fetchedObjects : \(frc!.fetchedObjects!.count)")

                if let photoFrame = frc!.object(at: indexPath) as? PhotoFrame{

                    cell.collectionImageView.image = UIImage(data:(photoFrame.imageData! as NSData) as Data)
                    cell.activityIndicator.stopAnimating()
                cell.activityIndicator.isHidden = true
                }
            }
        }

一切正常但是在NSFetchedResultsControllerDelegate函数

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>)

当我运行应用程序时,我得到了这个奇怪的输出:

*** controller will change content
Insert an item newIndexPath : [0, 0]
Inserting FetchedObjects at: [0, 0]
IndexPath: [0, 0]
fetchedObjects : 1
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 0]
Inserting FetchedObjects at: [0, 0]
IndexPath: [0, 0]
fetchedObjects : 2
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 0]
Inserting FetchedObjects at: [0, 0]
IndexPath: [0, 0]
fetchedObjects : 3
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 1]
Inserting FetchedObjects at: [0, 1]
IndexPath: [0, 1]
fetchedObjects : 4
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 1]
Inserting FetchedObjects at: [0, 1]
IndexPath: [0, 1]
fetchedObjects : 5
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 2]
Inserting FetchedObjects at: [0, 2]
IndexPath: [0, 2]
fetchedObjects : 6
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 2]
Inserting FetchedObjects at: [0, 2]
IndexPath: [0, 2]
fetchedObjects : 7
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 3]
Inserting FetchedObjects at: [0, 3]
IndexPath: [0, 3]
fetchedObjects : 8
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 3]
Inserting FetchedObjects at: [0, 3]
IndexPath: [0, 3]
fetchedObjects : 9
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 4]
Inserting FetchedObjects at: [0, 4]
IndexPath: [0, 4]
fetchedObjects : 10
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 4]
Inserting FetchedObjects at: [0, 4]
IndexPath: [0, 4]
fetchedObjects : 11
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 5]
Inserting FetchedObjects at: [0, 5]
IndexPath: [0, 5]
fetchedObjects : 12
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 5]
Inserting FetchedObjects at: [0, 5]
IndexPath: [0, 5]
fetchedObjects : 13
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 6]
Inserting FetchedObjects at: [0, 6]
IndexPath: [0, 6]
fetchedObjects : 14
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 6]
Inserting FetchedObjects at: [0, 6]
IndexPath: [0, 6]
fetchedObjects : 15
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 7]
Inserting FetchedObjects at: [0, 7]
IndexPath: [0, 7]
fetchedObjects : 16
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 7]
Inserting FetchedObjects at: [0, 7]
IndexPath: [0, 7]
fetchedObjects : 17
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 8]
Inserting FetchedObjects at: [0, 8]
IndexPath: [0, 8]
fetchedObjects : 18
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 8]
Inserting FetchedObjects at: [0, 8]
IndexPath: [0, 8]
fetchedObjects : 19
OK Saved Background
*** controller will change content
Insert an item newIndexPath : [0, 9]
Inserting FetchedObjects at: [0, 9]
IndexPath: [0, 9]
fetchedObjects : 20
OK Saved Background
Success
*** controller will change content
Insert an item newIndexPath : [0, 9]
Inserting FetchedObjects at: [0, 9]
IndexPath: [0, 9]
fetchedObjects : 21
OK Saved Background

任何想法为什么我在.insert(newIndexPath!)上复制indexPaths?

0 个答案:

没有答案