NSFetchedResultsController并不总是更新

时间:2018-02-14 14:31:07

标签: ios core-data nsfetchedresultscontroller nsmanagedobjectcontext

我有一个"谷歌驱动器"应用程序,每次我进入文件夹我都有NSFetchedResultsController,用" parentId"获取实体参数。

我的NSFetchedResultsController包装器:

class KnowledgeFetchedResultsController<T : KnowledgeBaseEntity> {

    private var controllerContex : NSManagedObjectContext? = appDelegate.coreDataContexts.managedObjectContext

    init(controllerTag: String,cacheName:String?, sortDescriptors: [NSSortDescriptor],predicate : NSPredicate? = nil){
        super.init()
        self.controllerTag = controllerTag

        let fetchRequest = T.getFetchRequest()
        fetchRequest.sortDescriptors = sortDescriptors
        fetchRequest.predicate = predicate
        self.fetchedResultsController = NSFetchedResultsController(fetchRequest:  fetchRequest, managedObjectContext: self.controllerContex!, sectionNameKeyPath: nil , cacheName: cacheName)
        self.fetchedResultsController?.delegate = self
    }

    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        self.delegate?.onFetchedResultsUpdate(tag: self.controllerTag)
    }
}

onFetchedResultsUpdate委托正在调用tableView.reloadData()

我的ViewController:

class KnowledgeBaseViewController : UIViewController,UITableViewDelegate, NSFetchedResultsControllerDelegate {

     private var knowledgeRepoController : KnowledgeRepoController?

     override func viewDidLoad() {
         knowledgeRepoController = KnowledgeRepoControllerFactory.BaseItemsRepoController(parentId: self.parentId)
         knowledgeRepoController?.repoDelegate = self
         }

    func onFetchedResultsUpdate( tag: String) {
        self.tableView.reloadData()
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = ...// get object
        if item?.entityType != KnowledgeType.folder {
            return
        }
        let controller = UIStoryboard.knowledgeBaseController(parentId: item?.id)
        self.navigationController?.pushViewController(controller, animated: true)
    }
}

我的背景:

lazy var managedObjectContext: NSManagedObjectContext? =
    {
        // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
        let coordinator = self.store?.persistentStoreCoordinator
        if coordinator == nil {
            return nil
        }
        var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)
        managedObjectContext.persistentStoreCoordinator = coordinator
        managedObjectContext.stalenessInterval = 0
        return managedObjectContext
}()

func contextDidSaveContext(notification: NSNotification) {
    if let sender = notification.object as? NSManagedObjectContext {
        if sender === self.managedObjectContext {
            self.backgroundContext?.perform {
                self.backgroundContext?.mergeChanges(fromContextDidSave: notification as Notification)
            }
        } else if sender === self.backgroundContext {
            self.managedObjectContext?.perform {
                self.managedObjectContext?.mergeChanges(fromContextDidSave: notification as Notification)
            }
        } else {
            self.backgroundContext?.perform {
                self.backgroundContext?.mergeChanges(fromContextDidSave: notification as Notification)
            }
            self.managedObjectContext?.perform {
                self.managedObjectContext?.mergeChanges(fromContextDidSave: notification as Notification)
            }
        }
    }
}

在第一个视图控制器中,所有工作正常, controllerDidChangeContent 正在调用,并且所有内容都已更新。

但是!当我进入第二个视图控制器时,NSFetchedResultsController不会更新 controllerDidChangeContent 不会调用。

我不知道如何修复它,我在互联网上尝试每一个答案

0 个答案:

没有答案