Swift:CoreData - managedObjectContext-Error

时间:2016-02-03 23:35:17

标签: ios swift core-data error-handling

在我想要实现CoreData之后,我的应用程序中出现了一个错误。我希望你们能帮助我。

当我的应用程序尝试使用addNewContact函数(在viewDidLoad()中进行测试时),它会崩溃。 我能够将错误跟踪到默认的AppDelegate类,因为每次我尝试声明我的managedContext时,都不会执行print(“2”) - 语句,而是在我声明之前执行print(“1”) - 语句managedContext是。

首先,这是我的函数代码:

//MARK: - Add contact to CoreData
func addNewContact()
{
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    print("1")
    let managedContext = appDelegate.managedObjectContext
    print("2")

    let entity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: managedContext)

    let contact = Contact(entity: entity!, insertIntoManagedObjectContext: managedContext)
    contact.firstName = "testName"
    contact.lastName = "testLastName"
    contact.eMail = "testMail"
    contact.number = "testNumber"

    do {
        try managedContext.save()
        print("contact \(contact.firstName, contact.lastName) saved succesfully")
    } catch {
        fatalError("Error while trying to save \(contact.firstName, contact.firstName)")
    }
}

现在是默认的AppDelegate.swift,错误如下: AppDelegate.swift with error

我真的不知道我做错了什么。我希望你能帮助我。 非常感谢你。

BTW以下是我的CoreData-Entity 联系人的代码:

class Contact: NSManagedObject
{

// Insert code here to add functionality to your managed object     subclass
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?)
{
    super.init(entity: entity, insertIntoManagedObjectContext: context)
}
}

import Foundation
import CoreData

extension Contact {

@NSManaged var firstName: String?
@NSManaged var number: String?
@NSManaged var eMail: String?
@NSManaged var lastName: String?

}

编辑: 我的控制台输出:

1
2016-02-04 01:02:03.370 Lime[8326:9552995] CoreData: error: -        addPersistentStoreWithType:SQLite configuration:(null)    URL:file:///Users/Raphael/Library/Developer/CoreSimulator/Devices/CCAE776D-    A906-4D46-BBE2-9DC3E4E70F01/data/Containers/Data/Application/CDC77942-    8076-459E-9903-AEA97BEE6BA4/Documents/SingleViewCoreData.sqlite options:  (null) ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "   (null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 641;
    NSStoreModelVersionHashes =     {
        Contact = <e97b3c3f 518adb38 fa59ff9d a6f5972d fc5d011f   b40e72e7 37b1c87b afd1ef28>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "D526A5C9-30C1-4F07-AB7A-C7F251058E22";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one      used to create the store} with userInfo dictionary {
    metadata =     {
        NSPersistenceFrameworkVersion = 641;
        NSStoreModelVersionHashes =         {
            Contact = <e97b3c3f 518adb38 fa59ff9d a6f5972d fc5d011f b40e72e7 37b1c87b afd1ef28>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "D526A5C9-30C1-4F07-AB7A-C7F251058E22";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the  one used to create the store";
}
2016-02-04 01:02:03.373 Lime[8326:9552995] Unresolved error Error     Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's    saved data" UserInfo={NSLocalizedDescription=Failed to initialize the application's saved data, NSUnderlyingError=0x7b6f5430 {Error   Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={
    NSPersistenceFrameworkVersion = 641;
    NSStoreModelVersionHashes =     {
        Contact = <e97b3c3f 518adb38 fa59ff9d a6f5972d fc5d011f b40e72e7 37b1c87b afd1ef28>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "D526A5C9-30C1-4F07-AB7A-C7F251058E22";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one   used to create the store}}, NSLocalizedFailureReason=There was an error   creating or loading the application's saved data.},     [NSLocalizedDescription: Failed to initialize the application's saved data,     NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134100 "(null)"     UserInfo={metadata={
    NSPersistenceFrameworkVersion = 641;
    NSStoreModelVersionHashes =     {
        Contact = <e97b3c3f 518adb38 fa59ff9d a6f5972d fc5d011f b40e72e7 37b1c87b afd1ef28>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "D526A5C9-30C1-4F07-AB7A-C7F251058E22";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one    used to create the store}, NSLocalizedFailureReason: There was an error   creating or loading the application's saved data.]
(lldb) 

不知怎的,它不会显示为代码。多次尝试。对不起:/。

1 个答案:

答案 0 :(得分:0)

重要的错误是控制台输出顶部的错误,部分是......

 NSCocoaErrorDomain Code=134100

此错误告诉您模型已更改,因此现在与商店不兼容。有两种方法可以解决这个问题:

  1. 简单的方法是从设备中删除应用程序。因此,当您再次运行它时,将没有存储导致不兼容。这是您在开发模型时开发核心数据应用程序时所做的(很多)。
  2. 如果您分发应用程序,您需要做的就是执行迁移,将商店从旧模型升级到新模式。
相关问题