File tree Expand file tree Collapse file tree 1 file changed +25
-3
lines changed
Expand file tree Collapse file tree 1 file changed +25
-3
lines changed Original file line number Diff line number Diff line change 11import CoreData
22import RxSwift
33import QueryKit
4+ import Foundation
45
56
67/// Extension to QuerySet to provide observables
@@ -16,8 +17,29 @@ extension QuerySet {
1617 /// Performs a query for the count of all objects matching the set predicate.
1718 /// Emits an Int containing the amount of objects matching the predicate and updates when the managed object context is changed.
1819 func count( ) throws -> Observable < Int > {
19- return context. qk_objectsDidChange ( ) . map { [ unowned self] notification in
20- return try self . count ( )
21- } . startWith ( try self . count ( ) )
20+ var count : Int = try self . count ( )
21+
22+ return AnonymousObservable { observer in
23+ observer. on ( . Next( count) )
24+
25+ return self . context. qk_objectsDidChange ( ) . subscribeNext { notification in
26+ let insertedObjects = notification. insertedObjects. filter {
27+ $0. entity. name == self . entityName
28+ }
29+ let deletedObjects = notification. deletedObjects. filter {
30+ $0. entity. name == self . entityName
31+ }
32+
33+ if let predicate = self . predicate {
34+ count += ( insertedObjects as NSArray ) . filteredArrayUsingPredicate ( predicate) . count
35+ count += ( deletedObjects as NSArray ) . filteredArrayUsingPredicate ( predicate) . count
36+ } else {
37+ count -= insertedObjects. count
38+ count -= deletedObjects. count
39+ }
40+
41+ observer. on ( . Next( count) )
42+ }
43+ }
2244 }
2345}
You can’t perform that action at this time.
0 commit comments