क्या स्विफ्ट 2 के साथ कोर डेटा पर प्रोग्रामेटिक रूप से एक इकाई बनाने का कोई तरीका है? मैंने इसकी खोज की, लेकिन मुझे कुछ नहीं मिला।एंटिटी प्रोग्रामेटिकली (कोर डेटा)
उत्तर
कोर डेटा मॉडल प्रोग्रामेटिक रूप से परिभाषित करना संभव है। मुझे एक अच्छा उदाहरण मिला, हालांकि यह उद्देश्य सी में लिखा गया है। मुझे यकीन है कि यह स्विफ्ट 2 के लिए भी काम कर रहा है। आपको बस इसे फिर से लिखना होगा। कुछ मिनट लेना चाहिए।
https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/
वहाँ केवल वेब पर कुछ ट्यूटोरियल हैं (संभवतः केवल 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 के जीयूआई में बनाया गया) इस सीडी मॉडल के बराबर है:
कोड में मॉडल बनाना भी बहुत कुछ है
तो, यहाँ एक स्विफ्ट कोड है जीयूआई का उपयोग करने से जटिल।
लेकिन, आईएमओ, यह आपके मॉडल को प्राप्त करने के लिए कोरडाटा मॉडल फ़ाइल लोड करने से तेज़ और सुरक्षित है (यदि कोई फ़ाइल मौजूद नहीं है या फ़ाइल क्षतिग्रस्त है?)।
'सुरक्षित' से मेरा मतलब है कि डिस्क से कोरडाटा मॉडल पढ़ने से संबंधित डिस्क IO त्रुटियों को संभालने की आवश्यकता नहीं है (आपका मॉडल कोड में है, मॉडल फ़ाइल में कोई आवश्यकता नहीं है)। औसत कोरडाटा उपयोगकर्ता सिर्फ इन त्रुटियों को संभालना नहीं चाहता क्योंकि
- 1. मैं कोर डेटा एंटिटी
- 2. एंटिटी फ्रेमवर्क कोर
- 3. एंटिटी फ्रेमवर्क कोर
- 4. एंटिटी फ्रेमवर्क कोर
- 5. अमेज़ॅन एस 3 प्रोग्रामेटिकली एक्सेस उपयोग डेटा
- 6. एंटिटी फ्रेमवर्क कोर और मल्टीथ्रेडिंग
- 7. मूल डेटा को सूचित करें जब बच्चे डेटा रिलेशनशिप एंटिटी कोर डेटा
- 8. एक्समरिन फॉर्म और एंटिटी फ्रेमवर्क कोर नेटस्टैन्डर्ड
- 9. एंटिटी डेटा मॉडल विज़ार्ड
- 10. एंटिटी डेटा मॉडल
- 11. कोर डेटा iCloud सिंक्रनाइज़ेशन
- 12. यूआईएसविच: स्विफ्ट 3: प्रोग्रामेटिकली
- 13. कोर डेटा
- 14. कोर डेटा
- 15. कोर डेटा
- 16. कोर डेटा
- 17. कोर डेटा
- 18. कोर डेटा
- 19. कोर डेटा
- 20. कोर डेटा
- 21. कोर डेटा
- 22. कोर डेटा -
- 23. कोर डेटा
- 24. कोर-डेटा
- 25. कोर डेटा
- 26. कोर डेटा
- 27. कोर डेटा
- 28. कोर डेटा
- 29. कोर डेटा
- 30. कोर डेटा
एप्लिकेशन को समाप्त करना आसान है, तो आपका क्या मतलब है? आप एक को तत्काल करने का मतलब है? – Lubos
@hantoren क्या आपका मतलब एक रिकॉर्ड डालना है? या एक इकाई खुद बनाओ? कोर डेटा एक ऑब्जेक्ट ग्राफ़ प्रबंधन है और मुझे लगता है कि वर्णन करने के लिए .xcdatamodeld आवश्यक है। – Allen
@Allen '.xcdatamodeld' आवश्यक नहीं है, [कोड में कोरडाटा मॉडल बनाना] देखें (https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/)। आप कोड से पूरे कोरडाटा कॉन्फ़िगरेशन (मॉडल, इकाइयां, आदि) का प्रबंधन कर सकते हैं। आईएमएचओ, जब आप डीबी एक्सेस करना चाहते हैं तो लोड की गई '.xcdatamodeld' फ़ाइल होने से कहीं अधिक बेहतर है। – b1nary