`
darrenzhu
  • 浏览: 807207 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

flex中的uid(analogy to java hashcode)

阅读更多
1. Be careful when defining uid property in your class
if you defined a uid property in your class (in regardless of its type, no matter what type it is), you will certainly find something wrong in your application soon, such as you cannot select some rows in List control and nodes in Tree control, why do we have this kind of strange issue when we don’t see any compiler error and runtime error, the reason is because flex data provider controls use a unique identifier (UID) to track data items, flex can automatically create and manage UIDs, but if you own class has uid property, flex will use the class defined uid, when the value of uid property in your object is not unique, then you will get above described strange issue. 
By the way, you can use logger. debug ("selected object's uid:"+UIDUtil.getUID(selectedObject)); to check the uid value of your selected object.
Please see the following section for more details about uid in flex.

2. Data providers and the uid property
Flex data provider controls use a unique identifier (UID) to track data items. Flex can automatically create and manage UIDs. However, there are circumstances when you must supply your own uid property by implementing the IUID interface, and there are circumstances when supplying your own uid property improves processing efficiency.
Because the Object and Array classes are dynamic, you normally do not do anything special for data objects whose items belong to these classes. However, you should consider implementing the IUID if your data object items belong to custom classes that you define.
Note: When Flex creates a UID for an object, such as an item in an ArrayCollection, it adds the UID as an mx_internal_uid property of the item. Flex creates mx_internal_uid properties for any objects that are dynamic and do not have bindable properties. To avoid having Flex create mx_internal_uid properties, the object class should do any of the following things: have at least one property with a [Bindable] metadata tag; implement the IUID interface; or have a uid property with a value.

If Flex must consider two or more different objects to be identical, the objects must implement the IUID interface so that you can assign the same uid value to multiple objects. A typical case where you must implement the IUID interface is an application that uses paged collections. As the cursor moves through the collection, a particular item might be pulled down from the server and released from memory repeatedly. Every time the item is pulled into memory, a new object is created to represent the item. If you need to compare items for equality, Flex should consider all objects that represent the same item to be the same "thing."
More common than the case where you must implement the IUID interface is the case where you can improve processing efficiency by doing so. As a general rule, you do not implement the IUID interface if the data provider elements are members of dynamic classes. Flex can automatically create a uid property for these classes. There is still some inefficiency, however, so you might consider implementing the IUID interface if processing efficiency is particularly important.

In all other cases, Flex uses the Dictionary mechanism to manage the uid, which might not be as efficient as supplying your own UID.
The IUID interface contains a single property, uid, which is a unique identifier for the class member, and no methods. Flex provides a UIDUtil class that uses a pseudo-random-number generator to create an identifier that conforms to the standard GUID format. Although this identifier is not guaranteed to be universally unique, it should be unique among all members of your class. To implement a class that uses the UIDUtil class, such as a Person class that has fields for a first name, last name, and ID, you can use the following pattern:
package {
    import mx.core.IUID;
    import mx.utils.UIDUtil;

    [Bindable]
    public class Person implements IUID {
        public var id:String;
        public var firstName:String;
        public var lastName:String;
        private var _uid:String;

        public function Person() {
            _uid = UIDUtil.createUID();
        }

        public function get uid():String {
            return _uid;
        }

        public function set uid(value: String):void {
            // Do nothing, the constructor created the uid.
        }
    }
}

You do not need to use the UIDUtil class in a case where the objects contain a uniquely-identifying field such as an employee ID. In this case, you can use the person's ID as the uid property, because the uid property values uniquely identify the object only in the data provider. The following example implements this approach:
package
{
    import mx.core.IUID;

    [Bindable]
    public class Person implements IUID {
        public var employee_id:String;
        public var firstName:String;
        public var lastName:String;

        public function get uid(): String {
            return employee_id;
        }

        public function set uid(value: String): void {
            employee_id=value;
        }
    }
}

Note: Object cloning does not manage or have a relationship with UIDs, so if you clone something that has an internal UID you must also change that internal UID. UIDs are stored on mx_internal_uid only for dynamic Objects. Instances of data classes that implement IUID store their UIDs in a uid property, so that is the property that must be changed after cloning.


3. Some guys who suffering from this problem
This has cost me a lot of time. If you implement a custom ValueObject which has a public property called uid (which was actually an user id in my app) and all of the items in the list have the same user id this will lead into serious problems. There should be at least a compiler warning or something that assigning uid can lead to problems if it isn't unique for all items. I searched about 2 hours for this entry.

An alternative to implementing iuid in order to have multiple objects as equal, is to check for the existence of an object in memory before instantiating it.  A simple factory method that updates instead of creates when appropriate will do the job.  Often a primary key for the database table will serve the purpose to identify the equal object.
 
It might be useful to use a database primary key as a light weight iuid, but I have not yet tried this because the value of my primary keys change from a negative number to a positive one when the row is inserted in the database.
分享到:
评论

相关推荐

    501_word_analogy

    全英文的魔电书,对留学生比较有帮助,在ccebook下载的不知各位大侠赏不赏脸。。。

    Java编程基础

    is fi rst illustrated through real-life analogy. Corresponding Java language constructs are introduced in an integrated fashion to demonstrate the programming required to simulate the real-world ...

    Deep Visual Analogy-Making.pdf

    ### 深度视觉类比生成 (Deep Visual Analogy-Making) #### 摘要 本文探讨了深度视觉类比生成技术,这是一种重要的图像理解任务,不仅涉及识别单个图像中的内容,还涉及到图像间的关联及生成相关图像。近年来,深度...

    JAVA面试题目 JAVA面试题目一览表

    and engage with professionals in online forums to stay updated on the latest trends and technologies in the JAVA field."例如:“我经常参加行业活动,阅读文章和博客,并在在线论坛中与同行交流,以保持对...

    Visual Attribute Transfer through Deep Image Analogy(个人阅读与注释)

    这是论文Visual Attribute Transfer through Deep Image Analogy的原文,内含有小编本人的理解与注释,谨以此分享个人经验和向广大大牛请教,如有错误,请不吝指出,万分感谢!

    Analogy.LogViewer:可自定义的日志查看器,具有创建自定义提供程序的能力。 可以与C#,C ++,Python,Java等一起使用

    类比日志查看器 日志查看器,可以解析日志文件并流式传输C#,Python,Java和其他应用程序的日志。 您还可以实现自己的解析器或请求为其他日志添加新的解析器内容一般的类比日志查看器是Windows操作系统的多用途日志...

    Analogy-0.1-py3-none-any.whl.zip

    标题中的"Analogy-0.1-py3-none-any.whl.zip"是一个压缩文件,它包含了一个名为"Analogy-0.1-py3-none-any.whl"的Python软件包。这种格式通常用于分发Python的第三方库。下面将详细解释相关知识点。 1. **Python ...

    Analogy-开源

    这些插件可以无缝集成到Analogy中,为音乐制作增添无尽的创意选项。例如,portaudio_x86.dll是PortAudio库的一个组件,用于跨平台的音频输入和输出,它确保了Analogy与其他VST插件的兼容性。 此外,Analogy的GUI...

    Deep-image-analogy-TensorFlow-master_python_patchmatch算法_patchma

    使用python实现的patchmatch算法,该代码主要实现补丁匹配算法,有两个patchmatch代码:PatchMatch.py??和PatchMatch_Bidirectional.py。最后一个用于代码DeepImageAnalogy

    Analogy.LogViewer.PowerToys:PowerToys日志的类比解析器

    "Analogy.LogViewer.PowerToys" 是一个专为开发者设计的工具,用于解析和查看应用程序的日志文件。这个工具基于C#编程语言开发,旨在帮助IT专业人士和开发者更有效地理解和诊断软件运行时的问题。在日志分析过程中,...

    Superconductivity of metallic boron in MgB2

    and an analogy to Al, we estimate the coupling constant to be of order 1. Together with high phonon frequencies, which we estimate via zone-center frozen phonon calculations to be between 300 and 700 ...

    Analogy-and-Assessment:来自(NUI)梅努斯大学的类比和评估源代码

    纽伊姆 (NUI) 梅努斯大学源代码。 2014 年 12 月 12 日 ... ROS 生成和 ROS 评估工具包含在各自的文件夹中。 请使用 GenerateROS.html 查找 GenerateROS 程序的相关帮助文件和 ROS 评估工具的 rosat.html。

    Neural Turing Machines

    By analogy to Turing's enrichment of finite-state machines with an infinite memory tape, the addition of external memory to RNNs creates a "Neural Turing Machine." #### Practical Applications and ...

    Word-Analogy:使用GLoVe词嵌入法找到词的类比

    词类比 话题 给定三个词a,b和c,我们将必须生成下一个词d,以使a和b之间的关系与c和d之间的关系相同。 结果 这是单词嵌入的一些例子 学分 1.该存储库基于设置,。 2.杰弗里·彭宁顿(Jeffrey Pennington),理查德...

    3D论文-A Generic Framework for Efficient 2-D and 3-D Facial Expression Analogy

    本文提出了一种通用框架,用于高效地进行二维与三维面部表情模拟(Facial Expression Analogy)。该框架的目标是为计算机动画专业人士提供一种工具,将任意来源面部的表情映射到任意目标面部上。在过去的研究中,...

    Color_Transfer_Histogram_Analogy:[CGI 2020]“使用直方图类比进行深层颜色转移”的正式实施

    使用直方图类比进行深层色彩转移 图:各种源图像和参考图像对上的颜色转移结果。为了可视化,参考图像被裁剪为与其他图像相同的尺寸。 该存储库包含以下论文的官方PyTorch实现: 李俊勇,孙亨锡,李健熙,李钟hy,...

    Real-Time-Log-Server:gRPC日志服务器(Windows Services)接收然后转发消息到Analogy Log Viewer

    Analogy Log服务器支持以下实时数据: Windows事件日志(可在服务器设置中配置)任何实现Proto文件的gRPC客户端(目前已经完全实现了C#,Python和JAVA(Poc)客户端)存在以下模块:努吉特版本描述主要类比日志...

    Python-该项目是DeepImageAnalogy的Python实现

    Deep Image Analogy的基本思想是通过学习两个图像对之间的底层结构关系,然后将这种关系应用到第三个图像上,以实现视觉效果的转移。这种方法通常包括三个主要步骤:特征提取、关系学习和结果合成。在特征提取阶段,...

    High-Precision, Consistent EKF-based Visual-Inertial Odometry.pdf

    visual and inertial sensors.We term this estimation task visual-inertial odometry (VIO), in analogy to the well-known visual-odometry problem. We present a detailed study of EKF-based VIO algorithms, ...

Global site tag (gtag.js) - Google Analytics