在 CoreData 中设置值会使应用程序崩溃

Posted

技术标签:

【中文标题】在 CoreData 中设置值会使应用程序崩溃【英文标题】:Setting a value in CoreData crashes application 【发布时间】:2018-04-03 02:58:21 【问题描述】:

好的,所以我进行了大量搜索,我发现的所有内容似乎都与视图控制器有关,并且它们上的链接不起作用或未连接。那不是我的问题。我有一个应用程序,它使用 CoreData 在表格列表、表格编号、它们是否干净以及它们是否正在使用中保持标签。但是,当我尝试使用 setValue() 函数更改属性的键值时,它会使我的应用程序崩溃,并显示以下崩溃报告

2018-04-02 21:45:15.840265-0500 Trakr[26636:1455153] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSManagedObjectContext 0x6040001c4fb0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key inUse.'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010d1af1e6 __exceptionPreprocess + 294
1   libobjc.A.dylib                     0x0000000108f3d031 objc_exception_throw + 48
2   CoreFoundation                      0x000000010d1af0b9 -[NSException raise] + 9
3   Foundation                          0x000000010895eb47 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
4   Trakr                               0x0000000108615bee _T05Trakr24CheckTableViewControllerC18inUseSwitchChangedyypF + 846
5   Trakr                               0x0000000108615e41 _T05Trakr24CheckTableViewControllerC18inUseSwitchChangedyypFTo + 81
6   UIKit                               0x000000010ac1a448 -[UIApplication sendAction:to:from:forEvent:] + 83
7   UIKit                               0x000000010ad95804 -[UIControl sendAction:to:forEvent:] + 67
8   UIKit                               0x000000010ad95b21 -[UIControl _sendActionsForEvents:withEvent:] + 450
9   UIKit                               0x000000010b8d6d60 -[UISwitchModernVisualElement sendStateChangeActions] + 73
10  UIKit                               0x000000010b29889f -[UISwitchMVEGestureTrackingSession _sendStateChangeActionsIfNecessary] + 63
11  UIKit                               0x000000010b8d74ec -[UISwitchModernVisualElement _handleLongPressWithGestureLocationInBounds:gestureState:] + 760
12  UIKit                               0x000000010b21141b -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
13  UIKit                               0x000000010b21a1f0 _UIGestureRecognizerSendTargetActions + 109
14  UIKit                               0x000000010b217a38 _UIGestureRecognizerSendActions + 307
15  UIKit                               0x000000010b216c8c -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 859
16  UIKit                               0x000000010b1fbcf0 _UIGestureEnvironmentUpdate + 1329
17  UIKit                               0x000000010b1fb773 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
18  UIKit                               0x000000010b1fa875 -[UIGestureEnvironment _updateGesturesForEvent:window:] + 281
19  UIKit                               0x000000010ac9080b -[UIWindow sendEvent:] + 4064
20  UIKit                               0x000000010ac34370 -[UIApplication sendEvent:] + 352
21  UIKit                               0x000000010b57557f __dispatchPreprocessedEventFromEventQueue + 2796
22  UIKit                               0x000000010b578194 __handleEventQueueInternal + 5949
23  CoreFoundation                      0x000000010d151bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
24  CoreFoundation                      0x000000010d1364af __CFRunLoopDoSources0 + 271
25  CoreFoundation                      0x000000010d135a6f __CFRunLoopRun + 1263
26  CoreFoundation                      0x000000010d13530b CFRunLoopRunSpecific + 635
27  GraphicsServices                    0x000000010fa46a73 GSEventRunModal + 62
28  UIKit                               0x000000010ac190b7 UIApplicationMain + 159
29  Trakr                               0x000000010861e697 main + 55
30  libdyld.dylib                       0x000000010e318955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我终其一生都无法弄清楚为什么会发生这种情况。这是我的视图控制器类:

import UIKit
import CoreData

class CheckTableViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource 

@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var tableNumberLabel: UILabel!
@IBOutlet weak var cleanSwitch: UISwitch!
@IBOutlet weak var inUseSwitch: UISwitch!


var tablesList = [Table]()

override func viewDidLoad() 
    super.viewDidLoad()
    pickerView.delegate = self
    pickerView.dataSource = self

    let fetchRequest: NSFetchRequest<Table> = Table.fetchRequest()

    do 
        let tablesList = try PersistenceService.context.fetch(fetchRequest)
        self.tablesList = tablesList
     catch 

    backgroundImage.addBlurEffect()
    tableNumberLabel.text = "Select a Table"
    cleanSwitch.isEnabled = false
    inUseSwitch.isEnabled = false

    if tablesList.isEmpty 
        pickerView.isHidden = true
        tableNumberLabel.text = "Add Some Tables"
     else 
        pickerView.isHidden = false
    

    // Do any additional setup after loading the view.


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


// MARK: - Picker View and Switch Controlls
func numberOfComponents(in pickerView: UIPickerView) -> Int 
    return 1


func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return tablesList.count


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
    return String(tablesList[row].tableNumber)



func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    if (tablesList.isEmpty) 
        tableNumberLabel.text = "Add a table"
     else 
        tableNumberLabel.text = String(tablesList[row].tableNumber)
        if cleanSwitch.isEnabled == false 
            cleanSwitch.isEnabled = true
        

        if inUseSwitch.isEnabled == false 
            inUseSwitch.isEnabled = true
        

        if tablesList[row].isClean == true 
            cleanSwitch.isOn = true
         else 
            cleanSwitch.isOn = false
        

        if tablesList[row].inUse == true
            inUseSwitch.isOn = true
         else 
            inUseSwitch.isOn = false
        
    


@IBAction func cleanSwitchChanged(_ sender: Any) 
    if cleanSwitch.isOn == false 
        PersistenceService.context.setValue(false, forKey: "isClean")
     else 
        PersistenceService.context.setValue(true, forKey: "isClean")
    


@IBAction func inUseSwitchChanged(_ sender: Any) 
    if inUseSwitch.isOn == false 
        let selectedRow = pickerView.selectedRow(inComponent: 0)
        tablesList[selectedRow].inUse = false
        PersistenceService.context.setValue(false, forKey: "inUse")

     else 
        let selectedRow = pickerView.selectedRow(inComponent: 0)
        tablesList[selectedRow].inUse = true
        PersistenceService.context.setValue(true, forKey: "inUse")
    



这是我的 PersistenceService 类,负责 CoreData 功能的类

import Foundation
import CoreData

class PersistenceService 
// MARK: - Core Data stack

private init() 

static var context: NSManagedObjectContext 
    return persistentContainer.viewContext


static var persistentContainer: NSPersistentContainer = 
    /*
     The persistent container for the application. This implementation
     creates and returns a container, having loaded the store for the
     application to it. This property is optional since there are legitimate
     error conditions that could cause the creation of the store to fail.
     */
    let container = NSPersistentContainer(name: "tableModel")
    container.loadPersistentStores(completionHandler:  (storeDescription, error) in
        if let error = error as NSError? 
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

            /*
             Typical reasons for an error here include:
             * The parent directory does not exist, cannot be created, or disallows writing.
             * The persistent store is not accessible, due to permissions or data protection when the device is locked.
             * The device is out of space.
             * The store could not be migrated to the current model version.
             Check the error message to determine what the actual problem was.
             */
            fatalError("Unresolved error \(error), \(error.userInfo)")
        
    )
    return container
()

// MARK: - Core Data Saving support

static func saveContext () 
    let context = persistentContainer.viewContext
    if context.hasChanges 
        do 
            try context.save()
            print("Saved to CoreData")
         catch 
            // Replace this implementation with code to handle the error appropriately.
            // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        
    


最后,a screenshot of my tableModel CoreData Model

谁能告诉我为什么这不起作用?我这辈子都无法理解....

【问题讨论】:

这应该是 entity instance 而不是 NSManagedObjectContext instance 您设置值的位置。 【参考方案1】:

您应该创建 NSManagedObject 的 Table 子类,而不是使用 KVC。然后你可以直接在“Table”实例中设置值。

另外,您将值设置为context.setValue(:),您应该首先创建一个Table 的实体描述,如下所示

var newTable = NSEntityDescription.insertNewObject(forEntityName: "Table", into: PersistenceService.context)
newTable.setValue(fale, forKey: "isClean")

【讨论】:

谢谢!但是,我还发现调用 PersistenceService.saveContext() 实际上是在保存数据,尽管据我所知,我还没有修改上下文。这是怎么回事?

以上是关于在 CoreData 中设置值会使应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

jquery.inputmask - 在“oncomplete”回调中设置值会导致不正确的操作

使用三个相关的 NSPopUpButton 在 CoreData 中设置值

即使在删除之后,Core Data 命令也会使 ViewController 崩溃

UITableView 行删除崩溃应用

是否可以在没有设置器的情况下在属性中设置值?

在 OpenXml 复选框 word2013 中设置值