Skip to content

Commit

Permalink
Deprecate model initializers
Browse files Browse the repository at this point in the history
Too easy to misuse, instead us the `model(for:)` static
function, which is a little more secure (but will still
fail on different type sets reusing the same types).
  • Loading branch information
helje5 committed Oct 1, 2023
1 parent 3f9eb1f commit f9eb134
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public extension NSManagedObjectModel {
// - encodingVersion
// - version

@available(*, deprecated, renamed: "model(for:)", message:
"""
Entities can only be used in one NSManagedObjectModel, use the `model(for:)`
static function to get access to s ahred, cached model.
"""
)
@inlinable
convenience init(_ entities: NSEntityDescription...,
version: Schema.Version = Version(1, 0, 0))
Expand All @@ -19,13 +25,25 @@ public extension NSManagedObjectModel {
self.entities = entities
}

@available(*, deprecated, renamed: "model(for:)", message:
"""
Entities can only be used in one NSManagedObjectModel, use the `model(for:)`
static function to get access to s ahred, cached model.
"""
)
convenience init(_ types: [ any PersistentModel.Type ],
version: Schema.Version = Version(1, 0, 0))
{
self.init()
self.entities = SchemaBuilder.shared.lookupAllEntities(for: types)
}

@available(*, deprecated, renamed: "model(for:)", message:
"""
Entities can only be used in one NSManagedObjectModel, use the `model(for:)`
static function to get access to s ahred, cached model.
"""
)
@inlinable
convenience init(versionedSchema: any VersionedSchema.Type) {
self.init(versionedSchema.models,
Expand All @@ -39,8 +57,8 @@ public extension NSManagedObjectModel {
private let lock = NSLock()
private var map = [ Set<ObjectIdentifier> : NSManagedObjectModel ]()

extension NSManagedObjectModel {
public extension NSManagedObjectModel {

/// A cached version of the initializer.
static func model(for types: [ any PersistentModel.Type ])
-> NSManagedObjectModel
Expand All @@ -61,7 +79,8 @@ extension NSManagedObjectModel {
let mom : NSManagedObjectModel
if let cachedMOM { mom = cachedMOM }
else {
mom = NSManagedObjectModel(types)
mom = NSManagedObjectModel()
mom.entities = SchemaBuilder.shared.lookupAllEntities(for: types)
map[typeIDs] = mom
}
lock.unlock()
Expand Down

0 comments on commit f9eb134

Please sign in to comment.