2016-02-05 14 views
6

Gibt es eine Möglichkeit, eine Entity programmgesteuert auf Core Data mit swift2 zu erstellen? Ich suchte danach, aber ich habe nichts gefunden.Entität programmgesteuert erstellen (Core Data)

+0

Was genau meinen Sie? Du willst einen instanziieren? – Lubos

+0

@hantoren Meinst du einen Datensatz einfügen? Oder eine Entität selbst erstellen? Core-Daten sind ein Objekt Graph Management und .xcdatamodeld ist notwendig, um zu beschreiben, denke ich. – Allen

+1

@Allen '.xcdatamodeld' ist nicht erforderlich, siehe [CoreData-Modell im Code erstellen] (https://www.cocoaenetics.com/2012/04/creating-a-coredata-model-in-code/). Sie können die gesamte CoreData-Konfiguration (Modelle, Entitäten usw.) über Code verwalten. IMHO, das ist viel besser als eine '.xcdatamodeld'-Datei, die geladen wird, wenn Sie DB zugreifen möchten. – b1nary

Antwort

12

Es gibt nur ein paar Tutorials im Web (möglicherweise nur one).

Ich bin kein Fan von Xcode GUI-Tools (Nibs, Storyboards, XCDataModeld, etc), so dass alles (von DB bis UI) in Code zu erstellen, ist die übliche Sache für mich. Der von @Lubos referenzierte Artikel (2 Minuten nachdem ich einen Link dazu in Kommentare hinzugefügt habe, hmm ...) ist in ObjC geschrieben.

So, hier ist ein 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 
} 

Dieser Code ist gleich dieser CD-Modell (in Xcode GUI erstellt):

GUI CoreData model

Modelle in Code erstellen ist viel mehr Komplizierter als die Verwendung von GUI.

Aber IMO, es ist schneller und sicherer als Laden CoreData-Modell-Datei, um Ihr Modell (was, wenn keine Datei existiert? Oder die Datei ist beschädigt?).

Mit "sicherer" meine ich, dass Sie keine Disk-IO-Fehler im Zusammenhang mit dem Lesen von CoreData-Modell von der Festplatte behandeln müssen (Ihr Modell ist im Code, es gibt keine Notwendigkeit in der Modelldatei). Der durchschnittliche CoreData-Benutzer möchte diese Fehler einfach nicht behandeln, da es einfacher ist, eine Anwendung zu beenden.