Skip to content

Commit 82846f7

Browse files
committed
Merge branch 'develop'
2 parents ef2358f + dc46dbe commit 82846f7

File tree

5 files changed

+47
-21
lines changed

5 files changed

+47
-21
lines changed

Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import SwiftDiagnostics
1515
* named(init), // Initializers.
1616
* named(schemaMetadata), // The metadata.
1717
* named(entity), // Override the `entity()` function.
18-
* named(fetchRequest), // The fetchRequest factory
1918
* named(_$entity), // The cached the Entity
2019
* named(_$originalName),
2120
* named(_$hashModifier)
@@ -49,23 +48,6 @@ extension ModelMacro: MemberMacro { // @attached(member, names:...)
4948
initializers: findInitializers(in: classDecl)
5049
)
5150

52-
if classDecl.findFunctionWithName("fetchRequest", isStaticOrClass: true,
53-
numberOfParametersWithoutDefaults: 0)
54-
== nil
55-
{
56-
let modelClassName = modelClassName.text
57-
newMembers.append(
58-
"""
59-
/// Returns an `NSFetchRequest` setup for the `\(raw: modelClassName)`.
60-
@nonobjc \(raw: access)class func fetchRequest() -> CoreData.NSFetchRequest<\(raw: modelClassName)> {
61-
let fetchRequest = CoreData.NSFetchRequest<\(raw: modelClassName)>(entityName: "\(raw: modelClassName)")
62-
fetchRequest.entity = Self._$entity
63-
return fetchRequest
64-
}
65-
"""
66-
)
67-
}
68-
6951
let metadata = generateMetadataSlot(
7052
access: access,
7153
modelClassName: modelClassName,

Sources/ManagedModels/Container/ModelConfiguration.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ public struct ModelConfiguration: Hashable {
1818
public var allowsSave = true
1919

2020
public var isStoredInMemoryOnly : Bool {
21-
set { path = newValue ? "/dev/null" : try! lookupDefaultPath(for: name) }
21+
set {
22+
if newValue {
23+
path = "/dev/null"
24+
}
25+
else if path == "/dev/null" {
26+
do { path = try lookupDefaultPath(for: name) }
27+
catch { fatalError("Could not lookup path for: \(name) \(error)") }
28+
}
29+
// else: preserve existing path
30+
}
2231
get { path == "/dev/null" }
2332
}
2433

@@ -63,7 +72,6 @@ public struct ModelConfiguration: Hashable {
6372
self.cloudKitDatabase = cloudKitDatabase
6473
self.schema = schema
6574
self.allowsSave = allowsSave
66-
self.isStoredInMemoryOnly = isStoredInMemoryOnly
6775
}
6876
}
6977

Sources/ManagedModels/Container/NSPersistentStoreDescription+Data.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ extension NSPersistentStoreDescription {
2424

2525
// TBD: options, timeout, sqlitePragmas
2626

27+
if !modelConfiguration.allowsSave {
28+
shouldMigrateStoreAutomatically = false
29+
}
30+
2731
shouldAddStoreAsynchronously = false
2832
// shouldMigrateStoreAutomatically
2933
// shouldInferMappingModelAutomatically

Sources/ManagedModels/ModelMacroDefinition.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public macro _PersistedProperty() =
8282
named(init), // Initializers.
8383
named(schemaMetadata), // The metadata.
8484
named(entity), // Override the `entity()` function.
85-
named(fetchRequest), // The fetchRequest factory
8685
named(_$entity), // The cached the Entity
8786
named(_$originalName),
8887
named(_$hashModifier)

Sources/ManagedModels/PersistentModel/PersistentModel.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,36 @@ extension PersistentModel {
5959
@inlinable
6060
public static var _$entity : NSEntityDescription { self.entity() }
6161
}
62+
63+
public extension PersistentModel {
64+
65+
@inlinable
66+
static func fetchRequest() -> NSFetchRequest<Self> {
67+
NSFetchRequest<Self>(entityName: _$entity.name ?? NSStringFromClass(self))
68+
}
69+
70+
@inlinable
71+
static func fetchRequest<T>(filter : NSPredicate? = nil,
72+
sortBy keyPath : KeyPath<Self, T>,
73+
order: NSSortDescriptor.SortOrder = .forward,
74+
fetchOffset : Int? = nil,
75+
fetchLimit : Int? = nil)
76+
-> NSFetchRequest<Self>
77+
{
78+
let fetchRequest = Self.fetchRequest()
79+
fetchRequest.predicate = filter
80+
if let meta = Self.schemaMetadata.first(where: { $0.keypath == keyPath }) {
81+
fetchRequest.sortDescriptors = [
82+
NSSortDescriptor(key: meta.name, ascending: order == .forward)
83+
]
84+
}
85+
else {
86+
fetchRequest.sortDescriptors = [
87+
NSSortDescriptor(keyPath: keyPath, ascending: order == .forward)
88+
]
89+
}
90+
if let fetchOffset { fetchRequest.fetchOffset = fetchOffset }
91+
if let fetchLimit { fetchRequest.fetchLimit = fetchLimit }
92+
return fetchRequest
93+
}
94+
}

0 commit comments

Comments
 (0)