Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Me and my importers

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Wird geladen in …3
×

Hier ansehen

1 von 124 Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)

Ähnlich wie Me and my importers (20)

Anzeige

Aktuellste (20)

Anzeige

Me and my importers

  1. 1. ME AND MY IMPORTERS DONNY WALS
  2. 2. EXPLORING THE FIELD PROLOGUE:
  3. 3. LET’S SYNC ALL DATA FROM THE BACK- END INSTEAD OF REQUESTING TINY BITS. IT WILL BE GREAT! Back-end development ME AND MY IMPORTERS
  4. 4. PROBLEM 1, API DEPENDENCIES
  5. 5. App Settings Remote app config
  6. 6. Events Information about all the visible events in the app App Settings Remote app config
  7. 7. Events Information about all the visible events in the app Genres All the possible genres that are assigned to events App Settings Remote app config
  8. 8. Events Information about all the visible events in the app Genres All the possible genres that are assigned to events Userdata Information for the currently logged in user App Settings Remote app config
  9. 9. Events Information about all the visible events in the app Genres All the possible genres that are assigned to events Userdata Information for the currently logged in user Prizes Favorites Profile App Settings Remote app config
  10. 10. Events Information about all the visible events in the app Genres All the possible genres that are assigned to events Userdata Information for the currently logged in user Prizes Favorites Profile Homepage Which data to display on the homepage App Settings Remote app config
  11. 11. Events Information about all the visible events in the app Genres All the possible genres that are assigned to events Userdata Information for the currently logged in user Prizes Favorites Profile Homepage Which data to display on the homepage App Settings Remote app config 1
  12. 12. Events Information about all the visible events in the app Genres All the possible genres that are assigned to events Userdata Information for the currently logged in user Prizes Favorites Profile Homepage Which data to display on the homepage App Settings Remote app config 1 2
  13. 13. Events Information about all the visible events in the app Genres All the possible genres that are assigned to events Userdata Information for the currently logged in user Prizes Favorites Profile Homepage Which data to display on the homepage App Settings Remote app config 1 3 2
  14. 14. Events Information about all the visible events in the app Genres All the possible genres that are assigned to events Userdata Information for the currently logged in user Prizes Favorites Profile Homepage Which data to display on the homepage App Settings Remote app config 1 3 2 4 4
  15. 15. PROBLEM 2, DUPLICATION
  16. 16. EVENT Location Categories Artists Gallery Sponsor Country Texts
  17. 17. EVENT Location Categories Artists Gallery Sponsor Country Texts A LOT OF DATA IS DUPLICATED 😭
  18. 18. PROBLEM 3, STORAGE
  19. 19. THE USER SHOULD ALWAYS BE ABLE TO USE THE UI, EVEN WHEN A SYNC IS TAKING PLACE IN THE BACKGROUND. Me ME AND MY IMPORTERS
  20. 20. ME AND MY IMPORTERS AGENDA
  21. 21. ME AND MY IMPORTERS AGENDA ▸ Chapter 1: Chaining operations
  22. 22. ME AND MY IMPORTERS AGENDA ▸ Chapter 1: Chaining operations ▸ Chapter 2: Optimizing the data
  23. 23. ME AND MY IMPORTERS AGENDA ▸ Chapter 1: Chaining operations ▸ Chapter 2: Optimizing the data ▸ Chapter 3: Storing in CoreData
  24. 24. CHAINING OPERATIONS CHAPTER 1:
  25. 25. ADVANCED NSOPERATIONS WWDC 2015
  26. 26. Events Userdata Homepage Genres Information about all the visible events in the app All the possible genres that are assigned to events Information for the currently logged in user Prizes Favorites Profile Which data to display on the homepage App Settings Remote app config 1 3 2 4 4
  27. 27. Sync operation
  28. 28. App Settings Sync operation
  29. 29. App Settings Genres Sync operation
  30. 30. App Settings Genres Sync operation
  31. 31. App Settings Events Genres Sync operation
  32. 32. App Settings Events Genres Sync operation
  33. 33. App Settings Events Genres Homepage Sync operation
  34. 34. App Settings Events Genres Homepage Sync operation
  35. 35. App Settings Events Genres Userdata Homepage Sync operation
  36. 36. App Settings Events Genres Prizes Favorites Profile Userdata Homepage Sync operation
  37. 37. App Settings Events Genres Prizes Favorites Profile Userdata Homepage Sync operation
  38. 38. App Settings Events Genres Prizes Favorites Profile Userdata Homepage Completion Sync operation
  39. 39. App Settings Events Genres Prizes Favorites Profile Userdata Homepage Completion Sync operation
  40. 40. App Settings EventsGenres Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  41. 41. App Settings EventsGenres Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  42. 42. EventsGenres Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  43. 43. EventsGenres Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  44. 44. Events Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  45. 45. Events Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  46. 46. Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  47. 47. Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  48. 48. Prizes Favorites Profile Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  49. 49. Prizes Favorites Userdata HomepageCompletion SYNC OPERATION QUEUE USERDATA QUEUE
  50. 50. Prizes Favorites Userdata Completion SYNC OPERATION QUEUE USERDATA QUEUE
  51. 51. Prizes Userdata Completion SYNC OPERATION QUEUE USERDATA QUEUE
  52. 52. Completion SYNC OPERATION QUEUE USERDATA QUEUE
  53. 53. Completion SYNC OPERATION QUEUE USERDATA QUEUE
  54. 54. SYNC OPERATION QUEUE USERDATA QUEUE
  55. 55. EVENT FETCH OPERATION GENRE FETCH OPERATION
  56. 56. EVENT FETCH OPERATION override func generateDependencies() -> [DWOperation] { return [GenreFetchOperation()] } GENRE FETCH OPERATION
  57. 57. EVENT FETCH OPERATION override func generateDependencies() -> [DWOperation] { return [GenreFetchOperation()] } GENRE FETCH OPERATION override func generateDependencies() -> [DWOperation] { return [SyncAppSettingsOperation()] }
  58. 58. DWOPERATION QUEUE
  59. 59. DWOPERATION QUEUE override func addOperation(_ op: Operation) { // details, details for dependency in operation.generateDependencies() { operation.addDependency(dependency) addOperation(dependency) } operation.willEnqueue() }
  60. 60. FETCH USER DATA OPERATION
  61. 61. FETCH USER DATA OPERATION init(operations: [Operation]) { super.init() internalQueue.isSuspended = true internalQueue.delegate = self internalQueue.addOperation(startingOperation) for operation in operations { internalQueue.addOperation(operation) } }
  62. 62. FETCH USER DATA OPERATION
  63. 63. FETCH USER DATA OPERATION final func operationQueue(_ operationQueue: MyOperationQueue, operationDidFinish operation: Operation, withErro errors: [Error]) { aggregatedErrors.append(contentsOf: errors) if operation === finishingOperation { internalQueue.isSuspended = true finish(errors: aggregatedErrors) } else if operation !== startingOperation { operationDidFinish(operation: operation, withErrors: errors) } }
  64. 64. App Settings Events Genres Userdata Prizes Favorites Profile Homepage Completion Sync operation
  65. 65. ME AND MY IMPORTERS PROBLEM 1 SOLVED
  66. 66. ME AND MY IMPORTERS PROBLEM 1 SOLVED ▸ Operations simplify dependencies
  67. 67. ME AND MY IMPORTERS PROBLEM 1 SOLVED ▸ Operations simplify dependencies ▸ Operations can execute in parallel or serially
  68. 68. ME AND MY IMPORTERS PROBLEM 1 SOLVED ▸ Operations simplify dependencies ▸ Operations can execute in parallel or serially ▸ Operations can contain their own private queues for complex nesting
  69. 69. OPTIMIZING THE DATA CHAPTER 2:
  70. 70. EVENT Location Categories Artists Gallery Sponsor Country Texts A LOT OF DATA IS DUPLICATED 😭
  71. 71. EVENT Location Categories Artists Gallery Sponsor Country Texts THE SAME OBJECTS ARE IN THE JSON MANY, MANY, MANY TIMES
  72. 72. PROCESSING IMPLEMENTATION V1
  73. 73. PROCESSING IMPLEMENTATION V1 Loop over events in JSON response
  74. 74. PROCESSING IMPLEMENTATION V1 Find event in DB / Create new event Loop over events in JSON response
  75. 75. PROCESSING IMPLEMENTATION V1 Find event in DB / Create new event Find location in DB / Create new location Loop over events in JSON response
  76. 76. PROCESSING IMPLEMENTATION V1 Find event in DB / Create new event Find location in DB / Create new location Loop over events in JSON response Find artist in DB / Create new artist
  77. 77. PROCESSING IMPLEMENTATION V1 Find event in DB / Create new event Find location in DB / Create new location Loop over events in JSON response Find artist in DB / Create new artist Find gallery in DB / Create new gallery
  78. 78. PROCESSING IMPLEMENTATION V1 Find event in DB / Create new event Find location in DB / Create new location Loop over events in JSON response Find artist in DB / Create new artist Find text in DB / Create new text Find gallery in DB / Create new gallery
  79. 79. PROCESSING IMPLEMENTATION V1 Find event in DB / Create new event Find location in DB / Create new location Loop over events in JSON response Find artist in DB / Create new artist Find sponsor in DB / Create new sponsor Find text in DB / Create new text Find gallery in DB / Create new gallery
  80. 80. PROCESSING IMPLEMENTATION V1 Find event in DB / Create new event Find location in DB / Create new location Loop over events in JSON response Find artist in DB / Create new artist Find sponsor in DB / Create new sponsor Find text in DB / Create new text Find gallery in DB / Create new gallery Persist DB
  81. 81. BUT WHAT ABOUT THE DUPLICATED DATA?
  82. 82. THE DEFINITION OF INSANITY IS DOING THE SAME THING OVER AND OVER AGAIN AND EXPECTING A DIFFERENT RESULT. Not Albert Einstein ME AND MY IMPORTERS
  83. 83. PROCESSING IMPLEMENTATION V2
  84. 84. PROCESSING IMPLEMENTATION V2 Loop over events in JSON response
  85. 85. PROCESSING IMPLEMENTATION V2 Collect all unique events & relations Loop over events in JSON response
  86. 86. PROCESSING IMPLEMENTATION V2 Collect all unique events & relations Fetch / create objects Loop over events in JSON response
  87. 87. PROCESSING IMPLEMENTATION V2 Collect all unique events & relations Fetch / create objects Loop over events in JSON response Loop over event objects
  88. 88. PROCESSING IMPLEMENTATION V2 Collect all unique events & relations Fetch / create objects Loop over events in JSON response Loop over event objects Assign relations to event object
  89. 89. PROCESSING IMPLEMENTATION V2 Collect all unique events & relations Fetch / create objects Loop over events in JSON response Persist DB Loop over event objects Assign relations to event object
  90. 90. COLLECT ALL UNIQUE EVENTS AND RELATIONS?
  91. 91. COLLECT ALL UNIQUE EVENTS AND RELATIONS? for event in events { guard let eventId = event["id"] as? Int else { continue } eventIds.append(eventId) if let location = event["location"] as? DWJSON, let locationId = location["id"] as? Int { locationIds.append(locationId) } // etc... }
  92. 92. EACH OBJECT IS FETCHED/ CREATED AND UPDATED ONCE
  93. 93. ME AND MY IMPORTERS PROBLEM 2 SOLVED
  94. 94. ME AND MY IMPORTERS PROBLEM 2 SOLVED ▸ Collecting all unique ids for objects
  95. 95. ME AND MY IMPORTERS PROBLEM 2 SOLVED ▸ Collecting all unique ids for objects ▸ Fetching all objects in one go
  96. 96. ME AND MY IMPORTERS PROBLEM 2 SOLVED ▸ Collecting all unique ids for objects ▸ Fetching all objects in one go ▸ Each object is only updated once
  97. 97. ME AND MY IMPORTERS PROBLEM 2 SOLVED ▸ Collecting all unique ids for objects ▸ Fetching all objects in one go ▸ Each object is only updated once ▸ Pulling apart the data before processing it can have great benefits
  98. 98. STORING IN COREDATA CHAPTER 3:
  99. 99. THE USER SHOULD ALWAYS BE ABLE TO USE THE UI, EVEN WHEN A SYNC IS TAKING PLACE IN THE BACKGROUND. Me ME AND MY IMPORTERS
  100. 100. EASY, JUST USE MULTIPLE CONTEXTS You ME AND MY IMPORTERS
  101. 101. Read context MAIN QUEUE
  102. 102. Read context MAIN QUEUE Import context PRIVATE QUEUE
  103. 103. THE UI IS FLASHING A LOT WHEN I LAUNCH THE APP The almighty userbase ME AND MY IMPORTERS
  104. 104. UI UPDATING IMPLEMENTATION V1
  105. 105. UI UPDATING IMPLEMENTATION V1 Managed object context did save
  106. 106. UI UPDATING IMPLEMENTATION V1 Update UI Managed object context did save
  107. 107. EACH OPERATION SAVES THE CONTEXT, RESULTING IN MANY UI UPDATES
  108. 108. UI UPDATING IMPLEMENTATION V2
  109. 109. UI UPDATING IMPLEMENTATION V2 Sync helper did start notification
  110. 110. UI UPDATING IMPLEMENTATION V2 Managed object context did save Sync helper did start notification
  111. 111. UI UPDATING IMPLEMENTATION V2 Hold on to notification Managed object context did save Sync helper did start notification
  112. 112. UI UPDATING IMPLEMENTATION V2 Hold on to notification Managed object context did save Sync helper did start notification Sync helper did end notification
  113. 113. UI UPDATING IMPLEMENTATION V2 Hold on to notification Managed object context did save Sync helper did start notification Sync helper did end notification Fire one notification for each entity
  114. 114. ME AND MY IMPORTERS PROBLEM 3 SOLVED
  115. 115. ME AND MY IMPORTERS PROBLEM 3 SOLVED ▸ Use multiple contexts for reading and writing
  116. 116. ME AND MY IMPORTERS PROBLEM 3 SOLVED ▸ Use multiple contexts for reading and writing ▸ Store notifications while you’re importing
  117. 117. ME AND MY IMPORTERS PROBLEM 3 SOLVED ▸ Use multiple contexts for reading and writing ▸ Store notifications while you’re importing ▸ Filter notifications and fire only the notifications you really need to fire
  118. 118. ME AND MY IMPORTERS TAKEAWAYS
  119. 119. ME AND MY IMPORTERS TAKEAWAYS ▸ Operations can make complex processes easier to manage
  120. 120. ME AND MY IMPORTERS TAKEAWAYS ▸ Operations can make complex processes easier to manage ▸ You might need to pre-process imported data for performance
  121. 121. ME AND MY IMPORTERS TAKEAWAYS ▸ Operations can make complex processes easier to manage ▸ You might need to pre-process imported data for performance ▸ Inserting a layer between your app and CoreData can help with managing an import pipeline
  122. 122. 🍷

×