@@ -120,7 +120,8 @@ public final class D2SDisplayGroup<Object: OActiveRecord>
120
120
121
121
private func integrateCount( _ count: Int ) {
122
122
assert ( _dispatchPreconditionTest ( . onQueue( . main) ) )
123
-
123
+ self . countFetchToken = nil
124
+
124
125
#if false // nope, a fetch count means we rebuild!
125
126
if count == results. count { return } // all good already
126
127
#endif
@@ -138,11 +139,14 @@ public final class D2SDisplayGroup<Object: OActiveRecord>
138
139
results. clearOrderAndApplyNewCount ( count)
139
140
}
140
141
142
+ private var countFetchToken : AnyCancellable ?
143
+
141
144
private func fetchCount( _ fetchSpecification: FetchSpecification ) {
142
145
let fs = fetchSpecification // has to be done, can't use inside fetchCount?
143
- _ = dataSource. fetchCount ( fs, on: D2SFetchQueue)
146
+ countFetchToken = dataSource. fetchCount ( fs, on: D2SFetchQueue)
144
147
. receive ( on: RunLoop . main)
145
148
. catch { ( error : Swift . Error ) -> Just < Int > in
149
+ self . countFetchToken = nil
146
150
self . handleError ( error)
147
151
return Just ( 0 )
148
152
}
@@ -196,8 +200,13 @@ public final class D2SDisplayGroup<Object: OActiveRecord>
196
200
self . results = newResults
197
201
}
198
202
199
- private struct Query : Equatable {
203
+ private final class Query : Equatable {
200
204
let range : Range < Int >
205
+ var token : AnyCancellable ?
206
+ init ( range: Range < Int > ) { self . range = range }
207
+ static func == ( lhs: Query , rhs: Query ) -> Bool {
208
+ return lhs. range == rhs. range
209
+ }
201
210
}
202
211
private var activeQueries = [ Query ] ( )
203
212
@@ -242,7 +251,7 @@ public final class D2SDisplayGroup<Object: OActiveRecord>
242
251
let query = Query ( range: fetchRange)
243
252
activeQueries. append ( query) // keep it alive
244
253
245
- _ = dataSource. fetchGlobalIDs ( fs, on: D2SFetchQueue)
254
+ query . token = dataSource. fetchGlobalIDs ( fs, on: D2SFetchQueue)
246
255
. receive ( on: RunLoop . main)
247
256
. flatMap { ( globalIDs ) -> AnyPublisher < [ Object ] , Error > in
248
257
var missingGIDs = Set < GlobalID > ( )
@@ -317,7 +326,13 @@ fileprivate func buildInitialFetchSpec<Object: ActiveRecordType>
317
326
if ( fs. sortOrderings? . count ?? 0 ) == 0 {
318
327
fs. sortOrderings = dataSource. entity? . d2s. defaultSortOrderings ?? [ ]
319
328
}
320
- assert ( fs. sortOrderings != nil && !( fs. sortOrderings? . isEmpty ?? true ) )
329
+ #if os(macOS)
330
+ if !( fs. sortOrderings != nil && !( fs. sortOrderings? . isEmpty ?? true ) ) {
331
+ globalD2SLogger. error ( " got no sort orderings for fetchspec: " , fs)
332
+ }
333
+ #else
334
+ assert ( fs. sortOrderings != nil && !( fs. sortOrderings? . isEmpty ?? true ) )
335
+ #endif
321
336
322
337
if let aux = auxiliaryQualifier {
323
338
fs. qualifier = aux. and ( fs. qualifier)
0 commit comments