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

Android Minutes: Synchronizing data

Android Minutes: Synchronizing data

Synchronizing data MOBGEN internal talk between the Android team

Javier de Pedro López

October 25, 2016
Tweet

More Decks by Javier de Pedro López

Other Decks in Programming

Transcript

  1. Architecture evolution Architecture elements Web services and performance Thread manager

    Job scheduler and family When, what and how Q&A TABLE OF CONTENTS
  2. All the requests are made from a thousand lines activity

    with async tasks 1. ARCHITECTURE EVOLUTION
  3. Can contain a memory cache Plain object Easy to handle

    Easy to inject Easy to test Hard threading Dependent from other layer Service Easy to handle Hard to inject Hard to test Easy threading Independent from other layer 2. ARCHITECTURE ELEMENTS: REPOSITORY
  4. The only source of truth Plain object Easy to handle

    Easy to inject Easy to test Hard threading Dependent from other layer Content provider Hard to handle Hard to inject Hard to test Easy threading Independent from other layer 2. ARCHITECTURE ELEMENTS: DATABASE REPOSITORY
  5. With reference to the network client Plain object Easy to

    handle Easy to inject Easy to test Hard threading Dependent from other layer 2. ARCHITECTURE ELEMENTS: NETWORK REPOSITORY
  6. Make it from Android Plain object Easy to handle Easy

    to inject Easy to test Hard threading Dependent from other layer Service Hard to handle Hard to inject Hard to test Easy threading Independent from other layer Make it intelligent Sync adapter Job scheduler (or similar) 2. ARCHITECTURE ELEMENTS: SYNC ENGINE
  7. The backend have to be prepared to make the sync

    more smooth { " s y n c T i m e s t a m p " : 1 4 6 7 3 6 1 8 5 8 9 5 8 , " c r e a t e d " : [ { " n a m e " : " I n s t a n c e 1 0 5 7 " , " m o d u l e " : " 5 7 1 f 3 8 b 9 b b 7 f 3 7 2 9 0 0 a 1 4 c b c " , " c r e a t e d A t " : 1 4 6 4 0 0 0 0 8 7 9 7 4 , " u p d a t e d A t " : n u l l , " d e l e t e d A t " : n u l l , " i d " : " 5 7 4 2 d e 5 7 6 e c 1 3 2 2 f 0 0 d 7 9 6 c 5 " } ] , " u p d a t e d " : [ ] , " d e l e t e d " : [ ] } Not json patch, just timestamp! 3. WEB SERVICES AND PERFORMANCE: BACKEND
  8. How to dispatch in Backend Without timestamp cache With timestamp

    bring only the changes Prepare to receive a bunch of very small requests 3. WEB SERVICES AND PERFORMANCE: BACKEND
  9. How to request in Android First request send without timestamp

    and with cache ag Inmediatly make a request with the timestamp from server to now Request every 'n' time or when the user needs to (internet connection handing) Use a cursor to display it and not ll your memory 3. WEB SERVICES AND PERFORMANCE: FRONTEND
  10. Depending on the device, database used and network Fetch, save

    and display takes... 3. WEB SERVICES AND PERFORMANCE: TIMING 1500 INSTANCES / SEC
  11. Handle all your threading in the same class If you

    are not a hipster that uses RxJava 4. THREAD MANAGER
  12. 3 9 . c a s e S A M

    E _ T H R E A D _ P O L I C Y : 4 0 . f u t u r e = n e w F u t u r e T a s k < > ( r u n n a b l e , n u l l ) ; 4 1 . r u n n a b l e . r u n ( ) ; 4 2 . b r e a k ; 3 2 . s w i t c h ( t h r e a d ) { 3 3 . c a s e P O O L _ Q U E U E _ P O L I C Y : 3 4 . f u t u r e = m P o o l Q u e u e . s u b m i t ( r u n n a b l e ) ; 3 5 . b r e a k ; 3 6 . c a s e S I N G L E _ Q U E U E _ P O L I C Y : 3 7 . f u t u r e = m S i n g l e Q u e u e . s u b m i t ( r u n n a b l e ) ; 3 8 . b r e a k ; 4 3 . d e f a u l t : 4 4 . t h r o w n e w H a l o C o n f i g u r a t i o n E x c e p t i o n ( " U n s u p p o r t e d o p t i o n o r a n A c t i o n o p e r a t i o n " ) ; 4 5 . } 4 6 . r e t u r n f u t u r e ; 4 7 . } 4 8 . }
  13. J o b S c h e d u l

    e r j o b S c h e d u l e r = ( J o b S c h e d u l e r ) g e t A p p l i c a t i o n C o n t e x t ( ) . g e t S y s t e m S e r v i c e ( J O B _ S C H E D U L E R _ S E R V I C E ) ; C o m p o n e n t N a m e c o m p o n e n t N a m e = n e w C o m p o n e n t N a m e ( g e t A p p l i c a t i o n C o n t e x t ( ) , T e s t S e r v i c e . c l a s s ) ; J o b I n f o j o b I n f o = n e w J o b I n f o . B u i l d e r ( 1 , c o m p o n e n t N a m e ) . s e t O v e r r i d e D e a d l i n e ( 1 0 ) . s e t R e q u i r e s C h a r g i n g ( t r u e ) . b u i l d ( ) ; j o b S c h e d u l e r . s c h e d u l e ( j o b I n f o ) ; Only for 5.0+!!! 5. JOB SCHEDULER AND FAMILY
  14. Trigger sample J o b j o b = n

    e w J o b ( n e w A c t i o n ( ) { @ O v e r r i d e p r o t e c t e d v o i d a c t ( ) { / / d o s o m e t h i n g } } ) . w i t h E x t r a ( n e w C o n d i t i o n ( ) { @ O v e r r i d e p u b l i c S t r i n g [ ] g e t A c t i o n ( ) { r e t u r n n e w S t r i n g [ ] { Y O U R _ B R O A R C A S T } ; } } ) ; t r i g g e r . s c h e d u l e ( j o b ) ; 5. JOB SCHEDULER AND FAMILY
  15. Halo scheduler H a l o F r a m

    e w o r k f r a m e w o r k = H a l o . i n s t a n c e ( ) . f r a m e w o r k ( ) ; f r a m e w o r k . s y n c ( ) . s c h e d u l e ( J o b . b u i l d e r ( n e w S c h e d u l e ( ) { @ O v e r r i d e p r o t e c t e d v o i d e x e c ( ) { e n q u e u e O n R e a d y ( a c t i o n ) ; } } ) . n e e d s N e t w o r k ( J o b . N E T W O R K _ T Y P E _ A N Y ) . t h r e a d ( t h r e a d i n g ) . t a g ( " s y n c U s e r " ) . b u i l d ( ) ) ; 5. JOB SCHEDULER AND FAMILY
  16. The big rule is: use every element depending on your

    needs and the time you have for it 6. WHEN, WHAT AND HOW