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 {
23
23
version: Schema . Version = Version ( 1 , 0 , 0 ) )
24
24
{
25
25
self . init ( )
26
- self . entities = SchemaBuilder . shared . lookupAllEntities ( for: types)
26
+ self . entities = SchemaBuilder ( ) . lookupAllEntities ( for: types)
27
27
}
28
28
29
29
@inlinable
@@ -38,6 +38,7 @@ public extension NSManagedObjectModel {
38
38
39
39
private let lock = NSLock ( )
40
40
private var map = [ Set < ObjectIdentifier > : NSManagedObjectModel ] ( )
41
+ private let sharedBuilder = SchemaBuilder ( )
41
42
42
43
public extension NSManagedObjectModel {
43
44
@@ -69,7 +70,7 @@ public extension NSManagedObjectModel {
69
70
if let cachedMOM { mom = cachedMOM }
70
71
else {
71
72
mom = NSManagedObjectModel ( )
72
- mom. entities = SchemaBuilder . shared . lookupAllEntities ( for: types)
73
+ mom. entities = sharedBuilder . lookupAllEntities ( for: types)
73
74
map [ typeIDs] = mom
74
75
}
75
76
lock. unlock ( )
Original file line number Diff line number Diff line change @@ -27,17 +27,11 @@ import CoreData
27
27
*/
28
28
public final class SchemaBuilder {
29
29
// 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
31
31
// object is the authority!
32
32
// - there can be multiple entities that use the same name, this spans the
33
33
// 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
+
41
35
private let lock = NSLock ( ) // TODO: use better lock :-)
42
36
43
37
/// 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 {
224
224
XCTAssertEqual ( address. attributes. count, 2 )
225
225
}
226
226
}
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
+
228
255
func testOptionalBackRef( ) throws {
229
256
let cache = SchemaBuilder ( )
230
257
let schema = NSManagedObjectModel (
You can’t perform that action at this time.
0 commit comments