Skip to content

Conversation

@crazytonyli
Copy link
Contributor

The original implementation overrides NSManagedObject.willChangeValue which must not be overridden as mentioned in the API doc.

Regression Notes

  1. Potential unintended areas of impact
    None.

  2. What I did to test those areas of impact (or what existing automated tests I relied on)
    None.

  3. What automated tests I added (or what prevented me from doing so)
    None.

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

The original implementation overrides `NSManagedObject.willChangeValue`
which must not be overridden as mentioned in the API doc.

See https://developer.apple.com/documentation/coredata/nsmanagedobject/1506229-willchangevalue
@crazytonyli crazytonyli added the Core Data Issues related to Core Data label Oct 4, 2022
@crazytonyli crazytonyli added this to the 21.0 milestone Oct 4, 2022
@crazytonyli crazytonyli requested a review from mokagio October 4, 2022 08:34
@crazytonyli crazytonyli self-assigned this Oct 4, 2022
//
guard notification.managedObjectContext?.concurrencyType == .mainQueueConcurrencyType else {
return
}
Copy link
Contributor Author

@crazytonyli crazytonyli Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original observer logic is kept, including this one here. But I think not invaliding the cache just because the notification object is in a background context is problematic, see the unit test below for more.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Oct 4, 2022

You can test the changes in WordPress from this Pull Request by:
  • Clicking here or scanning the QR code below to access App Center
  • Then installing the build number pr19395-da753bd on your iPhone

If you need access to App Center, please ask a maintainer to add you.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Oct 4, 2022

You can test the changes in Jetpack from this Pull Request by:
  • Clicking here or scanning the QR code below to access App Center
  • Then installing the build number pr19395-da753bd on your iPhone

If you need access to App Center, please ask a maintainer to add you.

Copy link
Contributor

@mokagio mokagio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love how you wrote a test to show the threading issue!

I started looking at the git blame, then got interrupted, then side tracked, and I'm only now coming back to my WIP-review.

I didn't get to the bottom of how that .mainQueueConcurrencyType came about, but the comment states it was a matter of optimization. Maybe it was a case of premature optimization?

I'd be okay with removing the restriction and see how the app behaves.

///
fileprivate var cachedHeaderAndBodyContentGroups: [FormattableContentGroup]?

private let cachedAttributesObserver: NotificationCachedAttributesObserver? = nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the explicit = nil assignment intentional?

I think the compiler is already doing that for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for let variable apparently. But this property is changed to a var and I've removed the nil assignment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for let variable apparently

Blimey! I didn't notice it was a let 😳

Comment on lines 93 to 96
cachedAttributesObserver = NotificationCachedAttributesObserver()
for attr in Notification.cachedAttributes {
addObserver(cachedAttributesObserver!, forKeyPath: attr, context: nil)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of doing the following, to remove the force unwrap?

Suggested change
cachedAttributesObserver = NotificationCachedAttributesObserver()
for attr in Notification.cachedAttributes {
addObserver(cachedAttributesObserver!, forKeyPath: attr, context: nil)
}
let cachedAttributesObserver = NotificationCachedAttributesObserver()
for attr in Notification.cachedAttributes {
addObserver(cachedAttributesObserver, forKeyPath: attr, context: nil)
}
self.cachedAttributesObserver = cachedAttributesObserver

The force unwrap is of course okay in the way it's used now, but who's to tell how the code will evolve? There might end up being a code path in which it's nil in the future.

Maybe I'm overthinking it 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 addressed in da753bd. It's always good to not use force unwrap.

This will reduce amount of KVO observer calls, now that only the fetched
instances have KVO observer registered, not the inserted ones.
@crazytonyli crazytonyli merged commit 614c816 into trunk Oct 7, 2022
@crazytonyli crazytonyli deleted the reimplement-notification-cache-observer branch October 7, 2022 05:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core Data Issues related to Core Data [Status] DO NOT MERGE

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants