Skip to content

Commit

Permalink
Fix Observation notification not triggering on nested properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaafanador3 committed Jul 11, 2023
1 parent 2df4354 commit 20b29c3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion RealmSwift/Projection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ extension ProjectionObservable {

var projectedChanges = [PropertyChange]()
for i in 0..<newValues.count {
for property in schema.filter({ $0.originPropertyKeyPathString == names[i] }) {
for property in schema.filter({ $0.originPropertyKeyPathString.components(separatedBy: ".").dropLast().joined(separator: "") == names[i] }) {
var changeOldValue: Any?
if oldValues != nil {
changeOldValue = unmanagedRoot![keyPath: property.projectedKeyPath]
Expand Down
21 changes: 21 additions & 0 deletions RealmSwift/Tests/ProjectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,27 @@ class ProjectionTests: TestCase {
token.invalidate()
}

func testObserveNestedProjection() {
let realm = populatedRealm()
let johnProjection = realm.objects(PersonProjection.self).first!

let ex = expectation(description: "testProjectionNotification")
let token = johnProjection.observe { changes in
if case .change(_, _) = changes {
ex.fulfill()
} else {
XCTFail("expected .change, got \(changes)")
}

}
try! realm.write {
let johnObject = realm.objects(CommonPerson.self).filter("lastName == 'Snow'").first!
johnObject.address?.city = "Barranquilla"
}
waitForExpectations(timeout: 1000.0, handler: nil)
token.invalidate()
}

var changeDictionary: [NSKeyValueChangeKey: Any]?
override func observeValue(forKeyPath keyPath: String?, of object: Any?,
change: [NSKeyValueChangeKey: Any]?,
Expand Down
10 changes: 10 additions & 0 deletions Test.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 20b29c3

Please sign in to comment.