Flex data is prone to mistakes: Common misuse and errors

Source: Internet
Author: User

Forgot to release and bear the risk of Internal Check Leakage

You can useTags or braces are used to implement the binding function. However, these methods produce overhead. In addition, you cannot use these technologies to remove bindings. If you want to optimize high-performance applications, you can useBindingUtilsClass to bind your object.BindingUtilsThere are two ways to use the class:

  • bindProperty()The static method is used to bind a common property.
  • bindSetter()The method is a static method used to bind the setter function.

Let's take a look.bindPropertyMethod signature:

public static function bindProperty(     site:Object, prop:String,     host:Object, chain:Object,     commitOnly:Boolean = false,     useWeakReference:Boolean = false):ChangeWatcher

Site and host parameters indicate the targetLocationAndSourceObject. When the handler only calls a committed change event, you cancommitOnlySettrueWhen the handler can call a commit and non-commit change event, you can set the commitonlyfalse(Default ).

useWeakReferenceThe parameter allows you to define that the reference of the host isStrongOrWeak. Strong reference (default) prevents the host from being garbage collected. However, weak references cannot do this. The following example contains a text input program and a simple component. When the textinput control is pre-initializedbindPropertyMethodpreinitializeHandler()Function.

<? XML version = "1.0" encoding = "UTF-8"?>
<S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns: S = "Library: // ns.adobe.com/flex/spark"

Xmlns: MX = "Library: // ns.adobe.com/flex/mx"
Minwidth = "1024" minheight = "768">
<FX: SCRIPT>

<! [CDATA [
ImportMX. Binding. utils. bindingutils;
ImportMX. Events. flexevent;

Protected FunctionPreinitializehandler (Event: flexevent ):Void

{
Bindingutils. bindproperty (label,"Text", Textinput,"Text");

}
]>
</FX: SCRIPT>

<S: layout>
<S: verticallayout/>
</S: layout>

<S: textinput id = "textinput" preinitialize = "preinitializehandler (event)"/>


<S: Label id = "label"/>

</S: Application>

Below isbindSetterExample:

Public static function bindsetter (setter: function, host: object,
Chain: object,
Commitonly: Boolean = false,
Useweakreference: Boolean = false): changewatcher

 

setterThe parameter defines the setter method, so that when the current value of the chain is changed, the setter method can be called using the independent variable of this value. WherehostIndicates the source object, andchainAttribute name.commitOnlyAnduseWeakReferenceParameters andbindPropertyIs the same.

Here is an example that usesbindSetter:

<? XML version = "1.0" encoding = "UTF-8"?>
<S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009"

Xmlns: S = "Library: // ns.adobe.com/flex/spark"
Xmlns: MX = "Library: // ns.adobe.com/flex/mx"
Minwidth = "1024" minheight = "768">

<FX: SCRIPT>
<! [CDATA [
Import MX. Binding. utils. bindingutils;
Import MX. Events. flexevent;

Protected function preinitializehandler (Event: flexevent): void

{
Bindingutils. bindsetter (bindingsetter, textinput, "text ");
}

Private function bindingsetter (STR: string): void
{
Label. Text = STR;
}
]>
</FX: SCRIPT>

<S: layout>
<S: verticallayout/>

</S: layout>

<S: textinput id = "textinput" preinitialize = "preinitializehandler (event)"/>

<S: Label id = "label"/>


</S: Application>

In the background,ChangeWatcherClass can beBindingUtilsClass. It supportsweakReference, so when you setuseWeakReferenceSettrueThe garbage collector automatically cleans the host to avoid potential memory leakage.

When you useweakWhen you use it to complete the task,ChangeWatcherThe object must be unmonitored. You are obligated to handle this task to ensure that there is no memory leakage. The best way to handle this task isbindPropertyThe returned static method is assigned toChangeWatcherVariable (because the static method can returnChangeWatcherInstance, you can assign it to a variable ). You can useunwatchMethod, as you can see in the following example (see figure 5 ).

<? XML version = "1.0" encoding = "UTF-8"?>
<S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns: S = "Library: // ns.adobe.com/flex/spark"

Xmlns: MX = "Library: // ns.adobe.com/flex/mx"
Minwidth = "1024" minheight = "768">

<FX: SCRIPT>
<! [CDATA [
Import MX. Binding. utils. changewatcher;
Import MX. Binding. utils. bindingutils;
Import MX. Events. flexevent;

Private var change: changewatcher;

Protected function preinitializehandler (Event: flexevent): void

{
Change = bindingutils. bindproperty (label, "text ",
Textinput, "text", false, true );
}
Protected function clickhandler (Event: mouseevent): void
{
Change. unwatch ();
Change = NULL;
}
]>
</FX: SCRIPT>

<S: layout>

<S: verticallayout/>
</S: layout>

<S: textinput id = "textinput"
Preinitialize = "preinitializehandler (event)"/>

<S: Label id = "label"/>

<S: button label = "Stop binding" Click = "clickhandler (event)"/>

</S: Application>

Figure 5.

One useBindingUtilsClass and use buttons to bind and remove simple applications

TextInputOftextThe property isLabelOftextProperty bound when youTextInputThe text data is copiedLabelComponenttextAttribute. When you are about to remove the binding, clickStop Binding. This operation unmonitors the corresponding attributes and sets the corresponding objectnullIn this way, the object will be cleared during garbage collection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.