A JSManaged object wraps a JSValue object, adding “conditional retain” behavior to provide automatic memory management of values. The primary use case for a managed value is to store a JavaScript value in an Objective-C or Swift object that is itself exported to JavaScript.
SDKs
- iOS 7.0+
- macOS 10.9+
- tvOS 9.0+
Framework
- JavaScriptCore
Overview
Important
Do not store a non-managed JSValue object in a native object that is exported to JavaScript. Because a JSValue object references its enclosing JSContext object, this action creates a retain cycle, keeping the context from being deallocated.
A managed value’s “conditional retain” behavior ensures that its underlying JavaScript value is retained as long as either of the following conditions is true:
The JavaScript value is reachable through the JavaScript object graph (that is, not subject to JavaScript garbage collection)
The
JSManagedobject is reachable through the Objective-C or Swift object graph, as reported to the JavaScriptCore virtual machine using theValue addmethodManaged Reference(_: with Owner:)
However, if neither of these conditions are true, the managed value sets its value property to nil, releasing the underlying JSValue object.
Note
On its own, a JSManaged object behaves similarly to an ARC weak reference to its underlying JSValue object—that is, if you do not use the add method to add “conditional retain” behavior, the managed value’s value property automatically becomes nil when the JavaScript garbage collector destroys the underlying JavaScript value.