Core Data in Motion

2,314 views

Published on

Have you tried to use CoreData in RubyMotion, only to get lost in the quagmire of simplistic or confusing examples, DSL's and gems? Have you been asking yourself these questions: Do I have to use XCode to create a CoreData model?; How do relationships work in code work anyway?; How do I pre-load data into my CoreData store?; What is an NSFetchedResultsController, and why do I need one?

We'll delve into each of these questions, and review the surprisingly simple, elegant solutions that RubyMotion can provide.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,314
On SlideShare
0
From Embeds
0
Number of Embeds
251
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Core Data in Motion

  1. 1. Core Data in Motion NSScotland 2013 - Oct 19/20 Monday, 21 October, 13
  2. 2. Who? • Lori M Olson • @wndxlori • on Github too • and ADN Monday, 21 October, 13
  3. 3. i Develop • • Monday, 21 October, 13 Ruby (mostly) • • • Rails iOS (RubyMotion) JRuby Javascript (some) • • • jQuery Maps/Charts Javascript Testing
  4. 4. i Teach • Ruby on Rails for Real Developers (RoR4Real) • Rails for iOS Developers (Rails4iOS) • Ladies Learning Code - Ruby Introduction Monday, 21 October, 13
  5. 5. i Speak • • • • • Monday, 21 October, 13 2013 • RubyConf AU, NSScotland 2012 • Confoo, RailsConf, Aloha RubyConf 2011 • jQueryConf, Madison Ruby 2010 • Conferencia Rails 2009 • RailsConf
  6. 6. Warning: LOTS of code Monday, 21 October, 13
  7. 7. The Story • To rewrite a (mobile) web application (WIMBY) as an iOS application • Using RubyMotion • WIMBY = Wells In My Back Yard Monday, 21 October, 13
  8. 8. The Challenges • LOTS of data • On a Map • In a list (table view) • Filtering by location Monday, 21 October, 13
  9. 9. Oh, not much data Monday, 21 October, 13
  10. 10. RubyMotion gems/DSL’s • Nitron • MotionDataWrapper • MotionModel • MotionMigrate Monday, 21 October, 13
  11. 11. iOS Basics • Ray Wenderlich! • http://www.raywenderlich.com/934/coredata-tutorial-for-ios-getting-started Monday, 21 October, 13
  12. 12. 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? Monday, 21 October, 13
  13. 13. 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 Monday, 21 October, 13
  14. 14. It’s not as scary as it looks Monday, 21 October, 13
  15. 15. What? • Models (entities) in code • Relationships in code • Loading data • Optimization Monday, 21 October, 13
  16. 16. Models in code Monday, 21 October, 13
  17. 17. Models in Code • Hey, isn’t there a sample app for that? • Locations • https://github.com/HipByte/ RubyMotionSamples/tree/master/ios/ Locations • MVCS pattern Monday, 21 October, 13
  18. 18. location.rb (model) Monday, 21 October, 13
  19. 19. location.rb (entity) Monday, 21 October, 13
  20. 20. location.rb (properties) Monday, 21 October, 13
  21. 21. location_store.rb (store) Monday, 21 October, 13
  22. 22. location_store.rb (MoM) Monday, 21 October, 13
  23. 23. location_store.rb (psc) Monday, 21 October, 13
  24. 24. location_store.rb (MoC) Monday, 21 October, 13
  25. 25. That was easy Monday, 21 October, 13
  26. 26. Or was it? Monday, 21 October, 13
  27. 27. Monday, 21 October, 13
  28. 28. Overly simplistic • Doesn’t work with multiple models with relationships • You need a reference to an entity, to define a relationship fully Monday, 21 October, 13
  29. 29. Chicken & Egg problem Monday, 21 October, 13
  30. 30. Relationships in code http://www.slowfamilyonline.com/2011/12/slow-news-day-hooray-for-low-tech-toys/tinkertoys/ Monday, 21 October, 13
  31. 31. Solution • Define your entities. First. • Lazily! define your entities’ properties • properties = (attributes and relationships) Monday, 21 October, 13
  32. 32. Another example • Ray Wenderlich’s Failed Bank example has 2 models • So let’s just start there Monday, 21 October, 13
  33. 33. Just the entity... Monday, 21 October, 13
  34. 34. MoM, lazily defined properties Monday, 21 October, 13
  35. 35. attributes Monday, 21 October, 13
  36. 36. attributes Monday, 21 October, 13
  37. 37. relationships Monday, 21 October, 13
  38. 38. relationships Monday, 21 October, 13
  39. 39. The “other” model Monday, 21 October, 13
  40. 40. Relationships • Done • With that, you can pretty much define any kind of model and relationship you want, in code, no magic required. Monday, 21 October, 13
  41. 41. Data Loading https://twitter.com/usmanm/status/388407160159211520/photo/1 Monday, 21 October, 13
  42. 42. iOS Core Data Basics • Back to Ray • http://www.raywenderlich.com/12170/coredata-tutorial-how-to-preloadimportexisting-data-updated Monday, 21 October, 13
  43. 43. The RubyMotion way Monday, 21 October, 13
  44. 44. Read the data file (JSON) Monday, 21 October, 13
  45. 45. Add data to store Monday, 21 October, 13
  46. 46. add_bank Monday, 21 October, 13
  47. 47. That was easy Monday, 21 October, 13
  48. 48. Or was it? Monday, 21 October, 13
  49. 49. How many wells do I have to load again? Monday, 21 October, 13
  50. 50. 244,292 Oh. Monday, 21 October, 13
  51. 51. save for each add Monday, 21 October, 13
  52. 52. That won’t work • • • • Monday, 21 October, 13 Read in the whole file first?!? add, save, add, save • horribly inefficient for large data loads add, add, add ...., save once • yah, never mind add, add, add, save, add, add, add, save • better, but still fails on out-of-memory
  53. 53. Wait, what? Monday, 21 October, 13
  54. 54. 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. Monday, 21 October, 13
  55. 55. New, improved load Monday, 21 October, 13
  56. 56. streaming (CSV) load Monday, 21 October, 13
  57. 57. create_bank (no save) Monday, 21 October, 13
  58. 58. saves, every 100 progress every 100/1000 Monday, 21 October, 13
  59. 59. Catch the odd ones Monday, 21 October, 13
  60. 60. Ta da! 244,292 wells loaded Monday, 21 October, 13
  61. 61. And then your table view chokes on 244k items Monday, 21 October, 13
  62. 62. Optimization! http://fineartamerica.com/featured/dont-cross-the-streams-trever-miller.html Monday, 21 October, 13
  63. 63. Back to Basics (again) • Ray sure has a lot of good material, doesn’t he? • http://www.raywenderlich.com/999/coredata-tutorial-for-ios-how-to-usensfetchedresultscontroller Monday, 21 October, 13
  64. 64. Why? • NSFetchedResultsController gives us huge performance benefits • Avoids loading all the data in memory at once Monday, 21 October, 13
  65. 65. bank_store.rb Monday, 21 October, 13
  66. 66. failed_bank_table_view_controller.rb Monday, 21 October, 13
  67. 67. failed_bank_table_view_controller.rb Monday, 21 October, 13
  68. 68. number of rows Monday, 21 October, 13
  69. 69. cell for row Monday, 21 October, 13
  70. 70. configure cell Monday, 21 October, 13
  71. 71. the delegate Monday, 21 October, 13
  72. 72. Monday, 21 October, 13
  73. 73. DEMO! Monday, 21 October, 13
  74. 74. That’s All, Folks! • • Questions? Comments? • • • • @wndxlori Monday, 21 October, 13 https://github.com/wndxlori/ WNDXRubyMotion/tree/master/FailedBankCD lori@wndx.com http://www.linkedin.com/in/loriolson http://www.wndx.com
  75. 75. Core Data in Motion • It’s an ebook! • Early pre-release • http://coredatainmotion.com Monday, 21 October, 13

×