2016-02-05 21 views
6

Bir Varlığı, Çekirdek Veriler ile hızlıca biçimlendirmek için bir yol var mı? Bunun için aradım ama bir şey bulamadım.Öğe'yi programlı olarak oluşturma (Çekirdek Veriler)

+0

Tam olarak ne demek istiyorsun? Birini mi kastediyorsun? – Lubos

+0

@hantoren Bir kayıt eklemek mi istiyorsunuz? Veya bir varlığın kendisi mi yaratılsın? Çekirdek veriler bir nesne grafik yönetimidir ve .xcdatamodeld bence açıklamak için gereklidir. – Allen

+1

@Allen '.xcdatamodeld' gerekli değildir, bkz. [Kodda CoreData modeli oluşturma] (https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/). Tüm CoreData yapılandırmasını (modeller, varlıklar, vb.) Koddan yönetebilirsiniz. IMHO, erişim DB istediğiniz zaman yüklenen bir '.xcdatamodeld' dosyası sahip olmaktan çok daha iyi. – b1nary

cevap

12

sadece Web'de birkaç öğreticiler vardır (muhtemelen sadece one).

Xcode'un GUI araçlarının (Nibs, Storyboards, XCDataModeld, vb) bir hayranı değilim, bu nedenle koddaki her şeyi (DB'den UI'ye) oluşturmak benim için her zamanki şeydir. @Lubos tarafından atıfta bulunulan makaleye (yorum ekledikten 2 dakika sonra, hmm ...) ObjC'de yazılmıştır.

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 
} 

Bu kod (XCode en GUI oluşturulan), bu CD modeline eşittir:

GUI CoreData model

kodunda modelleri oluşturma çok daha edilmektedir

Yani, burada bir Swift koddur GUI kullanmaktan daha karmaşık.

Ancak IMO, modelinizi almak için CoreData model dosyasını yüklemekten daha hızlı ve güvenlidir (dosya yoksa ne olur veya dosya bozuksa ne olur?).

'Daha güvenli' ile diskten CoreData modelini okumakla ilgili disk IO hatalarını işlemek zorunda olmadığınız anlamına gelir (modeliniz koddadır, model dosyasına gerek yoktur). Ortalama CoreData kullanıcısı bir uygulamayı sonlandırmaktan daha kolay olduğu için bu hataların üstesinden gelmek istemiyor çünkü

İlgili konular