Skip to content

Commit c27d5e9

Browse files
In memory store
1 parent 09540f9 commit c27d5e9

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

Sources/ProgrammaticCoreData/NSManagedObjectModel+Extensions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ public extension NSManagedObjectModel {
5353
func createLocalContainer(name: String, subdirectory: String?) throws -> NSPersistentContainer {
5454
try NSPersistentContainer(name: name, managedObjectModel: self, subdirecotry: subdirectory)
5555
}
56+
57+
func createInMemoryContainer(name: String) throws -> NSPersistentContainer {
58+
let container = NSPersistentContainer(name: name, managedObjectModel: self)
59+
let description = NSPersistentStoreDescription()
60+
description.type = NSInMemoryStoreType
61+
container.persistentStoreDescriptions = [description]
62+
return container
63+
}
5664
}

Sources/ProgrammaticCoreData/NSPersistentContainer+Extensions.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public extension NSPersistentContainer {
2020

2121
case cloud(cloudContainerIdentifier: String, options: CloudOptions)
2222
case local(subdirecotry: String?)
23+
case inMemory
2324

2425
public static var local: Self {
2526
.local(subdirecotry: nil)
@@ -29,7 +30,7 @@ public extension NSPersistentContainer {
2930
switch self {
3031
case .cloud:
3132
return true
32-
case .local:
33+
case .local, .inMemory:
3334
return false
3435
}
3536
}
@@ -82,6 +83,8 @@ public extension NSPersistentContainer {
8283
)
8384
case .local(let subdirectory):
8485
container = try model.createLocalContainer(name: name, subdirectory: subdirectory)
86+
case .inMemory:
87+
container = try model.createInMemoryContainer(name: name)
8588
}
8689
try await container.loadPersistentStores()
8790
return container

0 commit comments

Comments
 (0)