我有以下代理回调,它们可以正常工作
// MARK: delegate callbacks
func updateViewControllerDidCancel(updateViewController: UpdateViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func updateViewControllerDidUpdate(updateViewController: UpdateViewController, parts: NSMutableSet) {
self.dismissViewControllerAnimated(true, completion: nil)
Helper.updateSession(updateViewController.session, parts: parts)
self.fetchDB()
}
func createViewControllerDidCancel(createViewController: CreateViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func createViewControllerDidCreate(createViewController: CreateViewController, parts: NSMutableSet) {
self.dismissViewControllerAnimated(true, completion: nil)
Helper.createSessionForCurrentUserWithParts(parts, context: Helper.mainContext())
self.fetchDB()
}
创建会话正在运行。 更新会话工作温和,但重新启动后,更新消失。
以下是我设置UpdateViewController
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let session = sessions[indexPath.row]
let updateVC = Helper.vcWithIdentifier(vcUpdate) as! UpdateViewController
updateVC.delegate = self
updateVC.session = session
self.presentViewController(UINavigationController(rootViewController: updateVC), animated: true, completion: nil)
}
在UpdateViewController中,我像这样调用委托方法
@IBAction func update(sender: AnyObject) {
if selected.count > 0 {
self.delegate?.updateViewControllerDidUpdate(self, parts: selected)
} else {
Helper.alert(self, message: "you have to pick at least one part")
}
}
2个辅助方法是:
static func updateSession(session: Session, parts: NSSet) -> Void {
guard let context = session.managedObjectContext else {return}
session.parts = parts
do {
try context.save()
} catch let err as NSError {
print(err)
}
}
static func createSessionForCurrentUserWithParts(parts: NSSet, context: NSManagedObjectContext) -> Session? {
guard let user = currentUser else { return nil }
let session = NSEntityDescription.insertNewObjectForEntityForName("Session", inManagedObjectContext: context) as! Session
session.user = user
session.parts = parts
session.datetime = NSDate()
do {
try context.save()
} catch let err as NSError {
print(err)
}
return session
}
这就是context.save()
2016-05-23 16:34:08.844 CrazyGym[5229:970972] CoreData: sql: SELECT t0.Z_ENT, t0.Z_PK, Z_FOK_USER FROM ZSESSION t0 WHERE t0.ZUSER = ?
2016-05-23 16:34:08.844 CrazyGym[5229:970972] CoreData: annotation: sql connection fetch time: 0.0003s
2016-05-23 16:34:08.845 CrazyGym[5229:970972] CoreData: annotation: total fetch execution time: 0.0005s for 1 rows.
2016-05-23 16:34:08.845 CrazyGym[5229:970972] CoreData: annotation: to-many relationship fault "sessions" for objectID 0xd000000000040000 <x-coredata://4F69534D-B928-464B-8E9F-87329CB80C71/User/p1> fulfilled from database. Got 1 rows
2016-05-23 16:34:08.845 CrazyGym[5229:970972] CoreData: sql: BEGIN EXCLUSIVE
2016-05-23 16:34:08.845 CrazyGym[5229:970972] CoreData: sql: UPDATE ZSESSION SET Z_OPT = ? WHERE Z_PK = ? AND Z_OPT = ?
2016-05-23 16:34:08.845 CrazyGym[5229:970972] CoreData: sql: COMMIT
2016-05-23 16:34:08.846 CrazyGym[5229:970972] CoreData: sql: pragma page_count
2016-05-23 16:34:08.846 CrazyGym[5229:970972] CoreData: annotation: sql execution time: 0.0002s
2016-05-23 16:34:08.846 CrazyGym[5229:970972] CoreData: sql: pragma freelist_count
2016-05-23 16:34:08.846 CrazyGym[5229:970972] CoreData: annotation: sql execution time: 0.0001s
我只使用AppDelegate的主要上下文
// MARK: core data
static func coreDataStack() -> CoreDataStack {
return (UIApplication.sharedApplication().delegate as! AppDelegate).coreDataStack
}
static func mainContext() -> NSManagedObjectContext {
return coreDataStack().managedObjectContext
}