support compound indexes and unique constraints on CoreStore properties

This commit is contained in:
John Rommel Estropia
2018-03-17 23:56:42 +09:00
parent 0c29e07ddb
commit 7b1075b759
9 changed files with 159 additions and 105 deletions

View File

@@ -292,7 +292,6 @@ public final class CoreStoreSchema: DynamicSchema {
description.name = attribute.keyPath
description.attributeType = Swift.type(of: attribute).attributeType
description.isOptional = attribute.isOptional
description.isIndexed = attribute.isIndexed
description.defaultValue = attribute.defaultValue()
description.isTransient = attribute.isTransient
description.allowsExternalBinaryDataStorage = attribute.allowsExternalBinaryDataStorage
@@ -442,6 +441,53 @@ public final class CoreStoreSchema: DynamicSchema {
entityDescription: entityDescription
)
}
for (entity, entityDescription) in entityDescriptionsByEntity {
let uniqueConstraints = entity.uniqueConstraints.filter({ !$0.isEmpty })
if !uniqueConstraints.isEmpty {
CoreStore.assert(
entityDescription.superentity == nil,
"Uniqueness constraints must be defined at the highest level possible."
)
entityDescription.uniquenessConstraints = entity.uniqueConstraints.map { $0.map { $0 as NSString } }
}
guard !entity.indexes.isEmpty else {
continue
}
defer {
entityDescription.coreStoreEntity = entity // reserialize
}
let attributesByName = entityDescription.attributesByName
if #available(iOS 11.0, OSX 10.13, watchOS 4.0, tvOS 11.0, *) {
entityDescription.indexes = entity.indexes.map { (compoundIndexes) in
return NSFetchIndexDescription.init(
name: "_CoreStoreSchema_indexes_\(entityDescription.name!)_\(compoundIndexes.joined(separator: "-"))",
elements: compoundIndexes.map { (keyPath) in
return NSFetchIndexElementDescription(
property: attributesByName[keyPath]!,
collationType: .binary
)
}
)
}
}
else {
entityDescription.compoundIndexes = entity.indexes.map { (compoundIndexes) in
return compoundIndexes.map { (keyPath) in
return attributesByName[keyPath]!
}
}
}
}
}
private static func fourthPassSynthesizeManagedObjectClasses(for entityDescriptionsByEntity: [DynamicEntity: NSEntityDescription], allCustomGettersSetters: [DynamicEntity: [KeyPathString: CoreStoreManagedObject.CustomGetterSetter]]) {