gpt4 book ai didi

swift - 添加新联系人后未调用controllerDidChangeContent

转载 作者:行者123 更新时间:2023-11-30 10:04:32 25 4
gpt4 key购买 nike

基本上看起来我的 NSFetchedResultsController 没有检测到更改,所以我什至不认为在这里创建的新对象正在被保存。

else if segue.identifier == "AddContact" {
let navigationController = segue.destinationViewController as! UINavigationController
let addViewController = navigationController.topViewController as! NewContactViewController
let contactEntity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: context)
let newContact = Contact(entity: contactEntity!, insertIntoManagedObjectContext: context)

addViewController.contact = newContact
addViewController.context = newContact.managedObjectContext
addViewController.delegate = self
}

用户按“完成”返回到联系人 ListView 后,将根据我创建的委托(delegate)调用此函数。

func didFinishViewController(viewController:NewContactViewController, didSave:Bool) {

// 1
if didSave {
// 2
let context = viewController.context
context.performBlock({ () -> Void in
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
print("Error: \(nserror.localizedDescription)")
abort()
}
}

// 3
self.coreDataStack.saveContext()
})
}

在检查上下文数百万次后,它们始终匹配,因此推送工作正常,但似乎正在记录更改。 CoreDataStack.swift 就是这样。

//exablishes the directory to be used for the stack in SQLite fashion
private lazy var applicationDocumentsDirectory: NSURL = {
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
return urls[urls.count-1]
}()

//establishes the object model that interacts with the GUI object data model
private lazy var managedObjectModel: NSManagedObjectModel = {
let modelURL = NSBundle.mainBundle().URLForResource(self.modelName, withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

//establishes the coordinator that connects the managed context with the persistent store
private lazy var psc: NSPersistentStoreCoordinator = { let coordinator = NSPersistentStoreCoordinator(
managedObjectModel: self.managedObjectModel)

let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(self.modelName + ".sqlite")

do {
let options = [NSMigratePersistentStoresAutomaticallyOption : true]

try coordinator.addPersistentStoreWithType( NSSQLiteStoreType, configuration: nil, URL: url, options: nil) //establishes the actual persistent store
} catch {
print("Error adding persistent store.")
}
return coordinator
}()


//establishes the managed context by which managed objects interact in reading and writing
lazy var context: NSManagedObjectContext = {
var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = self.psc
return managedObjectContext
}()

func saveContext () {
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
print("Error: \(nserror.localizedDescription)")
abort()
}
}
}

NSFetchedResultsController 实现

// MARK: Fetched Results controller
func contactListFetchedResultsController()-> NSFetchedResultsController {

fetchedResultController = NSFetchedResultsController(fetchRequest: contactListFetchRequest(), managedObjectContext: coreDataStack.context, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultController.delegate = self

do {
try fetchedResultController.performFetch()
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
abort()
}

return fetchedResultController
}

func contactListFetchRequest() -> NSFetchRequest {

let fetchRequest = NSFetchRequest(entityName: "Contact")
fetchRequest.fetchBatchSize = 20

let sortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)

fetchRequest.sortDescriptors = [sortDescriptor]

return fetchRequest
}

最佳答案

当你已经在 UI 线程上时,为什么还要有 context.performBlock ?您正在处理不止一种环境吗?

假设您使用多个上下文,您将在子上下文之前将主上下文保存为 performBlock(),同时使子上下文在之后保存调用 self.coreDataStack.saveContext()。

由于这是所有与 UI 相关的代码,因此您最好只使用整个 UI 的主队列上下文,并且我怀疑您的 NSFetchedResultsController 将开始接收通知。

关于swift - 添加新联系人后未调用controllerDidChangeContent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36816872/

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