Skip to content

Commit 8ad436c

Browse files
committed
Drop generation of _$entity
No caching of that.
1 parent 921d3e7 commit 8ad436c

File tree

9 files changed

+11
-55
lines changed

9 files changed

+11
-55
lines changed

Sources/ManagedModelMacros/ModelMacro/GenerateInitializers.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ extension ModelMacro {
4646
message: "Use `init(context:)` or `init()` instead.")
4747
\(raw: access)override init(entity: CoreData.NSEntityDescription, insertInto context: NSManagedObjectContext?)
4848
{
49-
assert(entity === Self._$entity, "Attempt to initialize PersistentModel w/ different entity?")
5049
super.init(entity: entity, insertInto: context)
5150
}
5251
"""
@@ -67,7 +66,7 @@ extension ModelMacro {
6766
/// - Parameters:
6867
// - context: An `NSManagedObjectContext` the object should be inserted into.
6968
\(raw: access)init(context: CoreData.NSManagedObjectContext?) {
70-
super.init(entity: Self._$entity, insertInto: context)
69+
super.init(entity: Self.entity(), insertInto: context)
7170
}
7271
"""
7372
)
@@ -78,7 +77,7 @@ extension ModelMacro {
7877
/// Initialize a `\(modelClassName)` object w/o inserting it into a
7978
/// context.
8079
\(raw: access)init() {
81-
super.init(entity: Self._$entity, insertInto: nil)
80+
super.init(entity: Self.entity(), insertInto: nil)
8281
}
8382
"""
8483
)

Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import SwiftDiagnostics
1414
* @attached(member, names: // Those are the names we add
1515
* named(init), // Initializers.
1616
* named(schemaMetadata), // The metadata.
17-
* named(entity), // Override the `entity()` function.
18-
* named(_$entity), // The cached the Entity
1917
* named(_$originalName),
2018
* named(_$hashModifier)
2119
* )
@@ -55,17 +53,6 @@ extension ModelMacro: MemberMacro { // @attached(member, names:...)
5553
)
5654
newMembers.append(DeclSyntax(metadata))
5755

58-
if classDecl.findFunctionWithName("entity", isStaticOrClass: true,
59-
parameterCount: 0) == nil
60-
{
61-
newMembers.append(
62-
"""
63-
/// Returns the `NSEntityDescription` associated w/ the `PersistentModel`.
64-
\(raw: access)override class func entity() -> NSEntityDescription { _$entity }
65-
"""
66-
)
67-
}
68-
6956
// TODO: Lookup `originalName` parameter in `macroNode`
7057
newMembers.append(
7158
"""
@@ -77,15 +64,6 @@ extension ModelMacro: MemberMacro { // @attached(member, names:...)
7764
\(raw: access)static let _$hashModifier : String? = nil
7865
"""
7966
)
80-
81-
newMembers.append(
82-
"""
83-
/// The shared `NSEntityDescription` for the `PersistentModel`.
84-
/// Never modify the referred object!
85-
\(raw: access)static let _$entity =
86-
ManagedModels.SchemaBuilder.shared._entity(for: \(modelClassName).self)
87-
"""
88-
)
8967

9068
return newMembers
9169
}

Sources/ManagedModels/ModelMacroDefinition.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ public macro _PersistedProperty() =
8181
@attached(member, names: // Those are the names we add
8282
named(init), // Initializers.
8383
named(schemaMetadata), // The metadata.
84-
named(entity), // Override the `entity()` function.
85-
named(_$entity), // The cached the Entity
8684
named(_$originalName),
8785
named(_$hashModifier)
8886
)

Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ public extension PersistentModel {
120120
func _setOptionalToOneValue<T>(forKey key: String, to model: T?)
121121
where T: PersistentModel
122122
{
123-
#if DEBUG
124-
let relship = Self._$entity.relationshipsByName[key]!
125-
assert(!relship.isToMany, "relship: \(relship)")
126-
#endif
127123
if let model {
128124
if model.modelContext != self.modelContext {
129125
if let otherCtx = model.modelContext, self.modelContext == nil {

Sources/ManagedModels/PersistentModel/PersistentModel.swift

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ public protocol PersistentModel: NSManagedObject, Hashable, Identifiable {
1919
*/
2020
static var schemaMetadata : [ NSManagedObjectModel.PropertyMetadata ] { get }
2121

22-
/**
23-
* Reflection data for the model.
24-
*
25-
* This is considered private, use a Schema to access entities, and NEVER
26-
* modify the schema objects after they got setup.
27-
*
28-
* API DIFF: SwiftData doesn't have that, always builds dynamically.
29-
*/
30-
static var _$entity : NSEntityDescription { get }
31-
// Why have that? Cheap cache.
3222

3323
/// The `renamingIdentifier` of the model.
3424
static var _$originalName : String? { get }
@@ -55,16 +45,13 @@ extension PersistentModel {
5545
public static var schemaMetadata : [ NSManagedObjectModel.PropertyMetadata ] {
5646
fatalError("Subclass needs to implement `schemaMetadata`")
5747
}
58-
59-
@inlinable
60-
public static var _$entity : NSEntityDescription { self.entity() }
6148
}
6249

6350
public extension PersistentModel {
6451

6552
@inlinable
6653
static func fetchRequest() -> NSFetchRequest<Self> {
67-
NSFetchRequest<Self>(entityName: _$entity.name ?? NSStringFromClass(self))
54+
NSFetchRequest<Self>(entityName: _typeName(Self.self, qualified: false))
6855
}
6956

7057
@inlinable

Tests/ManagedModelTests/BasicModelTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ final class BasicModelTests: XCTestCase {
1616

1717
func testEntityName() throws {
1818
let addressType = Fixtures.PersonAddressSchema.Address.self
19-
XCTAssertEqual(addressType._$entity.name, "Address")
2019
XCTAssertEqual(addressType.entity().name, "Address")
2120
}
2221

Tests/ManagedModelTests/CoreDataAssumptionsTests.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ final class CoreDataAssumptionsTests: XCTestCase {
2020
let relationship = NSRelationshipDescription()
2121
relationship.name = "addresses"
2222
//relationship.destinationEntity =
23-
// Fixtures.PersonAddressSchema.Address._$entity
23+
// Fixtures.PersonAddressSchema.Address.entity()
2424
//relationship.inverseRelationship =
25-
// Fixtures.PersonAddressSchema.Address._$entity.relationshipsByName["person"]
25+
// Fixtures.PersonAddressSchema.Address.entity().relationshipsByName["person"]
2626

2727
// This just seems to be the default.
2828
XCTAssertTrue(relationship.isToMany)
@@ -32,9 +32,10 @@ final class CoreDataAssumptionsTests: XCTestCase {
3232
let relationship = NSRelationshipDescription()
3333
relationship.name = "person"
3434
relationship.maxCount = 1 // toOne marker!
35+
#if false // old
3536
relationship.destinationEntity =
36-
Fixtures.PersonAddressSchema.Person._$entity
37-
// Yes! This does not work at this point.
37+
Fixtures.PersonAddressSchema.Person.entity()
38+
#endif
3839
XCTAssertFalse(relationship.isToMany)
3940
}
4041

Tests/ManagedModelTests/Schemas/ExpandedPersonAddressSchema.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension Fixtures {
3131

3232
init(firstname: String, lastname: String, addresses: [ Address ]) {
3333
// Note: Could do Self.entity!
34-
super.init(entity: Self._$entity, insertInto: nil)
34+
super.init(entity: Self.entity(), insertInto: nil)
3535
self.firstname = firstname
3636
self.lastname = lastname
3737
self.addresses = addresses
@@ -42,7 +42,6 @@ extension Fixtures {
4242
.init(name: "lastname" , keypath: \Person.lastname),
4343
.init(name: "addresses" , keypath: \Person.addresses)
4444
]
45-
public static let _$entity = SchemaBuilder.shared._entity(for: Person.self)
4645
public static let _$originalName : String? = nil
4746
public static let _$hashModifier : String? = nil
4847
}
@@ -54,7 +53,7 @@ extension Fixtures {
5453
@NSManaged var person : Person
5554

5655
init(street: String, appartment: String? = nil, person: Person) {
57-
super.init(entity: Self._$entity, insertInto: nil)
56+
super.init(entity: Self.entity(), insertInto: nil)
5857
self.street = street
5958
self.appartment = appartment
6059
self.person = person
@@ -65,7 +64,6 @@ extension Fixtures {
6564
.init(name: "appartment" , keypath: \Address.appartment),
6665
.init(name: "person" , keypath: \Address.person)
6766
]
68-
public static let _$entity = SchemaBuilder.shared._entity(for: Address.self)
6967
public static let _$originalName : String? = nil
7068
public static let _$hashModifier : String? = nil
7169
}

Tests/ManagedModelTests/Schemas/PersonAddressSchemaNoInverse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension Fixtures {
2424
var addresses : [ Address ]
2525

2626
init(firstname: String, lastname: String, addresses: [ Address ]) {
27-
super.init(entity: Self._$entity, insertInto: nil)
27+
super.init(entity: Self.entity(), insertInto: nil)
2828
self.firstname = firstname
2929
self.lastname = lastname
3030
self.addresses = addresses

0 commit comments

Comments
 (0)