2016-02-05 7 views
6

क्या स्विफ्ट 2 के साथ कोर डेटा पर प्रोग्रामेटिक रूप से एक इकाई बनाने का कोई तरीका है? मैंने इसकी खोज की, लेकिन मुझे कुछ नहीं मिला।एंटिटी प्रोग्रामेटिकली (कोर डेटा)

+0

एप्लिकेशन को समाप्त करना आसान है, तो आपका क्या मतलब है? आप एक को तत्काल करने का मतलब है? – Lubos

+0

@hantoren क्या आपका मतलब एक रिकॉर्ड डालना है? या एक इकाई खुद बनाओ? कोर डेटा एक ऑब्जेक्ट ग्राफ़ प्रबंधन है और मुझे लगता है कि वर्णन करने के लिए .xcdatamodeld आवश्यक है। – Allen

+1

@Allen '.xcdatamodeld' आवश्यक नहीं है, [कोड में कोरडाटा मॉडल बनाना] देखें (https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/)। आप कोड से पूरे कोरडाटा कॉन्फ़िगरेशन (मॉडल, इकाइयां, आदि) का प्रबंधन कर सकते हैं। आईएमएचओ, जब आप डीबी एक्सेस करना चाहते हैं तो लोड की गई '.xcdatamodeld' फ़ाइल होने से कहीं अधिक बेहतर है। – b1nary

उत्तर

3

कोर डेटा मॉडल प्रोग्रामेटिक रूप से परिभाषित करना संभव है। मुझे एक अच्छा उदाहरण मिला, हालांकि यह उद्देश्य सी में लिखा गया है। मुझे यकीन है कि यह स्विफ्ट 2 के लिए भी काम कर रहा है। आपको बस इसे फिर से लिखना होगा। कुछ मिनट लेना चाहिए।

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

12

वहाँ केवल वेब पर कुछ ट्यूटोरियल हैं (संभवतः केवल one)।

मैं एक्सकोड के जीयूआई उपकरण (निब्स, स्टोरीबोर्ड, एक्ससीडीएटा मॉडेल, इत्यादि) का प्रशंसक नहीं हूं, इसलिए कोड में सबकुछ (डीबी से यूआई तक) बनाना मेरे लिए सामान्य बात है। @Lubos द्वारा संदर्भित लेख (टिप्पणियों में इसे जोड़ने के 2 मिनट बाद, हम्म ...) ओबीजेसी में लिखा गया है।

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 
} 

इस कोड (Xcode के जीयूआई में बनाया गया) इस सीडी मॉडल के बराबर है:

GUI CoreData model

कोड में मॉडल बनाना भी बहुत कुछ है

तो, यहाँ एक स्विफ्ट कोड है जीयूआई का उपयोग करने से जटिल।

लेकिन, आईएमओ, यह आपके मॉडल को प्राप्त करने के लिए कोरडाटा मॉडल फ़ाइल लोड करने से तेज़ और सुरक्षित है (यदि कोई फ़ाइल मौजूद नहीं है या फ़ाइल क्षतिग्रस्त है?)।

'सुरक्षित' से मेरा मतलब है कि डिस्क से कोरडाटा मॉडल पढ़ने से संबंधित डिस्क IO त्रुटियों को संभालने की आवश्यकता नहीं है (आपका मॉडल कोड में है, मॉडल फ़ाइल में कोई आवश्यकता नहीं है)। औसत कोरडाटा उपयोगकर्ता सिर्फ इन त्रुटियों को संभालना नहीं चाहता क्योंकि

संबंधित मुद्दे