realm / realm/SwiftLint

Rule Request: Explicit NotificationCenter usage.

Open
#2,711 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

rule-request
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
New rule request
  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.
    We are making a Mac application and some of the older macOS versions still store targets without incrementing their weak reference count, so forgetting to remove the observer will result in a crash, that's why we made utility functions for NSObjects to store the observations (since they can also be block based, just removing self wouldn't be enough) and remove them from notification center when the object deallocates.
    According to this, iOS9 and macOS 10.11 are the minimum version where this is not needed anymore:
    https://developer.apple.com/documentation/foundation/notificationcenter/1413994-removeobserver

  2. Provide several examples of what would and wouldn't trigger violations.
    Trigger:
    NotificationCenter.default.addObserver(self, selector:selector, name:.notificationName, object:nil
    NotificationCenter.default.addObserver(forName:.notificationName, object: nil, queue: nil, using: {})
    Not trigger:
    NotificationCenter.default.post(.notificationName)

  3. Should the rule be configurable, if so what parameters should be configurable?
    no parameters

  4. Should the rule be opt-in or enabled by default? Why?
    opt-in since not everyone has to support OS versions where this is a problem.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation file or test is named. Start by reviewing existing SwiftLint rule implementations and rule tests to find the appropriate entry points, then verify that selector- and block-based addObserver calls are flagged, post calls are not, and the rule is opt-in.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.