【问题标题】:Swift: CoreData – managedObjectContext-ErrorSwift:CoreData – managedObjectContext-Error
【发布时间】:2016-02-03 23:35:16
【问题描述】:

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

当我的应用程序尝试使用函数 addNewContact(在 viewDidLoad() 中进行测试时就是这种情况)时,它会崩溃。 我能够将错误跟踪到默认的 AppDelegate 类,因为每次我尝试声明我的 managedContext 时,都不会执行 print("2")-statement,但在我声明之前的 print("1")-statement 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

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

顺便说一句,这是我的 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) 

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

【问题讨论】:

  • 记录了什么错误?

标签: ios swift core-data error-handling


【解决方案1】:

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

 NSCocoaErrorDomain Code=134100

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

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

【讨论】:

  • 嘿,非常感谢!我对 iOS 开发很陌生,错误处理不是我的强项(还 :))。工作得很好!
猜你喜欢
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多