Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions Sources/ProgrammaticCoreData/NSManagedObjectModel+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public extension NSManagedObjectModel {
)
}

func createContainerSync(
name: String? = nil,
location: NSPersistentContainer.Location
) throws -> NSPersistentContainer {
try NSPersistentContainer.createSync(
name: prefixName(name),
model: self,
location: location
)
}

func createCloudContainer(
name: String,
cloudContainerIdentifier: String,
Expand All @@ -45,13 +56,13 @@ public extension NSManagedObjectModel {
options: options
)
}

func createLocalContainer(name: String, path: URL) -> NSPersistentContainer {
NSPersistentContainer(name: name, managedObjectModel: self, path: path)
func createLocalContainer(name: String? = nil, path: URL) -> NSPersistentContainer {
NSPersistentContainer(name: prefixName(name), managedObjectModel: self, path: path)
}

func createLocalContainer(name: String, subdirectory: String?) throws -> NSPersistentContainer {
try NSPersistentContainer(name: name, managedObjectModel: self, subdirecotry: subdirectory)
func createLocalContainer(name: String? = nil, subdirectory: String?) throws -> NSPersistentContainer {
try NSPersistentContainer(name: prefixName(name), managedObjectModel: self, subdirecotry: subdirectory)
}

func createInMemoryContainer(name: String) throws -> NSPersistentContainer {
Expand All @@ -61,4 +72,13 @@ public extension NSManagedObjectModel {
container.persistentStoreDescriptions = [description]
return container
}

/// Be lazy to type names, provide a basically unique prefix for naming.
func prefixName(_ name: String?) -> String {
if let name = name { name } else {
Self.prefix + String(describing: Self.self)
}
}

private static let prefix: String = "ProgrammaticCoreData."
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,29 @@ public extension NSPersistentContainer {
try await container.loadPersistentStores()
return container
}

static func createSync(
name: String,
model: NSManagedObjectModel,
location: Location
) throws -> NSPersistentContainer {
let container: NSPersistentContainer
switch location {
case .cloud(let cloudContainerIdentifier, let options):
container = try model.createCloudContainer(
name: name,
cloudContainerIdentifier: cloudContainerIdentifier,
options: options
)
case .local(let subdirectory):
container = try model.createLocalContainer(name: name, subdirectory: subdirectory)
case .inMemory:
container = try model.createInMemoryContainer(name: name)
}
var callback: (desc: NSPersistentStoreDescription?, error: (any Error)?) = (nil, nil)
container.loadPersistentStores { callback = ($0, $1) }
if let desc = callback.desc { debugPrint("[createSync]", desc) }
if let error = callback.error { throw error }
return container
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public extension SelfDescribingCoreDataEntity {
)
}

#if swift(>=5.9)
// even more than A24...
static func description<each A>(
_ parameters: repeat EntityDescriptionAttribute<Self, each A>
) -> NSEntityDescription {
var properties: [NSPropertyDescription] = []
for para in repeat each parameters {
properties.append(para.nsPropertyDescription)
}
return NSEntityDescription(Self.self, properties: properties)
}
#endif

static func description<A0, A1, A2>(
_ a0: EntityDescriptionAttribute<Self, A0>,
_ a1: EntityDescriptionAttribute<Self, A1>,
Expand Down