Skip to content

Commit 3e620e5

Browse files
committed
Fix queries
..
1 parent 86b36d6 commit 3e620e5

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

Sources/DirectToSwiftUI/Support/CoreData/DataSource.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,35 +64,46 @@ public class ManagedObjectDataSource<Object: NSManagedObject>: DataSource {
6464
try managedObjectContext.fetch(fr)
6565
}
6666
public func _primaryFetchCount(_ fr: NSFetchRequest<Object>) throws -> Int {
67-
try managedObjectContext.count(for: fr)
67+
if fr.resultType != .countResultType {
68+
let fr = fr.countCopy()
69+
fr.resultType = .countResultType
70+
return try managedObjectContext.count(for: fr)
71+
}
72+
else {
73+
return try managedObjectContext.count(for: fr)
74+
}
6875
}
6976
public func _primaryFetchGlobalIDs(_ fr: NSFetchRequest<NSManagedObjectID>)
7077
throws -> [ NSManagedObjectID ]
7178
{
7279
if fr.resultType != .managedObjectIDResultType {
73-
let fs = fr.objectIDsCopy()
74-
fs.resultType = .managedObjectIDResultType
75-
return try managedObjectContext.fetch(fs)
80+
let fr = fr.objectIDsCopy()
81+
fr.resultType = .managedObjectIDResultType
82+
return try managedObjectContext.fetch(fr)
7683
}
7784
else {
7885
return try managedObjectContext.fetch(fr)
7986
}
8087
}
8188

8289
public func fetchGlobalIDs() throws -> [ NSManagedObjectID ] {
83-
let fs = fetchRequest?.objectIDsCopy()
90+
let fr = fetchRequest?.objectIDsCopy()
8491
?? NSFetchRequest<NSManagedObjectID>(entityName: entity.name ?? "")
85-
fs.resultType = .managedObjectIDResultType
86-
return try _primaryFetchGlobalIDs(fs)
92+
fr.resultType = .managedObjectIDResultType
93+
return try _primaryFetchGlobalIDs(fr)
8794
}
88-
public func fetchGlobalIDs(_ fs: NSFetchRequest<Object>)
95+
public func fetchGlobalIDs(_ fr: NSFetchRequest<Object>)
8996
throws -> [ NSManagedObjectID ]
9097
{
91-
let fs = fs.objectIDsCopy()
92-
fs.resultType = .managedObjectIDResultType
93-
return try _primaryFetchGlobalIDs(fs)
98+
let fr = fr.objectIDsCopy()
99+
fr.resultType = .managedObjectIDResultType
100+
return try _primaryFetchGlobalIDs(fr)
94101
}
95-
102+
103+
public func fetchCount(_ fr: NSFetchRequest<Object>) throws -> Int {
104+
return try _primaryFetchCount(fr)
105+
}
106+
96107
public func createObject() -> Object {
97108
NSEntityDescription.insertNewObject(
98109
forEntityName: entity.name ?? "",
@@ -104,10 +115,10 @@ public class ManagedObjectDataSource<Object: NSManagedObject>: DataSource {
104115
public extension ManagedObjectDataSource {
105116

106117
func find() throws -> Object? {
107-
let fs = try fetchRequestForFetch()
108-
fs.fetchLimit = 2
118+
let fr = try fetchRequestForFetch()
119+
fr.fetchLimit = 2
109120

110-
let objects = try _primaryFetchObjects(fs)
121+
let objects = try _primaryFetchObjects(fr)
111122
assert(objects.count < 2)
112123
return objects.first
113124
}

Sources/DirectToSwiftUI/Support/CoreData/EntityExtras.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ extension NSEntityDescription {
9191
// => name:He* 10111 -skyrix +addresses.city:Magdeburg
9292
// TODO: show message unless cookie is set with instructions (using Semantic
9393
// Nag attached to cookie)
94+
if qs.isEmpty { return nil }
95+
9496
let isNumber = Int(qs)
9597
var q : NSPredicate? = nil
9698

Sources/DirectToSwiftUI/Support/CoreData/FetchRequestExtras.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public extension NSFetchRequest {
3030
}
3131
return typed
3232
}
33+
@objc func countCopy() -> NSFetchRequest<NSNumber> {
34+
let me = copy()
35+
guard let typed = me as? NSFetchRequest<NSNumber> else {
36+
fatalError("can't convert fetch request type! \(type(of: me))")
37+
}
38+
return typed
39+
}
3340
}
3441

3542
public extension NSFetchRequest {

Sources/DirectToSwiftUI/Support/CoreData/PredicateExtras.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public func predicateToMatchAnyValue(_ values: [ String : Any? ]?,
132132
NSComparisonPredicate(
133133
leftExpression : NSExpression(forKeyPath: key),
134134
rightExpression : NSExpression(forConstantValue: value),
135-
modifier: .direct, type: .equalTo,
135+
modifier: .direct, type: op,
136136
options: caseInsensitive ? [ .caseInsensitive ] : []
137137
)
138138
}

Sources/DirectToSwiftUI/ViewModel/D2SDisplayGroup.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public final class D2SDisplayGroup<Object: NSManagedObject>
3939
let q = and(qs, auxiliaryPredicate)
4040
guard !q.isEqual(to: fetchRequest.predicate) else { return }
4141
fetchRequest.predicate = q
42+
setNeedsRefetch()
4243
}
4344
}
4445
@Published var sortAttribute : NSAttributeDescription? = nil {
@@ -53,6 +54,7 @@ public final class D2SDisplayGroup<Object: NSManagedObject>
5354
self.fetchRequest.sortDescriptors =
5455
dataSource.entity.d2s.defaultSortDescriptors
5556
}
57+
setNeedsRefetch()
5658
}
5759
}
5860

@@ -142,7 +144,9 @@ public final class D2SDisplayGroup<Object: NSManagedObject>
142144
private func fetchCount(_ fetchRequest: NSFetchRequest<Object>) {
143145
// TODO: make async like in ZeeQL version
144146
do {
145-
let count = try dataSource.fetchCount()
147+
let count = try dataSource.fetchCount(fetchRequest)
148+
globalD2SLogger.trace("fetched count:", count,
149+
"\n for:", fetchRequest.predicate)
146150
integrateCount(count)
147151
}
148152
catch {
@@ -259,8 +263,6 @@ public final class D2SDisplayGroup<Object: NSManagedObject>
259263
}
260264
}
261265

262-
internal let D2SFetchQueue = DispatchQueue(label: "de.zeezide.d2s.fetchqueue")
263-
264266
fileprivate func buildInitialFetchSpec<Object: NSManagedObject>
265267
(for dataSource : ManagedObjectDataSource<Object>,
266268
auxiliaryPredicate : NSPredicate?)

0 commit comments

Comments
 (0)