File tree Expand file tree Collapse file tree 3 files changed +33
-11
lines changed Expand file tree Collapse file tree 3 files changed +33
-11
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ public extension NSManagedObjectModel {
2323 version: Schema . Version = Version ( 1 , 0 , 0 ) )
2424 {
2525 self . init ( )
26- self . entities = SchemaBuilder . shared . lookupAllEntities ( for: types)
26+ self . entities = SchemaBuilder ( ) . lookupAllEntities ( for: types)
2727 }
2828
2929 @inlinable
@@ -38,6 +38,7 @@ public extension NSManagedObjectModel {
3838
3939private let lock = NSLock ( )
4040private var map = [ Set < ObjectIdentifier > : NSManagedObjectModel ] ( )
41+ private let sharedBuilder = SchemaBuilder ( )
4142
4243public extension NSManagedObjectModel {
4344
@@ -69,7 +70,7 @@ public extension NSManagedObjectModel {
6970 if let cachedMOM { mom = cachedMOM }
7071 else {
7172 mom = NSManagedObjectModel ( )
72- mom. entities = SchemaBuilder . shared . lookupAllEntities ( for: types)
73+ mom. entities = sharedBuilder . lookupAllEntities ( for: types)
7374 map [ typeIDs] = mom
7475 }
7576 lock. unlock ( )
Original file line number Diff line number Diff line change @@ -27,17 +27,11 @@ import CoreData
2727 */
2828public final class SchemaBuilder {
2929 // Notes:
30- // - this MUST NOT call `.entity` on the model! might recurse w/ lock, this
30+ // - this MUST NOT call `.entity() ` on the model! might recurse w/ lock, this
3131 // object is the authority!
3232 // - there can be multiple entities that use the same name, this spans the
3333 // whole type system. E.g. when versioned schemas are used.
34-
35- /**
36- * A shared SchemaBuilder that caches `NSEntityDescription` values for
37- * ``PersistentModel`` `NSManagedObject`'s.
38- */
39- public static let shared = SchemaBuilder ( )
40-
34+
4135 private let lock = NSLock ( ) // TODO: use better lock :-)
4236
4337 /// ObjectIdentifier of PersistentModel type to the associated schema.
Original file line number Diff line number Diff line change @@ -224,7 +224,34 @@ final class SchemaGenerationTests: XCTestCase {
224224 XCTAssertEqual ( address. attributes. count, 2 )
225225 }
226226 }
227-
227+
228+ func testDuplicateMOMGeneration( ) throws {
229+ try autoreleasepool {
230+ let model1 = NSManagedObjectModel ( [
231+ Fixtures . PersonAddressSchema. Person. self
232+ ] )
233+ XCTAssertEqual ( model1. entities. count, 2 )
234+
235+ let address = try XCTUnwrap (
236+ model1. entities. first ( where: { $0. name == " Address " } )
237+ )
238+ XCTAssertEqual ( address. attributes. count, 2 )
239+ }
240+
241+ // second run
242+ try autoreleasepool {
243+ let model2 = NSManagedObjectModel ( [
244+ Fixtures . PersonAddressSchema. Person. self
245+ ] )
246+ XCTAssertEqual ( model2. entities. count, 2 )
247+
248+ let address = try XCTUnwrap (
249+ model2. entities. first ( where: { $0. name == " Address " } )
250+ )
251+ XCTAssertEqual ( address. attributes. count, 2 )
252+ }
253+ }
254+
228255 func testOptionalBackRef( ) throws {
229256 let cache = SchemaBuilder ( )
230257 let schema = NSManagedObjectModel (
You can’t perform that action at this time.
0 commit comments