The Drunken Landlady

Preview:

DESCRIPTION

ArrayCollection: Advanced Filtering & Data Sharing

Citation preview

ArrayCollection:The Drunken Landlady

Ben Schmidtke III

Digital Primates

bschmidtke@digitalprimates.net

Twitter: stunnedgrowth

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

Agenda

• ArrayCollection (Overview)

• Standard Filtering

• Externalizing Filter

• Compound Filtering

• Sharing Collections, Better

• Questions

Disclaimer

Concepts in compound filtering are bare bones

that would require additional work in order to

optimize for performance

Disclaimer 2

Attempting the following solutions with

HierarchicalCollectionView and

GroupingCollection may require additional

customization.

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.

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

ArrayCollection

In Other Words

• ArrayCollection is a handy wrapper for a

source Array

• Methods for Adding & Removing Data

• Provides sorting and filtering

• Bindable: dispatches events

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

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

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

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

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;

}

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

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…

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

Filtering Tasks

• Task 1: Externalize the filterFunction

• Task 2: Auto refresh

• Task 3: Compound filtering

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

We need a few things

• Base Interface

– filterFunction (item:Object):Boolean

• Base Class

– Extends EventDispatcher

– Implements Interface

– Allows for override of filterFunction

Create Filter Class

• Create new class

• Extend base filter class

• Move filter code into filterFunction

method

• Add property to change outcome

• Apply to collection

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

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()

Auto Running

• Less code to maintain

• No need to call refresh() anymore

• Be careful when working with values that

change rapidly, causing performance

problems

Task 3: Compound Filtering

• ArrayCollection will:

– Allow for multiple filters to be applied

– Will refresh when any of them change

Sharing Collection Data

• Typically a ArrayCollection needs to be

manipulated for a variety of reasons:

– Sorting

– Filtering

– Data Modification

Common Issue

ArrayCollection needs to be filtered or

without affecting the data model because

the data is being used in multiple views.

Common Solution

var newColl:ArrayCollection =

new ArrayCollection( masterColl.source );

Sharing Option #1

new ArrayCollection( master.source );

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

Sharing Option #2

• Change ArrayCollection to accept a list

new ArrayCollection ( master.list );

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

Task 4: Modify ArrayCollection

• Modify ArrayCollection constructor to

accept a IList

• Change Model to return shared list

collections

Putting It All Together

• Example of single shared collection in

multiple views uniquely sorted with

compound filtering and all responding to

data changes.

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

Thank You, Questions?

Ben Schmidtke III

Consultant - Digital Primates IT Consulting Group

bschmidtke@digitalprimates.net

Recommended