gpt4 book ai didi

ios - 来自父级的子 NSManagedObjectContext 更新

转载 作者:行者123 更新时间:2023-11-28 06:47:13 24 4
gpt4 key购买 nike

我想在父上下文更新后更新子上下文。

我有一个使用子上下文的 NSFetchedResultsController,然后我想通过它的委托(delegate)更新 UI - 我不确定这是否是完全正确的模式,这是我现在正在做的:

我创建了一个从支持我的模型的类中的 Web 服务更新的子上下文。这是一个简化的例子:

class Messages {

var pmoc: NSManagedObjectContext!
var delegate: MessagesDelegate?

init() {

let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
let moc = appDel.managedObjectContext

let pmoc = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
pmoc.parentContext = moc
self.pmoc = pmoc
}

func dataToUpdatePrivateContextReceived() {

// Add things to the private moc
self.pmoc.performBlock { () -> Void in

// Create new NSManagedOBject, etc.
self.savePMOC()
}
}

func savePMOC() {

self.pmoc.performBlock { () -> Void in

do {

try self.pmoc.save()
// save main context through an abstraction...
// Inform any delegate a save has taken place
self.delegate?.pmocSavedIntoMain()

} catch let error as NSError {

print("Save pmoc error :\(error.localizedDescription)")
}
}
}
}

protocol MessagesDelegate {
func pmocSavedIntoMain()
}

然后在某些 UIViewController 中,我使用 NSFetchedResultsController 来更新 UITableView,我试图将此 Controller 与它自己的私有(private)上下文一起使用,因此它的更新不会阻止 UI。另一个简化的例子:

class ViewController: UIViewController {

var fetchedResultsController: NSFetchedResultsController!
var viewPMOC: NSManagedObjectContext!
let messages = Messages()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

messages.delegate = self

let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
let moc = appDel.managedObjectContext

let pmoc = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
pmoc.parentContext = moc
self.viewPMOC = pmoc

let fr = NSFetchRequest(entityName: "MyEntity")
fr.fetchBatchSize = 20

let sort = NSSortDescriptor(key: "id", ascending: false)
fr.sortDescriptors = [sort]

self.fetchedResultsController = NSFetchedResultsController(fetchRequest: fr,
managedObjectContext: self.viewPMOC,
sectionNameKeyPath: nil,
cacheName: nil)

self.fetchedResultsController.delegate = self

do {

try self.fetchedResultsController.performFetch()

} catch let error as NSError {

print("vdl fetch error is: \(error.localizedDescription)")
}
}
}

extension ViewController: NSFetchedResultsControllerDelegate {

func controllerWillChangeContent(controller: NSFetchedResultsController) {
// dispatch begin updates on maind thread
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
// handle update type on main thread
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
// dispatch end upds on main thread
}
}

extension ViewController: MessagesDelegate {
func pmocSavedIntoMain() {
// what can I do here to get the child context to update from
//parent and thus trigger the fetched results controller to update the view?
}
}

最佳答案

获取的结果 Controller 需要一个主上下文。

使用这种模式来摆脱“断断续续”。

RootContext (private queue) - saves to persistent store
MainContext (main queue) child of RootContext - use for UI (FRC)
WorkerContext (private queue) - child of MainContext - use for updates & inserts

您的网络查询完成后,创建工作上下文并更新数据模型。当您保存时,更改将被推送到主上下文,您的 UI 应该通过 FRC 委托(delegate)更新。保存主上下文和根上下文以持久保存。

确保在处理子上下文时始终使用 block 方法 performBlockperformBlockAndWait

关于ios - 来自父级的子 NSManagedObjectContext 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com