Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Entity programmatically (Core Data)

Is there a way to create a Entity programmatically on Core Data with swift2? I searched for it, but I doesn't found something.

like image 383
hantoren Avatar asked Feb 05 '16 14:02

hantoren


People also ask

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

How do I set up NSManagedObject?

From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both class and properties files into your project.


2 Answers

There are only a few tutorials on the Web (possibly only one).

I am not a fan of Xcode's GUI tools (Nibs, Storyboards, XCDataModeld, etc), so creating everything (from DB to UI) in code is usual thing for me. The article referenced by @Lubos (2 minutes after I added a link to it in comments, hmm...) is written in ObjC.

So, here is a Swift code:

internal var _model: NSManagedObjectModel {
    let model = NSManagedObjectModel()

    // Create the entity
    let entity = NSEntityDescription()
    entity.name = "DTCachedFile"
    // Assume that there is a correct 
    // `CachedFile` managed object class.
    entity.managedObjectClassName = String(CachedFile)

    // Create the attributes
    var properties = Array<NSAttributeDescription>()

    let remoteURLAttribute = NSAttributeDescription()
    remoteURLAttribute.name = "remoteURL"
    remoteURLAttribute.attributeType = .StringAttributeType
    remoteURLAttribute.optional = false
    remoteURLAttribute.indexed = true
    properties.append(remoteURLAttribute)

    let fileDataAttribute = NSAttributeDescription()
    fileDataAttribute.name = "fileData"
    fileDataAttribute.attributeType = .BinaryDataAttributeType
    fileDataAttribute.optional = false
    fileDataAttribute.allowsExternalBinaryDataStorage = true
    properties.append(fileDataAttribute)

    let lastAccessDateAttribute = NSAttributeDescription()
    lastAccessDateAttribute.name = "lastAccessDate"
    lastAccessDateAttribute.attributeType = .DateAttributeType
    lastAccessDateAttribute.optional = false
    properties.append(lastAccessDateAttribute)

    let expirationDateAttribute = NSAttributeDescription()
    expirationDateAttribute.name = "expirationDate"
    expirationDateAttribute.attributeType = .DateAttributeType
    expirationDateAttribute.optional = false
    properties.append(expirationDateAttribute)

    let contentTypeAttribute = NSAttributeDescription()
    contentTypeAttribute.name = "contentType"
    contentTypeAttribute.attributeType = .StringAttributeType
    contentTypeAttribute.optional = true
    properties.append(contentTypeAttribute)

    let fileSizeAttribute = NSAttributeDescription()
    fileSizeAttribute.name = "fileSize"
    fileSizeAttribute.attributeType = .Integer32AttributeType
    fileSizeAttribute.optional = false
    properties.append(fileSizeAttribute)

    let entityTagIdentifierAttribute = NSAttributeDescription()
    entityTagIdentifierAttribute.name = "entityTagIdentifier"
    entityTagIdentifierAttribute.attributeType = .StringAttributeType
    entityTagIdentifierAttribute.optional = true
    properties.append(entityTagIdentifierAttribute)

    // Add attributes to entity
    entity.properties = properties

    // Add entity to model
    model.entities = [entity]

    // Done :]
    return model
}

This code is equal to this CD model (created in Xcode's GUI):

GUI CoreData model

Creating models in code is much more complicated than using GUI.

But, IMO, it is faster and safer than loading CoreData model file to get your model (what if no file exists? or the file is damaged?).

By 'safer' I mean that you don't have to handle disk IO errors related to reading CoreData model from disk (your model is in code, there is no need in model file). Average CoreData user just don't want to handle these errors because its easier to terminate an application

like image 139
tie Avatar answered Sep 28 '22 06:09

tie


It is possible to define core data model programmatically. I found a good example, though it is written in Objective C. I am sure it is working also for Swift 2. You just need to rewrite it. Should take a few minutes.

https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/

like image 21
Lubos Avatar answered Sep 28 '22 07:09

Lubos