美文网首页
Core Data(Creating and Saving Ma

Core Data(Creating and Saving Ma

作者: Natus_Vincere | 来源:发表于2017-03-12 23:07 被阅读0次

    在定义了托管对象模型并初始化应用程序的Core Data的堆栈(stack)之后,你就可以开始为数据储存创建对象。

    创建托管对象(Creating Managed Objects)

    一个NSManagedObject的实例实现了Core Data对象模型所需的基本行为。NSManagedObject实例需要两个要素:一个是实体的描述(NSEntityDescription)和一个管理对象上下文(NSManagedObjectContext实例)。Entity的描述包括Entity的名称(name)、属性、和关系。托管对象上下文(managed context)代表你在其中创建托管对象的便签簿。上下文跟踪对象的变化以及对象之间的关系。

    如例所示,这个NSEntityDescripion类有一个类方法,这个类方法接受一个Entity的名称(name)和一个NSManagedObject实例将要关联的NSManagedObjectContext。这个示例定义返回的的对象是AAAEmployeeMO对象。
    例:

    Objective-C

    AAAEmployeeMO *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:[self managedObjectContext];
    

    Swift

    let employee = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext: managedObjectContext) as! EmployeeMO
    

    创建NSManagedObject的子类(Creating NSManagedObject SUbclasses)

    默认情况下,Core Data会在你的应用中返回一个NSManagedObject实例。无论怎样,在你的模型中为每一个Entity定义一个NSManagedObject是有用的。当你专门创建NSManagedObject的子类时,你可以定义一个用代码完成Entity的properties,你还可以为这些子类添加便利方法。

    在Xcode Core Data model editor创建一个NSManagedObject的子类,选中Entity,在模型检验器的Entity窗口中输入类的名称。然后在Xcode中创建子类。(To create a subclass of NSManagedObject, in the Xcode Core Data model editor, select the entity, and in the Entity pane of the Data Model inspector, enter the name in the Class field. Then create the subclass in Xcode.)

    Objective-C

    #import <CoreData/CoreData.h>
     
    @interface AAAEmployeeMO : NSManagedObject
     
    @property (nonatomic, strong) NSString *name;
     
    @end
     
    @implementation AAAEmployeeMO
     
    @dynamic name;
     
    @end
    

    The @dynamic tag informs the compiler that the variable will be resolved at runtime.

    Swift

    mport UIKit
    import CoreData
    import Foundation
     
    class EmployeeMO: NSManagedObject {
        
        @NSManaged var name: String?
        
    }
    

    在你的数据模型中定义子类并添加到项目中之后,可以直接在应用程序中引用它,并提高应用程序代码的可读性。

    保存NSManagedObject实例(Saving NSManagedObject Instances)

    创建的NSManagedObject实例并不保证其持久性,在托管对象模型的上下文创建NSManagedObject实例之后,通过保存上下文来讲这些更改保存到持久保存中。(Explicitly save that context to persist those changes to your persistent store.)

    Objective-C

    NSError *error = nil;
    if ([[self managedObjectContext] save:&error] == NO) {
        NSAssert(NO, @"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
    }
    

    Swift

    do {
        try managedObjectContext.save()
    } catch {
        fatalError("Failure to save context: \(error)")
    }
    

    在调用保存NSManagedObjectContext时会接收到一个NSError的变量,这个NSError的变量总是返回一个成功或者失败。如果保存失败,那么这个用以修正错误而展示(display)的错误条件(error condition)很重要。这个错误条件(error condition)的展示(display)可以像控制台输出错误一样简单,也可能像用户提供错误信息一样复杂(complicated)。如果保存成功,则不需要引用错误变量。

    相关文章

      网友评论

          本文标题:Core Data(Creating and Saving Ma

          本文链接:https://www.haomeiwen.com/subject/yjmxnttx.html