Successfully reported this slideshow.
Core Data in Motion
RubyMotion #inspect May 28-29, 2014
Lori Olson
• lori@wndx.com	

• www.wndx.com	

• @wndxlori	

• Freelance developer,
mentor, trainer	

• CANADA!
KUDOS
Changing the world, oneThankYou at a time!	

kudosnow.com
Thanks to my client Kudos, for being understanding about m...
i Develop
• Ruby (mostly)	

• Rails	

• iOS (RubyMotion)	

• JRuby	

• Javascript (some)	

• jQuery	

• Maps/Charts	

• Ja...
i Teach
• Ruby on Rails for Real Developers
(RoR4Real)	

• Rails for iOS Developers (Rails4iOS)	

• Ladies Learning Code -...
i Speak
• 2014 	

• Scottish Ruby Conference, RubyMotion #inspect	

• 2013	

• RubyConf AU, NSScotland	

• 2012	

• Confoo...
The Task
Why?
Learning RubyMotion
How?
To rewrite a (mobile) web application 	

as an iOS application
What?
Wells In My BackYard
The Challenges
LOTS of data
On a Map
In a List (table)
Filtering by Location
How hard could it
be?
Oh, not that much data
244,292
RubyMotion gems/DSL’s
• Nitron	

• MotionDataWrapper	

• MotionModel	

• MotionMigrate	

• many, many others
Problems
• RubyMotion gems/DSL’s hide a lot	

• Straight iOS Objective-C development
relies on Xcode magic (hides a lot)	
...
The Real Problem
• Sometimes you just need to understand
how to solve problems at the most basic
API code level, and the a...
iOS Basics
• Ray Wenderlich!	

• http://www.raywenderlich.com/934/core-
data-tutorial-for-ios-getting-started
It’s not as scary as it looks
What?
• Models (entities) in code	

• Relationships in code	

• Loading data	

• Optimization
Models in code
Models in Code
• Hey, isn’t there a sample app for that?	

• Locations	

• https://github.com/HipByte/
RubyMotionSamples/t...
location.rb (model)
location.rb (entity)
location.rb (properties)
location_store.rb (store)
location_store.rb (MoM)
location_store.rb (psc)
Persistent Store Coordinator
location_store.rb (MoC)
Managed Object Context
That was easy
Or was it?
Overly simplistic
• Doesn’t work with multiple models with
relationships	

• You need a reference to an entity, to define
a...
Chicken & Egg problem
Relationships in code
Solution
• Define your entities. First.	

• Lazily! define your entities’ properties	

• properties = (attributes and relati...
Just the entity...
Wait a minute...
MoC, lazily defined properties
attributes
attributes
relationships
relationships
properties
The “other” model
More of the same
Relationships
• Done	

• With that, you can pretty much define any
kind of model and relationship you want, in
code, no mag...
Data Loading
iOS Core Data Basics
• Back to Ray	

• http://www.raywenderlich.com/12170/core-
data-tutorial-how-to-preloadimport-
existi...
The RubyMotion way
Inside our store, create a load method
Read the data file (JSON)
Add data to store
Wait, what does add_bank do again?
add_bank
That was easy
Or was it?
How many wells do I
have to load again?
244,292
Oh.
Houston, we have a
problem
Read in the WHOLE file?
save for every add?
add, save, add, save
That won’t work either
• add, add, add ...., save once
Ok, no, that won’t work either
Batching… ?
• add, add, add, save, add, add, add, save
Looked promising, but still ended up with out of memory errors
Wait, what?
Back to Core Data
Basics
• Thankfully, Ray figured that out.	

• Updated the tutorial to operate as an OS X
(console) app.	...
New, improved load
streaming (CSV) load
create_bank (no save)
saves, every 100
progress every 100/1000
Catch the odd ones
Ta da!
244,292 wells loaded
And then your table
view chokes on 244k
items
Optimization
Back to Basics (again)
• Ray sure has a lot of good material, doesn’t
he?	

• http://www.raywenderlich.com/999/core-
data-...
Why?
• NSFetchedResultsController gives us huge
performance benefits	

• Avoids loading all the data in memory at
once
bank_store.rb
failed_bank_table_view_controller.rb
failed_bank_table_view_controller.rb
wall of text. let’s break that down
number of rows
cell for row
configure cell
the delegate
In Summary
• Create models in code	

• Create relationships between models in
code	

• Pre-loading data	

• LOTS of data	
...
Please use Core Data!
Finnegan!
Core Data in Motion
• It’s an ebook!	

• Almost finished!	

• http://coredatainmotion.com
That’s All, Folks!
• Code: 

https://github.com/wndxlori/WNDXRubyMotion/
tree/master/FailedBankCD	

• Questions? Comments?...
Core Data in RubyMotion #inspect
Core Data in RubyMotion #inspect
Upcoming SlideShare
Loading in …5
×

Core Data in RubyMotion #inspect

820 views

Published on

Core Data doesn't have to be hard. Step by step, learn how to use Core Data in code, creating models and relationships. Loading data and efficiently displaying that data.

Published in: Software, Technology
  • Be the first to comment

  • Be the first to like this

Core Data in RubyMotion #inspect

  1. 1. Core Data in Motion RubyMotion #inspect May 28-29, 2014
  2. 2. Lori Olson • lori@wndx.com • www.wndx.com • @wndxlori • Freelance developer, mentor, trainer • CANADA!
  3. 3. KUDOS Changing the world, oneThankYou at a time! kudosnow.com Thanks to my client Kudos, for being understanding about my speaking engagements.
  4. 4. i Develop • Ruby (mostly) • Rails • iOS (RubyMotion) • JRuby • Javascript (some) • jQuery • Maps/Charts • Javascript Testing
  5. 5. i Teach • Ruby on Rails for Real Developers (RoR4Real) • Rails for iOS Developers (Rails4iOS) • Ladies Learning Code - Ruby Introduction
  6. 6. i Speak • 2014 • Scottish Ruby Conference, RubyMotion #inspect • 2013 • RubyConf AU, NSScotland • 2012 • Confoo, RailsConf,Aloha RubyConf • 2011 • jQueryConf, Madison Ruby • 2010 • Conferencia Rails • 2009 • RailsConf
  7. 7. The Task
  8. 8. Why? Learning RubyMotion
  9. 9. How? To rewrite a (mobile) web application as an iOS application
  10. 10. What? Wells In My BackYard
  11. 11. The Challenges
  12. 12. LOTS of data
  13. 13. On a Map
  14. 14. In a List (table)
  15. 15. Filtering by Location
  16. 16. How hard could it be?
  17. 17. Oh, not that much data 244,292
  18. 18. RubyMotion gems/DSL’s • Nitron • MotionDataWrapper • MotionModel • MotionMigrate • many, many others
  19. 19. Problems • RubyMotion gems/DSL’s hide a lot • Straight iOS Objective-C development relies on Xcode magic (hides a lot) • Complex data is complex • Large data is large • What do I do when I reach the limitations of these solutions?
  20. 20. The Real Problem • Sometimes you just need to understand how to solve problems at the most basic API code level, and the abstractions (and magic) just get in the way
  21. 21. iOS Basics • Ray Wenderlich! • http://www.raywenderlich.com/934/core- data-tutorial-for-ios-getting-started
  22. 22. It’s not as scary as it looks
  23. 23. What? • Models (entities) in code • Relationships in code • Loading data • Optimization
  24. 24. Models in code
  25. 25. Models in Code • Hey, isn’t there a sample app for that? • Locations • https://github.com/HipByte/ RubyMotionSamples/tree/master/ios/ Locations • MVCS pattern
  26. 26. location.rb (model)
  27. 27. location.rb (entity)
  28. 28. location.rb (properties)
  29. 29. location_store.rb (store)
  30. 30. location_store.rb (MoM)
  31. 31. location_store.rb (psc) Persistent Store Coordinator
  32. 32. location_store.rb (MoC) Managed Object Context
  33. 33. That was easy
  34. 34. Or was it?
  35. 35. Overly simplistic • Doesn’t work with multiple models with relationships • You need a reference to an entity, to define a relationship fully
  36. 36. Chicken & Egg problem
  37. 37. Relationships in code
  38. 38. Solution • Define your entities. First. • Lazily! define your entities’ properties • properties = (attributes and relationships)
  39. 39. Just the entity... Wait a minute...
  40. 40. MoC, lazily defined properties
  41. 41. attributes
  42. 42. attributes
  43. 43. relationships
  44. 44. relationships
  45. 45. properties
  46. 46. The “other” model More of the same
  47. 47. Relationships • Done • With that, you can pretty much define any kind of model and relationship you want, in code, no magic required.
  48. 48. Data Loading
  49. 49. iOS Core Data Basics • Back to Ray • http://www.raywenderlich.com/12170/core- data-tutorial-how-to-preloadimport- existing-data-updated But of course, his way is the Xcode way. We need to translate to RubyMotion.
  50. 50. The RubyMotion way Inside our store, create a load method
  51. 51. Read the data file (JSON)
  52. 52. Add data to store Wait, what does add_bank do again?
  53. 53. add_bank
  54. 54. That was easy
  55. 55. Or was it?
  56. 56. How many wells do I have to load again?
  57. 57. 244,292 Oh.
  58. 58. Houston, we have a problem
  59. 59. Read in the WHOLE file?
  60. 60. save for every add? add, save, add, save
  61. 61. That won’t work either • add, add, add ...., save once Ok, no, that won’t work either
  62. 62. Batching… ? • add, add, add, save, add, add, add, save Looked promising, but still ended up with out of memory errors
  63. 63. Wait, what?
  64. 64. Back to Core Data Basics • Thankfully, Ray figured that out. • Updated the tutorial to operate as an OS X (console) app. • RubyMotion can do that, too. Instead of running INSIDE the simulator (in your device)
  65. 65. New, improved load
  66. 66. streaming (CSV) load
  67. 67. create_bank (no save)
  68. 68. saves, every 100 progress every 100/1000
  69. 69. Catch the odd ones
  70. 70. Ta da! 244,292 wells loaded
  71. 71. And then your table view chokes on 244k items
  72. 72. Optimization
  73. 73. Back to Basics (again) • Ray sure has a lot of good material, doesn’t he? • http://www.raywenderlich.com/999/core- data-tutorial-for-ios-how-to-use- nsfetchedresultscontroller
  74. 74. Why? • NSFetchedResultsController gives us huge performance benefits • Avoids loading all the data in memory at once
  75. 75. bank_store.rb
  76. 76. failed_bank_table_view_controller.rb
  77. 77. failed_bank_table_view_controller.rb wall of text. let’s break that down
  78. 78. number of rows
  79. 79. cell for row
  80. 80. configure cell
  81. 81. the delegate
  82. 82. In Summary • Create models in code • Create relationships between models in code • Pre-loading data • LOTS of data • Efficiently displaying data
  83. 83. Please use Core Data! Finnegan!
  84. 84. Core Data in Motion • It’s an ebook! • Almost finished! • http://coredatainmotion.com
  85. 85. That’s All, Folks! • Code: 
 https://github.com/wndxlori/WNDXRubyMotion/ tree/master/FailedBankCD • Questions? Comments? • @wndxlori • lori@wndx.com • http://www.linkedin.com/in/loriolson • http://www.wndx.com

×