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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

  7. Why?
    Learning RubyMotion

    View full-size slide

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

    View full-size slide

  9. What?
    Wells In My Back Yard

    View full-size slide

  10. The Challenges

    View full-size slide

  11. LOTS of data

    View full-size slide

  12. In a List (table)

    View full-size slide

  13. Filtering by Location

    View full-size slide

  14. How hard could it
    be?

    View full-size slide

  15. Oh, not that much data

    View full-size slide

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

    View full-size slide

  17. 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 full-size slide

  18. 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 full-size slide

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

    View full-size slide

  20. It’s not as scary as it looks

    View full-size slide

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

    View full-size slide

  22. Models in code

    View full-size slide

  23. 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 full-size slide

  24. location.rb (model)

    View full-size slide

  25. location.rb (entity)

    View full-size slide

  26. location.rb (properties)

    View full-size slide

  27. location_store.rb (store)

    View full-size slide

  28. location_store.rb (MoM)

    View full-size slide

  29. location_store.rb (psc)

    View full-size slide

  30. location_store.rb (MoC)

    View full-size slide

  31. That was easy

    View full-size slide

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

    View full-size slide

  33. Chicken & Egg problem

    View full-size slide

  34. Relationships in code

    View full-size slide

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

    View full-size slide

  36. Just the entity...

    View full-size slide

  37. MoC, lazily defined properties

    View full-size slide

  38. relationships

    View full-size slide

  39. relationships

    View full-size slide

  40. The “other” model

    View full-size slide

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

    View full-size slide

  42. Data Loading

    View full-size slide

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

    View full-size slide

  44. The RubyMotion way

    View full-size slide

  45. Read the data file (JSON)

    View full-size slide

  46. Add data to store

    View full-size slide

  47. That was easy

    View full-size slide

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

    View full-size slide

  49. Houston, we have a
    problem

    View full-size slide

  50. Read in the WHOLE file?

    View full-size slide

  51. save for every add?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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.

    View full-size slide

  55. New, improved load

    View full-size slide

  56. streaming (CSV) load

    View full-size slide

  57. create_bank (no save)

    View full-size slide

  58. saves, every 100
    progress every 100/1000

    View full-size slide

  59. Catch the odd ones

    View full-size slide

  60. Ta da!
    244,292 wells loaded

    View full-size slide

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

    View full-size slide

  62. Optimization

    View full-size slide

  63. 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 full-size slide

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

    View full-size slide

  65. bank_store.rb

    View full-size slide

  66. failed_bank_table_view_controller.rb

    View full-size slide

  67. failed_bank_table_view_controller.rb

    View full-size slide

  68. number of rows

    View full-size slide

  69. cell for row

    View full-size slide

  70. configure cell

    View full-size slide

  71. the delegate

    View full-size slide

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

    View full-size slide

  73. Please use Core Data!

    View full-size slide

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

    View full-size slide

  75. 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 full-size slide