Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Sep 30, 2023
2 parents ef2358f + dc46dbe commit 82846f7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
18 changes: 0 additions & 18 deletions Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import SwiftDiagnostics
* named(init), // Initializers.
* named(schemaMetadata), // The metadata.
* named(entity), // Override the `entity()` function.
* named(fetchRequest), // The fetchRequest factory
* named(_$entity), // The cached the Entity
* named(_$originalName),
* named(_$hashModifier)
Expand Down Expand Up @@ -49,23 +48,6 @@ extension ModelMacro: MemberMacro { // @attached(member, names:...)
initializers: findInitializers(in: classDecl)
)

if classDecl.findFunctionWithName("fetchRequest", isStaticOrClass: true,
numberOfParametersWithoutDefaults: 0)
== nil
{
let modelClassName = modelClassName.text
newMembers.append(
"""
/// Returns an `NSFetchRequest` setup for the `\(raw: modelClassName)`.
@nonobjc \(raw: access)class func fetchRequest() -> CoreData.NSFetchRequest<\(raw: modelClassName)> {
let fetchRequest = CoreData.NSFetchRequest<\(raw: modelClassName)>(entityName: "\(raw: modelClassName)")
fetchRequest.entity = Self._$entity
return fetchRequest
}
"""
)
}

let metadata = generateMetadataSlot(
access: access,
modelClassName: modelClassName,
Expand Down
12 changes: 10 additions & 2 deletions Sources/ManagedModels/Container/ModelConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ public struct ModelConfiguration: Hashable {
public var allowsSave = true

public var isStoredInMemoryOnly : Bool {
set { path = newValue ? "/dev/null" : try! lookupDefaultPath(for: name) }
set {
if newValue {
path = "/dev/null"
}
else if path == "/dev/null" {
do { path = try lookupDefaultPath(for: name) }
catch { fatalError("Could not lookup path for: \(name) \(error)") }
}
// else: preserve existing path
}
get { path == "/dev/null" }
}

Expand Down Expand Up @@ -63,7 +72,6 @@ public struct ModelConfiguration: Hashable {
self.cloudKitDatabase = cloudKitDatabase
self.schema = schema
self.allowsSave = allowsSave
self.isStoredInMemoryOnly = isStoredInMemoryOnly
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ extension NSPersistentStoreDescription {

// TBD: options, timeout, sqlitePragmas

if !modelConfiguration.allowsSave {
shouldMigrateStoreAutomatically = false
}

shouldAddStoreAsynchronously = false
// shouldMigrateStoreAutomatically
// shouldInferMappingModelAutomatically
Expand Down
1 change: 0 additions & 1 deletion Sources/ManagedModels/ModelMacroDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public macro _PersistedProperty() =
named(init), // Initializers.
named(schemaMetadata), // The metadata.
named(entity), // Override the `entity()` function.
named(fetchRequest), // The fetchRequest factory
named(_$entity), // The cached the Entity
named(_$originalName),
named(_$hashModifier)
Expand Down
33 changes: 33 additions & 0 deletions Sources/ManagedModels/PersistentModel/PersistentModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,36 @@ extension PersistentModel {
@inlinable
public static var _$entity : NSEntityDescription { self.entity() }
}

public extension PersistentModel {

@inlinable
static func fetchRequest() -> NSFetchRequest<Self> {
NSFetchRequest<Self>(entityName: _$entity.name ?? NSStringFromClass(self))
}

@inlinable
static func fetchRequest<T>(filter : NSPredicate? = nil,
sortBy keyPath : KeyPath<Self, T>,
order: NSSortDescriptor.SortOrder = .forward,
fetchOffset : Int? = nil,
fetchLimit : Int? = nil)
-> NSFetchRequest<Self>
{
let fetchRequest = Self.fetchRequest()
fetchRequest.predicate = filter
if let meta = Self.schemaMetadata.first(where: { $0.keypath == keyPath }) {
fetchRequest.sortDescriptors = [
NSSortDescriptor(key: meta.name, ascending: order == .forward)
]
}
else {
fetchRequest.sortDescriptors = [
NSSortDescriptor(keyPath: keyPath, ascending: order == .forward)
]
}
if let fetchOffset { fetchRequest.fetchOffset = fetchOffset }
if let fetchLimit { fetchRequest.fetchLimit = fetchLimit }
return fetchRequest
}
}

0 comments on commit 82846f7

Please sign in to comment.