Skip to content

[objective_c] Swift style KVO pattern #2344

New issue

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

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

Already on GitHub? # to your account

Merged
merged 18 commits into from
Jun 12, 2025
Merged

[objective_c] Swift style KVO pattern #2344

merged 18 commits into from
Jun 12, 2025

Conversation

liamappelbe
Copy link
Contributor

@liamappelbe liamappelbe commented Jun 5, 2025

Usage

  1. Implement the Observer protocol
  2. Call object.addObserver(observer, ...)
  3. addObserver returns an Observation, which must be held for as long as you want to keep receiving updates
  4. The Observation object holds strong refs to the Observer and the observed object, keeping them alive
  5. Call observation.remove(), or allow it to go out of scope and be GC'd, and the observations will stop

Implementation notes

  • Observer is an ordinary ffigen'd ObjC protocol defined in observer.h.
  • addObserver is an extension method on NSObject.
  • Observation is a thin wrapper around DOBJCObservation.
  • DOBJCObservation is defined in observer.h/m and holds a strong reference to both the observed NSObject and the Observer. These references are dropped if .remove() is called.
  • The observed NSObject internally holds a weak reference to the Observer.
  • It's fine for the user's implementation of Observer to hold a strong reference to the observed NSObject. But it would create reference cycles if they held strong references, even indirectly, to the Observer or the Observation (this is pretty much the same restriction we have for any protocol implementation).

Fixes #1508

Copy link

github-actions bot commented Jun 5, 2025

PR Health

Breaking changes ⚠️
Package Change Current Version New Version Needed Version Looking good?
objective_c Breaking 8.0.0 8.1.0-wip 9.0.0
Got "8.1.0-wip" expected >= "9.0.0" (breaking changes)
⚠️

This check can be disabled by tagging the PR with skip-breaking-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

API leaks ⚠️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
objective_c _FinalizablePointer
_Version

This check can be disabled by tagging the PR with skip-leaking-check.

License Headers ✔️
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/jni/lib/src/third_party/generated_bindings.dart
pkgs/native_doc_dartifier/example/native_doc_dartifier_example.dart
pkgs/native_doc_dartifier/lib/native_doc_dartifier.dart
pkgs/native_doc_dartifier/lib/src/native_doc_dartifier_base.dart
pkgs/objective_c/lib/src/ns_input_stream.dart

@coveralls
Copy link

coveralls commented Jun 5, 2025

Coverage Status

coverage: 91.485% (-0.08%) from 91.56%
when pulling 798c530 on objc_kvo_swift
into b57cb5a on main.

@liamappelbe liamappelbe changed the title [WIP] Swift style KVO pattern [objective_c] Swift style KVO pattern Jun 11, 2025
@liamappelbe liamappelbe marked this pull request as ready for review June 11, 2025 04:13
@liamappelbe liamappelbe changed the title [objective_c] Swift style KVO pattern WIP [objective_c] Swift style KVO pattern Jun 11, 2025
@liamappelbe liamappelbe marked this pull request as draft June 11, 2025 04:33
@liamappelbe liamappelbe marked this pull request as ready for review June 11, 2025 05:22
@liamappelbe liamappelbe changed the title WIP [objective_c] Swift style KVO pattern [objective_c] Swift style KVO pattern Jun 11, 2025
@HosseinYousefi
Copy link
Member

The added tests are failing on mac though

@liamappelbe
Copy link
Contributor Author

The added tests are failing on mac though

Yeah, still trying to debug.

@liamappelbe
Copy link
Contributor Author

Gave up trying to fix the test and just disabled the failing bits. Filed #2352

@liamappelbe liamappelbe merged commit e69b7c3 into main Jun 12, 2025
23 checks passed
@liamappelbe liamappelbe deleted the objc_kvo_swift branch June 12, 2025 05:16
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a mechanism for registering a KVO observer
3 participants