The Drunken Landlady


Published on

ArrayCollection: Advanced Filtering & Data Sharing

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

The Drunken Landlady

  1. 1. ArrayCollection:<br />The Drunken Landlady<br />Ben Schmidtke III<br />Digital Primates<br /><br />Twitter: stunnedgrowth<br />
  2. 2. Who Am I<br />Ben Schmidtke III<br />Consultant - Digital Primates IT Consulting Group<br />Flash Platform Developer for 11 years<br />Adobe Certified Instructor<br />Play Irish Fiddle & Concertina<br />Twitter: stunnedgrowth<br />
  3. 3. Agenda<br />ArrayCollection (Overview)<br />Standard Filtering<br />Externalizing Filter<br />Compound Filtering<br />Sharing Collections, Better<br />Questions<br />
  4. 4. Disclaimer<br />Concepts in compound filtering are bare bones that would require additional work in order to optimize for performance<br />
  5. 5. Disclaimer 2<br />Attempting the following solutions with HierarchicalCollectionView and GroupingCollection may require additional customization.<br />
  6. 6. ArrayCollection<br />“The ArrayCollection class is a wrapper class that exposes an Array as a collection…”<br />Is a class that presents different views of the underlying array<br />Can be sorted and filtered without modifying the source array.<br />
  7. 7. ArrayCollection<br />ArrayCollection is comprised of 3 critical pieces:<br />Array: Holds the data<br />ArrayList: Manages I/O to the Array, indexing, a lot of overhead here<br />ListCollectionView: Responds to ArrayList events, manages filtering and sorting<br />
  8. 8. ArrayCollection<br />
  9. 9. In Other Words<br />ArrayCollection is a handy wrapper for a source Array<br />Methods for Adding & Removing Data<br />Provides sorting and filtering<br />Bindable: dispatches events<br />
  10. 10. However…<br />ArrayCollection is<br />Forces coupling of filter function to use location<br />Difficult to share data efficiently<br />Difficult to keep multiple ArrayCollections in sync when adding & removing items<br />Performance Issues & Overhead<br />
  11. 11. Definition<br />“View”<br />Is a object that visibly displays content to be viewed by a pair of eyes<br />Is a collection of data providing a look at the underlying data to be consumed by a display object<br />
  12. 12. Standard Filtering<br />Filtering a ArrayCollection alters the data view but not the underlying data<br />To filter, uses filterFunction<br />A filterFunction runs every time the data in the collection changes<br />
  13. 13. Filter Function<br />Can be run manually by calling refresh() or setting the function again<br />Returns True or False if the item should be visible in the collection view<br />
  14. 14. Example Function<br />public function flexFeedsOnlyFilter( item:Object ):Boolean<br />{<br />if( !item || !(item is FeedItem) )<br />{<br />return false;<br />}<br />varfItem:FeedItem = (item as FeedItem);<br />if(fItem.subject == FeedTypes.FLEX)<br />{<br />return true;<br />}<br />return false;<br />}<br />
  15. 15. I Have Filtering Issues<br />Filter function’s end up in random classes scattered all over application.<br />Not easily reused<br />Couples filtering to view<br />Maintenance, difficult to find to change<br />Data replication/solutions so not affect source data model<br />
  16. 16. I Have Filtering Issues<br />Difficulty sharing filtered data with application, bad practices<br />Compound filtering difficult<br />Have to call refresh()<br />Refresh does a lot more than filter…<br />And more…<br />
  17. 17. Guideline<br />When describing a filter function’s functionality, if the word “and” is in the sentence, it’s doing too much.<br />Keep it simple<br />Limit it to one task<br />
  18. 18. Filtering Tasks<br />Task 1: Externalize the filterFunction<br />Task 2: Auto refresh<br />Task 3: Compound filtering<br />
  19. 19. Task 1: Externalize Function<br />Filter Function will:<br />Be wrapped in external class<br />Filter Function will contain all logic specific to the objective of the filter<br />Allow for easy manipulation of a property that varies the outcome of the results<br />
  20. 20. We need a few things<br />Base Interface<br />filterFunction (item:Object):Boolean<br />Base Class<br />Extends EventDispatcher<br />Implements Interface<br />Allows for override of filterFunction<br />
  21. 21. Create Filter Class<br />Create new class<br />Extend base filter class<br />Move filter code into filterFunction method<br />Add property to change outcome<br />Apply to collection<br />
  22. 22. Externalized<br />Easily reused<br />Much easier to UnitTest<br />Operates on data passed to it instead of reaching out<br />No longer coupled to the part of the application applying the filter<br />
  23. 23. Task 2: Autorun<br />Filter Function will:<br />Dispatch events when it should be run<br />Alter ArrayCollection:<br />Add new filter property<br />Automatically run filter when set<br />Watch for changes and refresh()<br />
  24. 24. Auto Running<br />Less code to maintain<br />No need to call refresh() anymore<br />Be careful when working with values that change rapidly, causing performance problems<br />
  25. 25. Task 3: Compound Filtering<br />ArrayCollection will:<br />Allow for multiple filters to be applied<br />Will refresh when any of them change<br />
  26. 26. Sharing Collection Data<br />Typically a ArrayCollection needs to be manipulated for a variety of reasons:<br />Sorting<br />Filtering<br />Data Modification<br />
  27. 27. Common Issue<br />ArrayCollection needs to be filtered or without affecting the data model because the data is being used in multiple views.<br />
  28. 28. Common Solution<br />varnewColl:ArrayCollection = <br /> new ArrayCollection( masterColl.source );<br />
  29. 29. Sharing Option #1<br />new ArrayCollection( master.source );<br />
  30. 30. The Problem?<br />Any changes to the master collection will not be reflected in the new collection without knowing to call refresh()<br />unless it is a property change event on existing data<br />
  31. 31. Sharing Option #2<br />Change ArrayCollection to accept a list<br />new ArrayCollection ( master.list );<br />
  32. 32. Perks To List Sharing<br />Any changes made to the data will automatically update in all ArrayCollections<br />Sorting & Filtering are unique<br />Smaller memory footprint with only one ArrayList<br />Less to manage<br />
  33. 33. Task 4: Modify ArrayCollection<br />Modify ArrayCollection constructor to accept a IList<br />Change Model to return shared list collections<br />
  34. 34. Putting It All Together<br />Example of single shared collection in multiple views uniquely sorted with compound filtering and all responding to data changes.<br />
  35. 35. Sharing Option #3<br />Uses masterColl.source<br />All sub collections handed out are stored<br />Watcher Object watches for changes on master collection<br />Notifies sub collections of changes<br />
  36. 36. Thank You, Questions?<br />Ben Schmidtke III<br />Consultant - Digital Primates IT Consulting Group<br /><br />