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

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!

Lori M Olson

May 28, 2014
Tweet

More Decks by Lori M Olson

Other Decks in Programming

Transcript

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

    View Slide

  2. Lori Olson
    [email protected]
    • www.wndx.com
    • @wndxlori
    • Freelance developer,
    mentor, trainer
    • CANADA!

    View Slide

  3. KUDOS
    Changing the world, one Thank You at a time!
    kudosnow.com

    View Slide

  4. i Develop
    • Ruby (mostly)
    • Rails
    • iOS (RubyMotion)
    • JRuby
    • Javascript (some)
    • jQuery
    • Maps/Charts
    • Javascript Testing

    View Slide

  5. i Teach
    • Ruby on Rails for Real Developers
    (RoR4Real)
    • Rails for iOS Developers (Rails4iOS)
    • Ladies Learning Code - Ruby Introduction

    View Slide

  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

    View Slide

  7. The Task

    View Slide

  8. Why?
    Learning RubyMotion

    View Slide

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

    View Slide

  10. What?
    Wells In My Back Yard

    View Slide

  11. The Challenges

    View Slide

  12. LOTS of data

    View Slide

  13. On a Map

    View Slide

  14. In a List (table)

    View Slide

  15. Filtering by Location

    View Slide

  16. How hard could it
    be?

    View Slide

  17. Oh, not that much data

    View Slide

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

    View Slide

  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?

    View Slide

  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

    View Slide

  21. iOS Basics
    • Ray Wenderlich!
    • http://www.raywenderlich.com/934/core-
    data-tutorial-for-ios-getting-started

    View Slide

  22. It’s not as scary as it looks

    View Slide

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

    View Slide

  24. Models in code

    View Slide

  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

    View Slide

  26. location.rb (model)

    View Slide

  27. location.rb (entity)

    View Slide

  28. location.rb (properties)

    View Slide

  29. location_store.rb (store)

    View Slide

  30. location_store.rb (MoM)

    View Slide

  31. location_store.rb (psc)

    View Slide

  32. location_store.rb (MoC)

    View Slide

  33. That was easy

    View Slide

  34. Or was it?

    View Slide

  35. View Slide

  36. Overly simplistic
    • Doesn’t work with multiple models with
    relationships
    • You need a reference to an entity, to define
    a relationship fully

    View Slide

  37. Chicken & Egg problem

    View Slide

  38. Relationships in code

    View Slide

  39. Solution
    • Define your entities. First.
    • Lazily! define your entities’ properties
    • properties = (attributes and relationships)

    View Slide

  40. Just the entity...

    View Slide

  41. MoC, lazily defined properties

    View Slide

  42. attributes

    View Slide

  43. attributes

    View Slide

  44. relationships

    View Slide

  45. relationships

    View Slide

  46. properties

    View Slide

  47. The “other” model

    View Slide

  48. Relationships
    • Done
    • With that, you can pretty much define any
    kind of model and relationship you want, in
    code, no magic required.

    View Slide

  49. Data Loading

    View Slide

  50. iOS Core Data Basics
    • Back to Ray
    • http://www.raywenderlich.com/12170/core-
    data-tutorial-how-to-preloadimport-
    existing-data-updated

    View Slide

  51. The RubyMotion way

    View Slide

  52. Read the data file (JSON)

    View Slide

  53. Add data to store

    View Slide

  54. add_bank

    View Slide

  55. That was easy

    View Slide

  56. Or was it?

    View Slide

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

    View Slide

  58. 244,292
    Oh.

    View Slide

  59. Houston, we have a
    problem

    View Slide

  60. Read in the WHOLE file?

    View Slide

  61. save for every add?

    View Slide

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

    View Slide

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

    View Slide

  64. Wait, what?

    View Slide

  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.

    View Slide

  66. New, improved load

    View Slide

  67. streaming (CSV) load

    View Slide

  68. create_bank (no save)

    View Slide

  69. saves, every 100
    progress every 100/1000

    View Slide

  70. Catch the odd ones

    View Slide

  71. Ta da!
    244,292 wells loaded

    View Slide

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

    View Slide

  73. Optimization

    View Slide

  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

    View Slide

  75. Why?
    • NSFetchedResultsController gives us huge
    performance benefits
    • Avoids loading all the data in memory at
    once

    View Slide

  76. bank_store.rb

    View Slide

  77. failed_bank_table_view_controller.rb

    View Slide

  78. failed_bank_table_view_controller.rb

    View Slide

  79. number of rows

    View Slide

  80. cell for row

    View Slide

  81. configure cell

    View Slide

  82. the delegate

    View Slide

  83. View Slide

  84. In Summary
    • Create models in code
    • Create relationships between models in
    code
    • Pre-loading data
    • LOTS of data
    • Efficiently displaying data

    View Slide

  85. Please use Core Data!

    View Slide

  86. Core Data in Motion
    • It’s an ebook!
    • Almost finished!
    • http://coredatainmotion.com

    View Slide

  87. That’s All, Folks!
    • Code: 

    https://github.com/wndxlori/WNDXRubyMotion/
    tree/master/FailedBankCD
    • Questions? Comments?
    • @wndxlori
    [email protected]
    • http://www.linkedin.com/in/loriolson
    • http://www.wndx.com

    View Slide