继承自:NSObject
遵守协议:NSObject
导入声明:@import JavaScriptCore;
适用范围:iOS 7.0 及以后
一个 JSManagedValue 对象是用来包装一个 JSValue 对象的,JSManagedValue 对象通过添加“有条件的持有(conditional retain)”行为来实现自动内存管理。一个managed value 的基本用法就是用来在一个要导出(exported)到 JavaScript 的 Objective-C 或者 Swift 对象中存储一个 JavaScript 值。
重要
不要在一个要导出(exported)到 JavaScript 的原生对象中存储一个 non-managed JSValue 对象。因为一个 JSValue 对象引用了它的封闭的(enclosing)JSContext 对象,这样就造成了循环引用,context 就不能被销毁了。
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.
只要下面所列出的情形中有一种出现,一个 managed value 的 “conditional retain” 行为就会保证他的 JavaScript 值是 retained 的:
The JavaScript value is reachable through the JavaScript object graph (that is, not subject to JavaScript garbage collection)
- The JSManagedValue object is reachable through the Objective-C or Swift object graph, as reported to the JavaScriptCore virtual machine using the addManagedReference:withOwner: method
但是,如果这些条件没有一个成立,那个 managed value 就会将它的 value
属性置为 nil
,并且释放 JSValue 对象。
说明
一个 JSManagedValue 对象本身就像是一个对它的 JSValue 的 ARC 弱引用——也就是说,如果你不用 addManagedReference:withOwner: 方法去添加 “conditional retain” 行为,那么当 JavaScript 垃圾回收器(garbage collector )销毁了那个 managed value 对应的 JavaScript 值时,那个 managed value 的 value 属性会自动变成 nil。
NOTE
On its own, a JSManagedValue object behaves similarly to an ARC weak reference to its underlying JSValue object—that is, if you do not use the addManagedReference:withOwner: 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.
一、功能(Tasks)
1.创建一个 Managed Value (Creating a Managed Value)
- initWithValue:
Initializes a managed value with the specified JavaScript value.
Declaration
- (instancetype)initWithValue:(JSValue *)value
Parameters
参数 | 含义 |
---|---|
value | A JavaScript value. |
Return Value
A new managed value.
Discussion
To ensure that the underlying JavaScript value is retained as long as the managed value remains in use in the Objective-C or Swift runtime, report the managed value’s owner to the JavaScriptCore virtual machine using the addManagedReference:withOwner: method.
Availability
Available in iOS 7.0 and later.
+ managedValueWithValue:
Creates a managed value with the specified JavaScript value.
Declaration
+ (JSManagedValue *)managedValueWithValue:(JSValue *)value
Parameters
参数 | 含义 |
---|---|
value | A JavaScript value. |
Return Value
A new managed value.
Discussion
To ensure that the underlying JavaScript value is retained as long as the managed value remains in use in the Objective-C or Swift runtime, report the managed value’s owner to the JavaScriptCore virtual machine using the addManagedReference:withOwner:
method.
Availability
Available in iOS 7.0 and later.
+ managedValueWithValue:andOwner:
Creates a managed value and associates it with an owner.
Declaration
+ (JSManagedValue *)managedValueWithValue:(JSValue *)value
andOwner:(id)owner
Parameters
参数 | 含义 |
---|---|
value | A JavaScript value. |
owner | The Objective-C or Swift object responsible for |
Return Value
A new managed value.
Discussion
Calling this method is equivalent to creating a managed value and then reporting it to the JavaScriptCore virtual machine using the addManagedReference:withOwner: method.
Availability
Available in iOS 8.0 and later.
2.获取 Managed Value(Accessing the Managed Value)
value Property
The managed value’s underlying JavaScript value. (read-only)
Declaration
@property(readonly, strong) JSValue *value
Discussion
If the JavaScript garbage collector removes the underlying value, this property becomes nil
.
Availability
Available in iOS 7.0 and later.
二、文档修订记录(Document Revision History)
下表中所列出的是文档 JSManagedValue Class Reference 的修改记录。
日期 | 说明 |
---|---|
2016-03-21 | 修复排版错误。 |
2015-12-08 | 创建新文档,描述一个 JavaScript 虚拟机(virtual machine)的对象空间(object space)中的值的内存管理。 |
问题(Question)
1.JSManagedValue 对象在内存管理中扮演了什么样的角色?