Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Async & Leaks (GDG Android Talks 2014)

Async & Leaks (GDG Android Talks 2014)

En esta charla hablaremos de diferentes formas de hacer trabajos asíncronos y de cómo evitar memory/Activity leaks en Android.
Video: No fue grabado.

Evelio Tarazona Cáceres

August 09, 2014
Tweet

More Decks by Evelio Tarazona Cáceres

Other Decks in Programming

Transcript

  1. Evelio Tarazona Cáceres Lead Android Software Engineer - Telly, Inc.

    GDG Android Talks - 09/08/2014 Async & Leaks evel.io/async
  2. Main thread p u b l i c c l

    a s s H e l l o W o r l d { p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { / / . . . } } J A V A /
  3. Main thread p u b l i c f i

    n a l c l a s s A c t i v i t y T h r e a d { p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { / / . . . } } J A V A /
  4. Main thread Encolar, despachar y consumir eventos Input Activity Lifecycle

    Layout & draw (pre-L) ... Nuestro código · · · · · · /
  5. Thread p u b l i c v o i

    d o n C l i c k ( V i e w v ) { n e w T h r e a d ( n e w R u n n a b l e ( ) { p u b l i c v o i d r u n ( ) { B i t m a p b = l o a d I m a g e F r o m N e t w o r k ( ) ; m I m a g e V i e w . s e t I m a g e B i t m a p ( b ) ; } } ) . s t a r t ( ) ; } J A V A android-developers.blogspot.com/2009/05/painless-threading.html /
  6. Thread + Handler p u b l i c v

    o i d o n C l i c k ( V i e w v ) { n e w T h r e a d ( n e w R u n n a b l e ( ) { p u b l i c v o i d r u n ( ) { f i n a l B i t m a p b = l o a d I m a g e F r o m N e t w o r k ( ) ; m I m a g e V i e w . p o s t ( n e w R u n n a b l e ( ) { p u b l i c v o i d r u n ( ) { m I m a g e V i e w . s e t I m a g e B i t m a p ( b ) ; } } ) ; } } ) . s t a r t ( ) ; } J A V A android-developers.blogspot.com/2009/05/painless-threading.html /
  7. AsyncTask p u b l i c v o i

    d o n C l i c k ( V i e w v ) { n e w D o w n l o a d I m a g e T a s k ( ) . e x e c u t e ( " h t t p : / / e x a m p l e . c o m / i m a g e . p n g " ) ; } p r i v a t e c l a s s D o w n l o a d I m a g e T a s k e x t e n d s A s y n c T a s k < . . . > { p r o t e c t e d B i t m a p d o I n B a c k g r o u n d ( S t r i n g . . . u r l s ) { r e t u r n l o a d I m a g e F r o m N e t w o r k ( u r l s [ 0 ] ) ; } p r o t e c t e d v o i d o n P o s t E x e c u t e ( B i t m a p r e s u l t ) { m I m a g e V i e w . s e t I m a g e B i t m a p ( r e s u l t ) ; } } J A V A android-developers.blogspot.com/2009/05/painless-threading.html /
  8. Loader p u b l i c c l a

    s s A p p L i s t L o a d e r e x t e n d s A s y n c T a s k L o a d e r < L i s t < A p p E n t r y > > { / / . . . @ O v e r r i d e p u b l i c L i s t < A p p E n t r y > l o a d I n B a c k g r o u n d ( ) { / / D o w o r k h e r e } / / . . . } J A V A developer.android.com/reference/android/content/AsyncTaskLoader.html /
  9. IntentService p u b l i c c l a

    s s R S S P u l l S e r v i c e e x t e n d s I n t e n t S e r v i c e { @ O v e r r i d e p r o t e c t e d v o i d o n H a n d l e I n t e n t ( I n t e n t w o r k I n t e n t ) { / / G e t s d a t a f r o m t h e i n c o m i n g I n t e n t S t r i n g d a t a S t r i n g = w o r k I n t e n t . g e t D a t a S t r i n g ( ) ; . . . / / D o w o r k h e r e , b a s e d o n t h e c o n t e n t s o f d a t a S t r i n g . . . } } J A V A developer.android.com/training/run-background-service/create-service.html /
  10. Groundy p u b l i c c l a

    s s E x a m p l e T a s k e x t e n d s G r o u n d y T a s k { @ O v e r r i d e p r o t e c t e d T a s k R e s u l t d o I n B a c k g r o u n d ( ) { / / l o t s o f c o d e r e t u r n s u c c e e d e d ( ) . a d d ( " t h e _ r e s u l t " , " s o m e r e s u l t " ) ; } } J A V A @ O n S u c c e s s ( E x a m p l e T a s k . c l a s s ) p u b l i c v o i d o n S u c c e s s ( @ P a r a m ( " t h e _ r e s u l t " ) S t r i n g r e s u l t ) { / / d o s o m e t h i n g w i t h t h e r e s u l t } J A V A github.com/telly/groundy /
  11. /

  12. Memory leaks Non static inner classes (e.g. callbacks) static fields

    (e.g. Singletons) Regular fields Service leaks · · · · /