繼承自:NSObject
遵守協(xié)議:NSObject
導(dǎo)入聲明:@import JavaScriptCore;
適用范圍:iOS 7.0 及以后
一個(gè) JSManagedValue 對象是用來包裝一個(gè) JSValue 對象的,JSManagedValue 對象通過添加“有條件的持有(conditional retain)”行為來實(shí)現(xiàn)自動內(nèi)存管理。一個(gè)managed value 的基本用法就是用來在一個(gè)要導(dǎo)出(exported)到 JavaScript 的 Objective-C 或者 Swift 對象中存儲一個(gè) JavaScript 值姿鸿。
重要
不要在一個(gè)要導(dǎo)出(exported)到 JavaScript 的原生對象中存儲一個(gè) non-managed JSValue 對象。因?yàn)橐粋€(gè) JSValue 對象引用了它的封閉的(enclosing)JSContext 對象兜挨,這樣就造成了循環(huán)引用,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.
只要下面所列出的情形中有一種出現(xiàn)扰路,一個(gè) 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
但是帖渠,如果這些條件沒有一個(gè)成立,那個(gè) managed value 就會將它的 value
屬性置為 nil
竭宰,并且釋放 JSValue 對象空郊。
說明
一個(gè) JSManagedValue 對象本身就像是一個(gè)對它的 JSValue 的 ARC 弱引用——也就是說,如果你不用 addManagedReference:withOwner: 方法去添加 “conditional retain” 行為切揭,那么當(dāng) JavaScript 垃圾回收器(garbage collector )銷毀了那個(gè) managed value 對應(yīng)的 JavaScript 值時(shí)狞甚,那個(gè) 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.創(chuàng)建一個(gè) Managed Value (Creating a Managed Value)
- initWithValue:
Initializes a managed value with the specified JavaScript value.
Declaration
- (instancetype)initWithValue:(JSValue *)value
Parameters
參數(shù) | 含義 |
---|---|
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
參數(shù) | 含義 |
---|---|
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
參數(shù) | 含義 |
---|---|
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 | 修復(fù)排版錯(cuò)誤孕豹。 |
2015-12-08 | 創(chuàng)建新文檔涩盾,描述一個(gè) JavaScript 虛擬機(jī)(virtual machine)的對象空間(object space)中的值的內(nèi)存管理。 |
問題(Question)
1.JSManagedValue 對象在內(nèi)存管理中扮演了什么樣的角色励背?