-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathExampleModel.swift
More file actions
138 lines (109 loc) 路 5.68 KB
/
Copy pathExampleModel.swift
File metadata and controls
138 lines (109 loc) 路 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// ExampleModel.swift
// Example
//
// Programmatic Core Data model definition
//
import Foundation
import CoreData
extension NSPersistentContainer {
convenience init(name: String) {
let model = NSManagedObjectModel()
// Person Entity
let personEntity = NSEntityDescription()
personEntity.name = "Person"
personEntity.managedObjectClassName = NSStringFromClass(NSManagedObject.self)
let nameAttribute = NSAttributeDescription()
nameAttribute.name = "name"
nameAttribute.attributeType = .stringAttributeType
nameAttribute.isOptional = false
let ageAttribute = NSAttributeDescription()
ageAttribute.name = "age"
ageAttribute.attributeType = .integer32AttributeType
ageAttribute.isOptional = false
let birthDateAttribute = NSAttributeDescription()
birthDateAttribute.name = "birthDate"
birthDateAttribute.attributeType = .dateAttributeType
birthDateAttribute.isOptional = true
let isActiveAttribute = NSAttributeDescription()
isActiveAttribute.name = "isActive"
isActiveAttribute.attributeType = .booleanAttributeType
isActiveAttribute.isOptional = false
isActiveAttribute.defaultValue = true
personEntity.properties = [nameAttribute, ageAttribute, birthDateAttribute, isActiveAttribute]
// Address Entity
let addressEntity = NSEntityDescription()
addressEntity.name = "Address"
addressEntity.managedObjectClassName = NSStringFromClass(NSManagedObject.self)
let streetAttribute = NSAttributeDescription()
streetAttribute.name = "street"
streetAttribute.attributeType = .stringAttributeType
streetAttribute.isOptional = false
let cityAttribute = NSAttributeDescription()
cityAttribute.name = "city"
cityAttribute.attributeType = .stringAttributeType
cityAttribute.isOptional = false
let zipCodeAttribute = NSAttributeDescription()
zipCodeAttribute.name = "zipCode"
zipCodeAttribute.attributeType = .stringAttributeType
zipCodeAttribute.isOptional = true
addressEntity.properties = [streetAttribute, cityAttribute, zipCodeAttribute]
// Task Entity
let taskEntity = NSEntityDescription()
taskEntity.name = "Task"
taskEntity.managedObjectClassName = NSStringFromClass(NSManagedObject.self)
let titleAttribute = NSAttributeDescription()
titleAttribute.name = "title"
titleAttribute.attributeType = .stringAttributeType
titleAttribute.isOptional = false
let taskDescriptionAttribute = NSAttributeDescription()
taskDescriptionAttribute.name = "taskDescription"
taskDescriptionAttribute.attributeType = .stringAttributeType
taskDescriptionAttribute.isOptional = true
let isCompletedAttribute = NSAttributeDescription()
isCompletedAttribute.name = "isCompleted"
isCompletedAttribute.attributeType = .booleanAttributeType
isCompletedAttribute.isOptional = false
isCompletedAttribute.defaultValue = false
let createdAtAttribute = NSAttributeDescription()
createdAtAttribute.name = "createdAt"
createdAtAttribute.attributeType = .dateAttributeType
createdAtAttribute.isOptional = false
taskEntity.properties = [titleAttribute, taskDescriptionAttribute, isCompletedAttribute, createdAtAttribute]
// Relationships
let personAddressRelationship = NSRelationshipDescription()
personAddressRelationship.name = "address"
personAddressRelationship.destinationEntity = addressEntity
personAddressRelationship.minCount = 0
personAddressRelationship.maxCount = 1
personAddressRelationship.deleteRule = .cascadeDeleteRule
let addressPersonRelationship = NSRelationshipDescription()
addressPersonRelationship.name = "person"
addressPersonRelationship.destinationEntity = personEntity
addressPersonRelationship.minCount = 0
addressPersonRelationship.maxCount = 1
addressPersonRelationship.deleteRule = .nullifyDeleteRule
personAddressRelationship.inverseRelationship = addressPersonRelationship
addressPersonRelationship.inverseRelationship = personAddressRelationship
personEntity.properties.append(personAddressRelationship)
addressEntity.properties.append(addressPersonRelationship)
let personTasksRelationship = NSRelationshipDescription()
personTasksRelationship.name = "tasks"
personTasksRelationship.destinationEntity = taskEntity
personTasksRelationship.minCount = 0
personTasksRelationship.maxCount = 0
personTasksRelationship.deleteRule = .cascadeDeleteRule
let taskPersonRelationship = NSRelationshipDescription()
taskPersonRelationship.name = "person"
taskPersonRelationship.destinationEntity = personEntity
taskPersonRelationship.minCount = 0
taskPersonRelationship.maxCount = 1
taskPersonRelationship.deleteRule = .nullifyDeleteRule
personTasksRelationship.inverseRelationship = taskPersonRelationship
taskPersonRelationship.inverseRelationship = personTasksRelationship
personEntity.properties.append(personTasksRelationship)
taskEntity.properties.append(taskPersonRelationship)
model.entities = [personEntity, addressEntity, taskEntity]
self.init(name: name, managedObjectModel: model)
}
}