Skip to content

Commit e8abc23

Browse files
[3.2] Mention NativeCallable in the ObjC multithreading limitations (#5336)
Mention the new `NativeCallable` API in the ObjC multithreading limitations section. It's no longer true that *all* Dart callbacks will cause the app to crash when invoked from the wrong thread. Fixes #5333 --------- Co-authored-by: Parker Lougheed <[email protected]>
1 parent 57844da commit e8abc23

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/guides/libraries/objective-c-interop.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,14 @@ and the way Apple's APIs handle multithreading:
320320
The first two points mean that a callback created in one isolate
321321
might be invoked on a thread running a different isolate,
322322
or no isolate at all.
323-
This will cause your app to crash.
324-
You can work around this limitation by writing some
325-
Objective-C code that intercepts your callback and
326-
forwards it over a [`Dart_Port`]({{site.dart-api}}/dart-ffi/NativePort.html)
327-
to the correct isolate.
328-
For an example of this,
329-
see the implementation of [`package:cupertino_http`][].
323+
Depending on the type of callback you are using,
324+
this could cause your app to crash.
325+
Callbacks created using
326+
[`Pointer.fromFunction`][] or [`NativeCallable.isolateLocal`][]
327+
must be invoked on the owner isolate's thread,
328+
otherwise they will crash.
329+
Callbacks created using [`NativeCallable.listener`][]
330+
can be safely invoked from any thread.
330331

331332
The third point means that directly calling some Apple APIs
332333
using the generated Dart bindings might be thread unsafe.
@@ -347,6 +348,10 @@ and doesn't have constraints about which thread it's called from.
347348
You can safely interact with Objective-C code,
348349
as long as you keep these limitations in mind.
349350

351+
[`Pointer.fromFunction`]: {{site.dart-api}}/dart-ffi/Pointer/fromFunction.html
352+
[`NativeCallable.isolateLocal`]: {{site.dart-api}}/dart-ffi/NativeCallable/isolateLocal.html
353+
[`NativeCallable.listener`]: {{site.dart-api}}/dart-ffi/NativeCallable/listener.html
354+
350355
## Swift example
351356

352357
This [example]({{page.swift_example}}) demonstrates how to

0 commit comments

Comments
 (0)