The Drunken Landlady

36
ArrayCollection: The Drunken Landlady Ben Schmidtke III Digital Primates [email protected] Twitter: stunnedgrowth

description

ArrayCollection: Advanced Filtering & Data Sharing

Transcript of The Drunken Landlady

Page 1: The Drunken Landlady

ArrayCollection:The Drunken Landlady

Ben Schmidtke III

Digital Primates

[email protected]

Twitter: stunnedgrowth

Page 2: The Drunken Landlady

Who Am I

• Ben Schmidtke III

• Consultant - Digital Primates IT Consulting Group

• Flash Platform Developer for 11 years

• Adobe Certified Instructor

• Play Irish Fiddle & Concertina

• Twitter: stunnedgrowth

Page 3: The Drunken Landlady

Agenda

• ArrayCollection (Overview)

• Standard Filtering

• Externalizing Filter

• Compound Filtering

• Sharing Collections, Better

• Questions

Page 4: The Drunken Landlady

Disclaimer

Concepts in compound filtering are bare bones

that would require additional work in order to

optimize for performance

Page 5: The Drunken Landlady

Disclaimer 2

Attempting the following solutions with

HierarchicalCollectionView and

GroupingCollection may require additional

customization.

Page 6: The Drunken Landlady

ArrayCollection

• “The ArrayCollection class is a wrapper class

that exposes an Array as a collection…”

• Is a class that presents different views of the

underlying array

• Can be sorted and filtered without modifying

the source array.

Page 7: The Drunken Landlady

ArrayCollection

• ArrayCollection is comprised of 3 critical

pieces:

– Array: Holds the data

– ArrayList: Manages I/O to the

Array, indexing, a lot of overhead here

– ListCollectionView: Responds to ArrayList

events, manages filtering and sorting

Page 8: The Drunken Landlady

ArrayCollection

Page 9: The Drunken Landlady

In Other Words

• ArrayCollection is a handy wrapper for a

source Array

• Methods for Adding & Removing Data

• Provides sorting and filtering

• Bindable: dispatches events

Page 10: The Drunken Landlady

However…

• ArrayCollection is

– Forces coupling of filter function to use

location

– Difficult to share data efficiently

– Difficult to keep multiple ArrayCollections in

sync when adding & removing items

– Performance Issues & Overhead

Page 11: The Drunken Landlady

Definition

• “View”

– Is a object that visibly displays content to be

viewed by a pair of eyes

– Is a collection of data providing a look at the

underlying data to be consumed by a display

object

Page 12: The Drunken Landlady

Standard Filtering

• Filtering a ArrayCollection alters the data

view but not the underlying data

• To filter, uses filterFunction

• A filterFunction runs every time the data

in the collection changes

Page 13: The Drunken Landlady

Filter Function

• Can be run manually by calling refresh()

or setting the function again

• Returns True or False if the item should

be visible in the collection view

Page 14: The Drunken Landlady

Example Functionpublic function flexFeedsOnlyFilter( item:Object ):Boolean

{

if( !item || !(item is FeedItem) )

{

return false;

}

var fItem:FeedItem = (item as FeedItem);

if(fItem.subject == FeedTypes.FLEX)

{

return true;

}

return false;

}

Page 15: The Drunken Landlady

I Have Filtering Issues

• Filter function’s end up in random classes

scattered all over application.

– Not easily reused

– Couples filtering to view

– Maintenance, difficult to find to change

– Data replication/solutions so not affect

source data model

Page 16: The Drunken Landlady

I Have Filtering Issues

• Difficulty sharing filtered data with

application, bad practices

• Compound filtering difficult

• Have to call refresh()

– Refresh does a lot more than filter…

• And more…

Page 17: The Drunken Landlady

Guideline

• When describing a filter function’s

functionality, if the word “and” is in the

sentence, it’s doing too much.

• Keep it simple

• Limit it to one task

Page 18: The Drunken Landlady

Filtering Tasks

• Task 1: Externalize the filterFunction

• Task 2: Auto refresh

• Task 3: Compound filtering

Page 19: The Drunken Landlady

Task 1: Externalize Function

• Filter Function will:

– Be wrapped in external class

– Filter Function will contain all logic specific

to the objective of the filter

– Allow for easy manipulation of a property

that varies the outcome of the results

Page 20: The Drunken Landlady

We need a few things

• Base Interface

– filterFunction (item:Object):Boolean

• Base Class

– Extends EventDispatcher

– Implements Interface

– Allows for override of filterFunction

Page 21: The Drunken Landlady

Create Filter Class

• Create new class

• Extend base filter class

• Move filter code into filterFunction

method

• Add property to change outcome

• Apply to collection

Page 22: The Drunken Landlady

Externalized

• Easily reused

• Much easier to UnitTest

• Operates on data passed to it instead of

reaching out

• No longer coupled to the part of the

application applying the filter

Page 23: The Drunken Landlady

Task 2: Autorun

• Filter Function will:

– Dispatch events when it should be run

• Alter ArrayCollection:

– Add new filter property

– Automatically run filter when set

– Watch for changes and refresh()

Page 24: The Drunken Landlady

Auto Running

• Less code to maintain

• No need to call refresh() anymore

• Be careful when working with values that

change rapidly, causing performance

problems

Page 25: The Drunken Landlady

Task 3: Compound Filtering

• ArrayCollection will:

– Allow for multiple filters to be applied

– Will refresh when any of them change

Page 26: The Drunken Landlady

Sharing Collection Data

• Typically a ArrayCollection needs to be

manipulated for a variety of reasons:

– Sorting

– Filtering

– Data Modification

Page 27: The Drunken Landlady

Common Issue

ArrayCollection needs to be filtered or

without affecting the data model because

the data is being used in multiple views.

Page 28: The Drunken Landlady

Common Solution

var newColl:ArrayCollection =

new ArrayCollection( masterColl.source );

Page 29: The Drunken Landlady

Sharing Option #1

new ArrayCollection( master.source );

Page 30: The Drunken Landlady

The Problem?

• Any changes to the master collection will

not be reflected in the new collection

without knowing to call refresh()

– unless it is a property change event on

existing data

Page 31: The Drunken Landlady

Sharing Option #2

• Change ArrayCollection to accept a list

new ArrayCollection ( master.list );

Page 32: The Drunken Landlady

Perks To List Sharing

• Any changes made to the data will

automatically update in all

ArrayCollections

• Sorting & Filtering are unique

• Smaller memory footprint with only one

ArrayList

• Less to manage

Page 33: The Drunken Landlady

Task 4: Modify ArrayCollection

• Modify ArrayCollection constructor to

accept a IList

• Change Model to return shared list

collections

Page 34: The Drunken Landlady

Putting It All Together

• Example of single shared collection in

multiple views uniquely sorted with

compound filtering and all responding to

data changes.

Page 35: The Drunken Landlady

Sharing Option #3

• Uses masterColl.source

• All sub collections handed out are stored

• Watcher Object watches for changes on

master collection

• Notifies sub collections of changes

Page 36: The Drunken Landlady

Thank You, Questions?

Ben Schmidtke III

Consultant - Digital Primates IT Consulting Group

[email protected]