Skip to content

Commit 3a13266

Browse files
committed
Handle sub-entities when filtering change sets
1 parent c2a007b commit 3a13266

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Fixes a bug where `count()` using a predicate may have not counted objects
88
that have been modified to come into the predicate.
9+
- QuerySet subscriptions will now correctly handle abstract and sub entities
10+
for managed objects.
911

1012
## 0.5.2
1113

RxQueryKit/RxQueryKit.swift

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,40 @@ import QueryKit
44
import Foundation
55

66

7+
private func managedObjectMatchesEntity(entityNames: [String]) -> (NSManagedObject) -> Bool {
8+
return { object in
9+
if let name = object.entity.name {
10+
return entityNames.contains(name)
11+
}
12+
13+
return false
14+
}
15+
}
16+
17+
718
/// Extension to QuerySet to provide observables
819
extension QuerySet {
20+
private var entityNames: [String] {
21+
if let entity = NSEntityDescription.entityForName(entityName, inManagedObjectContext: context) {
22+
return [entityName] + entity.subentitiesByName.keys
23+
}
24+
25+
return [entityName]
26+
}
27+
928
/// Performs a query for all objects matching the set predicate ordered by any set sort descriptors.
1029
/// Emits a value with an array of all objects when the managed object context is changed.
1130
public func objects() throws -> Observable<[ModelType]> {
1231
let initial = try self.array()
32+
let filter = managedObjectMatchesEntity(entityNames)
1333

1434
return Observable.create { observer in
1535
observer.on(.Next(initial))
1636

1737
return self.context.qk_objectsDidChange().subscribeNext { notification in
18-
let insertedObjects = notification.insertedObjects.filter {
19-
$0.entity.name == self.entityName
20-
}
21-
let updatedObjects = notification.updatedObjects.filter {
22-
$0.entity.name == self.entityName
23-
}
24-
let deletedObjects = notification.deletedObjects.filter {
25-
$0.entity.name == self.entityName
26-
}
38+
let insertedObjects = notification.insertedObjects.filter(filter)
39+
let updatedObjects = notification.updatedObjects.filter(filter)
40+
let deletedObjects = notification.deletedObjects.filter(filter)
2741

2842
if insertedObjects.isEmpty && updatedObjects.isEmpty && deletedObjects.isEmpty {
2943
return
@@ -43,14 +57,13 @@ extension QuerySet {
4357
/// Emits an Int containing the amount of objects matching the predicate and updates when the managed object context is changed.
4458
public func count() throws -> Observable<Int> {
4559
var count: Int = try self.count()
60+
let filter = managedObjectMatchesEntity(entityNames)
4661

4762
return Observable.create { observer in
4863
observer.on(.Next(count))
4964

5065
return self.context.qk_objectsDidChange().subscribeNext { notification in
51-
let updatedObjects = notification.updatedObjects.filter {
52-
$0.entity.name == self.entityName
53-
}
66+
let updatedObjects = notification.updatedObjects.filter(filter)
5467

5568
if !updatedObjects.isEmpty && self.predicate != nil {
5669
do {
@@ -62,12 +75,8 @@ extension QuerySet {
6275
return
6376
}
6477

65-
let insertedObjects = notification.insertedObjects.filter {
66-
$0.entity.name == self.entityName
67-
}
68-
let deletedObjects = notification.deletedObjects.filter {
69-
$0.entity.name == self.entityName
70-
}
78+
let insertedObjects = notification.insertedObjects.filter(filter)
79+
let deletedObjects = notification.deletedObjects.filter(filter)
7180

7281
var delta = 0
7382

0 commit comments

Comments
 (0)