在coredata的父子上下文中面临“ automaticallyMergesChangesFromParent”问题吗?

时间:2018-10-29 02:36:05

标签: ios core-data parent-child

我自动遇到CoreDataMergesChangesFromParent属性的问题。首先让我告诉您上下文结构。 writerContext(privateQ)-> tempContext(PrivateQ-writerContext的孩子),viewContext(mainQ-writerContext的孩子),updateContext(PrivateQ-writerContext的孩子)。 我使“ automaticallyMergesChangesFromParent”选项可配置为viewContext。因为我想要用户是否希望对tempContext / updateContext进行的任何更改都可以合并到viewContext中,这可以通过'automaticallyMergesChangesFromParent'属性来完成。但是,当用户设置为“ automaticallyMergesChangesFromParent = false”时,这些更改不应反映在viewContext上。

代码:-

**但是我所面对的行为完全不同。 让我分享代码。 私人功能getViewContext()-> NSManagedObjectContext吗? {如果viewContext == nil && writerContext!= nil {让writerContext = coredataStack? print(“ viewContext.automaticallyMergesChangesFromParent:(viewContext.automaticallyMergesChangesFromParent)”)return viewContext}。

  • 在我的viewcontroller中,下面的代码是:-

func updateAllObjects(){如果让context = CoreDataManager.sharedInstance.getContext(type:OperationType.Other,screenID:“ FirstView”){context.perform {//让allObjects = User.fetch(in:context,includePending: true,returnObjectsAsFaults:false,谓词:nil,sortedWith:nil)for self.fetchedObjectIDS中的oID(让exObject = context.object(with:oID)as?用户exObject?.name =“ NewName” print(“更新的用户对象:(String(描述:exObject))”)} / 用于allObjects中的对象{object.name =“ NewName” print(“更新的用户对象: (字符串(描述:对象))“)} / CoreDataManager.sharedInstance.saveContext(上下文:上下文,opType:OperationType.Other,完成:{(在DispatchQueue.main.async中的错误)(执行:{自我。 coreDataTableView.reloadData()})CoreDataManager.sharedInstance.resetObjects(context:context)CoreDataManager.sharedInstance.disPoseTempContext(object:context,screenID:“ FirstView”,完成:{(isDisposed)在print(“ FirstView”的TempContext被处置) “)})}) }}} func fetchDataAndShow(withObjectsIds mObjectIds:[NSManagedObjectID]){CoreDataManager.sharedInstance.setShouldMegreBackGroundChangesInViewContext(value:true)如果让viewContext = CoreDataManager.sharedInstance.getContext(type:OperationType.UI,screenID:nil){viewContext.perform {自我中的弱自我]。coreDataTableView.delegate=自我self..coreDataTableView.dataSource =自我if(mObjectIds.count> 0){for mID中的oID {如果让exObject = viewContext.object(with:oID)as?用户{self?.managedObjects.append(exObject)print(“ exObject:(String(describing:exObject.name))”)}}} else {让allObjects = User.fetch(in:viewContext,includePending:true,returnObjectsAsFaults: true,谓词:nil,sortedWith:nil)self?.managedObjects.append(contentsOf:allObjects)} CoreDataManager.sharedInstance.resetObjects(context:viewContext)DispatchQueue.main.async(execute:{self?.coreDataTableView.reloadData()self ?.timer = Timer.scheduledTimer(withTimeInterval:5,重复:false,块:{(timer)in self?.updateAllObjects()})})}}}} func insertValues(){var counter = 20000 var SavingCounter = 0让名称=如果让上下文= CoreDataManager.sharedInstance.getContext(type:OperationType.Other,screenID:“ FirstView”){context.perform {[weak self] in while counter> 0 {counter = counter- 1 SavingCounter = SavingCounter + 1如果让objectTuple = User.insertObject(context:上下文,实体:userEntity),则让userEntity = User.entity(in:context){让newObject = objectTuple.object as?用户newObject?.name =“(name)(counter)” newObject?.age =“(age)(counter)” print(“ newObjects:-(String(describing:newObject))”)self?.fetchedObjectIDS.append( objectTuple.objectID)} if(counter == 0){print(“ savingCounter(savingCounter)”)print(“ counter(counter)”)CoreDataManager.sharedInstance.saveContext(context:context,opType:OperationType.Other){(错误)在if(error == nil){print(“ Data Saved”)} else {print(“ Error:(String(describing:error?.domain))”))print(“ Error:(String(describing:error? .errorCode))“)} DispatchQueue.main.async(执行: {self?.completionHandler((self?.fetchedObjectIDS)!)})if(计数器== 0){CoreDataManager.sharedInstance.resetObjects(context:context)CoreDataManager.sharedInstance.disPoseTempContext(object:context,screenID:“ FirstView”,完成:{(isDisposed)在print(“ FirstView'的TempContext被处置”)}}}}}}}}}}} **

0 个答案:

没有答案