Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discarded notification observers in StatsWidgetsStore #20995

Merged
merged 2 commits into from
Jul 4, 2023

Conversation

crazytonyli
Copy link
Contributor

Relates to #20994.

This PR changes to use StatsWidgetsStore as notification observers, so that NSNotificationCenter automatically unregister the observer when the store instance is released.

StatsWidgetsStore is currently a singleton, so technically there is no issue with current code and this PR has no effect at all to the app. However, StatsWidgetsStore may get to escape from the StoreContainer singleton and the issue with notification observers will arise then. Also, we should not discard notification observers anyways.

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)
    Log in and log out.

  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.

UI Changes testing checklist: N/A

@crazytonyli crazytonyli added this to the 22.8 milestone Jul 3, 2023
@crazytonyli crazytonyli requested review from jkmassel and mokagio July 3, 2023 22:37
@crazytonyli crazytonyli self-assigned this Jul 3, 2023
@wpmobilebot
Copy link
Contributor

wpmobilebot commented Jul 3, 2023

WordPress Alpha📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
App NameWordPress Alpha WordPress Alpha
ConfigurationRelease-Alpha
Build Numberpr20995-66ff6fb
Version22.7
Bundle IDorg.wordpress.alpha
Commit66ff6fb
App Center BuildWPiOS - One-Offs #6201
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Jul 3, 2023

Jetpack Alpha📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
App NameJetpack Alpha Jetpack Alpha
ConfigurationRelease-Alpha
Build Numberpr20995-66ff6fb
Version22.7
Bundle IDcom.jetpack.alpha
Commit66ff6fb
App Center Buildjetpack-installable-builds #5227
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

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.

As I wrote elsewhere, +1 for using selectors over blocks since this way iOS does the lifecycle management for us 👏

There are already tests setup for this object. This could be a good occasion to add to them.

Comment on lines 265 to 272
UserDefaults(suiteName: WPAppGroupName)?.setValue(AccountHelper.isLoggedIn, forKey: AppConfiguration.Widget.Stats.userDefaultsLoggedInKey)

if !AccountHelper.isLoggedIn {
HomeWidgetTodayData.delete()
HomeWidgetThisWeekData.delete()
HomeWidgetAllTimeData.delete()
if !AccountHelper.isLoggedIn {
HomeWidgetTodayData.delete()
HomeWidgetThisWeekData.delete()
HomeWidgetAllTimeData.delete()

UserDefaults(suiteName: WPAppGroupName)?.setValue(nil, forKey: AppConfiguration.Widget.Stats.userDefaultsSiteIdKey)
WidgetCenter.shared.reloadTimelines(ofKind: AppConfiguration.Widget.Stats.todayKind)
WidgetCenter.shared.reloadTimelines(ofKind: AppConfiguration.Widget.Stats.thisWeekKind)
WidgetCenter.shared.reloadTimelines(ofKind: AppConfiguration.Widget.Stats.allTimeKind)
}
UserDefaults(suiteName: WPAppGroupName)?.setValue(nil, forKey: AppConfiguration.Widget.Stats.userDefaultsSiteIdKey)
Copy link
Contributor

Choose a reason for hiding this comment

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

One could DRY and this a tiny bit by storing the UserDefatuls instance and adding an early return

let defaults = UserDefaults(suiteName: WPAppGroupName)

defaults.setValue...

guard !AccountHelper.isLoggedIn else { return }

...

🤷‍♂️

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 66ff6fb

Copy link
Contributor

@jkmassel jkmassel left a comment

Choose a reason for hiding this comment

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

+1 to what Gio said – thanks for making it so this can't be used wrong!

@crazytonyli crazytonyli enabled auto-merge July 4, 2023 22:53
@crazytonyli crazytonyli merged commit c803cd2 into trunk Jul 4, 2023
@crazytonyli crazytonyli deleted the fix-memory-leak-in-notification-observers-1 branch July 4, 2023 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants