Mac OSX应用程序:与NSArrayController相关的问题NSTableView核心数据添加记录

问题描述 投票:1回答:2

我使用NSArrayController NSTableViewCore data绑定。

我采取了一个button并将add:NSArrayController方法连接到它的行动。

在添加新记录

TableView补充并显示新纪录。 NSArrayController's add:方法叫做

问题:值未添加到核心数据(Sqlite类型)中。

应用程序重新启动显示旧数据。

objective-c cocoa core-data nstableview nsarraycontroller
2个回答
0
投票

这是一个苹果示例代码。基本上它会尝试在应用程序终止之前保存上下文。根据您的具体情况,您可以将功能移至其他位置。

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
    // Save changes in the application's managed object context before the application terminates.
    let context = persistentContainer.viewContext

    if !context.commitEditing() {
        NSLog("\(NSStringFromClass(type(of: self))) unable to commit editing to terminate")
        return .terminateCancel
    }

    if !context.hasChanges {
        return .terminateNow
    }

    do {
        try context.save()
    } catch {
        let nserror = error as NSError

        // Customize this code block to include application-specific recovery steps.
        let result = sender.presentError(nserror)
        if (result) {
            return .terminateCancel
        }

        let question = NSLocalizedString("Could not save changes while quitting. Quit anyway?", comment: "Quit without saves error question message")
        let info = NSLocalizedString("Quitting now will lose any changes you have made since the last successful save", comment: "Quit without saves error question info");
        let quitButton = NSLocalizedString("Quit anyway", comment: "Quit anyway button title")
        let cancelButton = NSLocalizedString("Cancel", comment: "Cancel button title")
        let alert = NSAlert()
        alert.messageText = question
        alert.informativeText = info
        alert.addButton(withTitle: quitButton)
        alert.addButton(withTitle: cancelButton)

        let answer = alert.runModal()
        if answer == .alertSecondButtonReturn {
            return .terminateCancel
        }
    }
    // If we got here, it is time to quit.
    return .terminateNow
}

0
投票

您可能缺少在NSArrayController上设置managedObjectContextentityName

© www.soinside.com 2019 - 2024. All rights reserved.