Skip to content

Commit

Permalink
Another fix for duplicate builder runs
Browse files Browse the repository at this point in the history
Seems to work now, though it can't be used anyways ;-)
  • Loading branch information
helje5 committed Oct 1, 2023
1 parent 5575eba commit 3f9eb1f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Sources/ManagedModels/SchemaGeneration/SchemaBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,20 @@ public final class SchemaBuilder {
}
assertionFailure("Type frozen, but no entity found?")
}

allFrozen = false
if let newEntity = processModel(modelType) {
entities.append(newEntity)
}
}
if allFrozen { return } // all have been processed already

// TBD: The following does too much work, we might only need the
// most of those on the "new models"

// This recurses into `process`, if necessary.
discoverTargetTypes(in: entities, allEntities: &entities)

if allFrozen { return }

// Collect destination entity names in relships based on the modelType!
fillDestinationEntityNamesInRelationships(entities)
Expand All @@ -168,7 +170,16 @@ public final class SchemaBuilder {
continue
}
// This returns nil if the model is already processed.
guard let newEntity = processModel(targetType) else { continue }
guard let newEntity = processModel(targetType) else {
guard let existingEntity = lookupEntity(targetType) else {
assertionFailure("Type marked as processed, but no entity?")
continue
}
if !allEntities.contains(where: { $0 === existingEntity }) {
allEntities.append(existingEntity)
}
continue
}

allEntities.append(newEntity)
newEntities.append(newEntity)
Expand Down
29 changes: 29 additions & 0 deletions Tests/ManagedModelTests/SchemaGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,33 @@ final class SchemaGenerationTests: XCTestCase {
XCTAssertNotNil(mom.entitiesByName["Person"])
XCTAssertNotNil(mom.entitiesByName["Address"])
}

func testDuplicateGeneration() throws {
let cache = SchemaBuilder()

try autoreleasepool {
let entities = cache.lookupAllEntities(for: [
Fixtures.PersonAddressSchema.Person.self
])
XCTAssertEqual(entities.count, 2)

let address = try XCTUnwrap(
entities.first(where: { $0.name == "Address" })
)
XCTAssertEqual(address.attributes.count, 2)
}

// second run
try autoreleasepool {
let entities = cache.lookupAllEntities(for: [
Fixtures.PersonAddressSchema.Person.self
])
XCTAssertEqual(entities.count, 2)

let address = try XCTUnwrap(
entities.first(where: { $0.name == "Address" })
)
XCTAssertEqual(address.attributes.count, 2)
}
}
}

0 comments on commit 3f9eb1f

Please sign in to comment.