Core Data in RubyMotion - #inspect

Core Data in RubyMotion - #inspect

Have you tried to use CoreData in RubyMotion, only to get lost in the quagmire of simplistic or confusing examples, DSL's and gems? Let's go out and shave that herd of yaks!

B36ea58289890176b6170e382b3194fe?s=128

Lori M Olson

May 28, 2014
Tweet

Transcript

  1. Core Data in Motion RubyMotion #inspect May 28-29, 2014

  2. Lori Olson • lori@wndx.com • www.wndx.com • @wndxlori • Freelance

    developer, mentor, trainer • CANADA!
  3. KUDOS Changing the world, one Thank You at a time!

    kudosnow.com
  4. i Develop • Ruby (mostly) • Rails • iOS (RubyMotion)

    • JRuby • Javascript (some) • jQuery • Maps/Charts • Javascript Testing
  5. i Teach • Ruby on Rails for Real Developers (RoR4Real)

    • Rails for iOS Developers (Rails4iOS) • Ladies Learning Code - Ruby Introduction
  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. The Task

  8. Why? Learning RubyMotion

  9. How? To rewrite a (mobile) web application as an iOS

    application
  10. What? Wells In My Back Yard

  11. The Challenges

  12. LOTS of data

  13. On a Map

  14. In a List (table)

  15. Filtering by Location

  16. How hard could it be?

  17. Oh, not that much data

  18. RubyMotion gems/DSL’s • Nitron • MotionDataWrapper • MotionModel • MotionMigrate

    • many, many others
  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. 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. iOS Basics • Ray Wenderlich! • http://www.raywenderlich.com/934/core- data-tutorial-for-ios-getting-started

  22. It’s not as scary as it looks

  23. What? • Models (entities) in code • Relationships in code

    • Loading data • Optimization
  24. Models in code

  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. location.rb (model)

  27. location.rb (entity)

  28. location.rb (properties)

  29. location_store.rb (store)

  30. location_store.rb (MoM)

  31. location_store.rb (psc)

  32. location_store.rb (MoC)

  33. That was easy

  34. Or was it?

  35. None
  36. Overly simplistic • Doesn’t work with multiple models with relationships

    • You need a reference to an entity, to define a relationship fully
  37. Chicken & Egg problem

  38. Relationships in code

  39. Solution • Define your entities. First. • Lazily! define your

    entities’ properties • properties = (attributes and relationships)
  40. Just the entity...

  41. MoC, lazily defined properties

  42. attributes

  43. attributes

  44. relationships

  45. relationships

  46. properties

  47. The “other” model

  48. Relationships • Done • With that, you can pretty much

    define any kind of model and relationship you want, in code, no magic required.
  49. Data Loading

  50. iOS Core Data Basics • Back to Ray • http://www.raywenderlich.com/12170/core-

    data-tutorial-how-to-preloadimport- existing-data-updated
  51. The RubyMotion way

  52. Read the data file (JSON)

  53. Add data to store

  54. add_bank

  55. That was easy

  56. Or was it?

  57. How many wells do I have to load again?

  58. 244,292 Oh.

  59. Houston, we have a problem

  60. Read in the WHOLE file?

  61. save for every add?

  62. That won’t work either • add, add, add ...., save

    once
  63. Batching… ? • add, add, add, save, add, add, add,

    save
  64. Wait, what?

  65. 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.
  66. New, improved load

  67. streaming (CSV) load

  68. create_bank (no save)

  69. saves, every 100 progress every 100/1000

  70. Catch the odd ones

  71. Ta da! 244,292 wells loaded

  72. And then your table view chokes on 244k items

  73. Optimization

  74. 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
  75. Why? • NSFetchedResultsController gives us huge performance benefits • Avoids

    loading all the data in memory at once
  76. bank_store.rb

  77. failed_bank_table_view_controller.rb

  78. failed_bank_table_view_controller.rb

  79. number of rows

  80. cell for row

  81. configure cell

  82. the delegate

  83. None
  84. In Summary • Create models in code • Create relationships

    between models in code • Pre-loading data • LOTS of data • Efficiently displaying data
  85. Please use Core Data!

  86. Core Data in Motion • It’s an ebook! • Almost

    finished! • http://coredatainmotion.com
  87. 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