core data in motion
Post on 18-Oct-2014
1.526 views
DESCRIPTION
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.TRANSCRIPT
![Page 1: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/1.jpg)
Core Data in MotionNSScotland 2013 - Oct 19/20
Monday, 21 October, 13
![Page 2: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/2.jpg)
Who?
• Lori M Olson
• @wndxlori
• on Github too
• and ADN
Monday, 21 October, 13
![Page 3: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/3.jpg)
i Develop• Ruby (mostly)
• Rails
• iOS (RubyMotion)
• JRuby
• Javascript (some)
• jQuery
• Maps/Charts
• Javascript Testing
Monday, 21 October, 13
![Page 4: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/4.jpg)
i Teach
• Ruby on Rails for Real Developers (RoR4Real)
• Rails for iOS Developers (Rails4iOS)
• Ladies Learning Code - Ruby Introduction
Monday, 21 October, 13
![Page 5: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/5.jpg)
i Speak• 2013
• RubyConf AU, NSScotland
• 2012
• Confoo, RailsConf, Aloha RubyConf
• 2011
• jQueryConf, Madison Ruby
• 2010
• Conferencia Rails
• 2009
• RailsConf
Monday, 21 October, 13
![Page 6: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/6.jpg)
Warning:LOTS of code
Monday, 21 October, 13
![Page 7: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/7.jpg)
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
![Page 8: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/8.jpg)
The Challenges
• LOTS of data
• On a Map
• In a list (table view)
• Filtering by location
Monday, 21 October, 13
![Page 9: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/9.jpg)
Oh, not much dataMonday, 21 October, 13
![Page 10: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/10.jpg)
RubyMotion gems/DSL’s
• Nitron
• MotionDataWrapper
• MotionModel
• MotionMigrate
Monday, 21 October, 13
![Page 11: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/11.jpg)
iOS Basics
• Ray Wenderlich!
• http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started
Monday, 21 October, 13
![Page 12: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/12.jpg)
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
![Page 13: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/13.jpg)
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
![Page 14: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/14.jpg)
It’s not as scary as it looksMonday, 21 October, 13
![Page 15: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/15.jpg)
What?
• Models (entities) in code
• Relationships in code
• Loading data
• Optimization
Monday, 21 October, 13
![Page 16: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/16.jpg)
Models in code
Monday, 21 October, 13
![Page 17: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/17.jpg)
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
![Page 18: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/18.jpg)
location.rb (model)Monday, 21 October, 13
![Page 19: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/19.jpg)
location.rb (entity)Monday, 21 October, 13
![Page 20: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/20.jpg)
location.rb (properties)Monday, 21 October, 13
![Page 21: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/21.jpg)
location_store.rb (store)Monday, 21 October, 13
![Page 22: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/22.jpg)
location_store.rb (MoM)Monday, 21 October, 13
![Page 23: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/23.jpg)
location_store.rb (psc)Monday, 21 October, 13
![Page 24: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/24.jpg)
location_store.rb (MoC)Monday, 21 October, 13
![Page 25: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/25.jpg)
That was easy
Monday, 21 October, 13
![Page 26: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/26.jpg)
Or was it?
Monday, 21 October, 13
![Page 27: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/27.jpg)
Monday, 21 October, 13
![Page 28: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/28.jpg)
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
![Page 29: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/29.jpg)
Chicken & Egg problemMonday, 21 October, 13
![Page 30: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/30.jpg)
Relationships in code
http://www.slowfamilyonline.com/2011/12/slow-news-day-hooray-for-low-tech-toys/tinkertoys/
Monday, 21 October, 13
![Page 31: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/31.jpg)
Solution
• Define your entities. First.
• Lazily! define your entities’ properties
• properties = (attributes and relationships)
Monday, 21 October, 13
![Page 32: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/32.jpg)
Another example
• Ray Wenderlich’s Failed Bank example has 2 models
• So let’s just start there
Monday, 21 October, 13
![Page 33: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/33.jpg)
Just the entity...Monday, 21 October, 13
![Page 34: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/34.jpg)
MoM, lazily defined properties
Monday, 21 October, 13
![Page 35: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/35.jpg)
attributesMonday, 21 October, 13
![Page 36: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/36.jpg)
attributesMonday, 21 October, 13
![Page 37: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/37.jpg)
relationshipsMonday, 21 October, 13
![Page 38: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/38.jpg)
relationshipsMonday, 21 October, 13
![Page 39: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/39.jpg)
The “other” modelMonday, 21 October, 13
![Page 40: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/40.jpg)
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
![Page 41: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/41.jpg)
Data Loading
https://twitter.com/usmanm/status/388407160159211520/photo/1
Monday, 21 October, 13
![Page 42: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/42.jpg)
iOS Core Data Basics
• Back to Ray
• http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated
Monday, 21 October, 13
![Page 43: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/43.jpg)
The RubyMotion wayMonday, 21 October, 13
![Page 44: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/44.jpg)
Read the data file (JSON)Monday, 21 October, 13
![Page 45: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/45.jpg)
Add data to storeMonday, 21 October, 13
![Page 46: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/46.jpg)
add_bankMonday, 21 October, 13
![Page 47: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/47.jpg)
That was easy
Monday, 21 October, 13
![Page 48: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/48.jpg)
Or was it?
Monday, 21 October, 13
![Page 49: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/49.jpg)
How many wells do I have to load again?
Monday, 21 October, 13
![Page 50: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/50.jpg)
244,292Oh.
Monday, 21 October, 13
![Page 51: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/51.jpg)
save for each addMonday, 21 October, 13
![Page 52: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/52.jpg)
That won’t work
• 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
Monday, 21 October, 13
![Page 53: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/53.jpg)
Wait, what?
Monday, 21 October, 13
![Page 54: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/54.jpg)
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
![Page 55: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/55.jpg)
New, improved loadMonday, 21 October, 13
![Page 56: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/56.jpg)
streaming (CSV) loadMonday, 21 October, 13
![Page 57: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/57.jpg)
create_bank (no save)Monday, 21 October, 13
![Page 58: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/58.jpg)
saves, every 100progress every 100/1000
Monday, 21 October, 13
![Page 59: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/59.jpg)
Catch the odd onesMonday, 21 October, 13
![Page 60: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/60.jpg)
Ta da! 244,292 wells loaded
Monday, 21 October, 13
![Page 61: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/61.jpg)
And then your table view chokes on 244k
items
Monday, 21 October, 13
![Page 62: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/62.jpg)
Optimization!
http://fineartamerica.com/featured/dont-cross-the-streams-trever-miller.html
Monday, 21 October, 13
![Page 63: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/63.jpg)
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
Monday, 21 October, 13
![Page 64: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/64.jpg)
Why?
• NSFetchedResultsController gives us huge performance benefits
• Avoids loading all the data in memory at once
Monday, 21 October, 13
![Page 65: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/65.jpg)
bank_store.rbMonday, 21 October, 13
![Page 66: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/66.jpg)
failed_bank_table_view_controller.rb
Monday, 21 October, 13
![Page 67: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/67.jpg)
failed_bank_table_view_controller.rb
Monday, 21 October, 13
![Page 68: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/68.jpg)
number of rowsMonday, 21 October, 13
![Page 69: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/69.jpg)
cell for rowMonday, 21 October, 13
![Page 70: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/70.jpg)
configure cellMonday, 21 October, 13
![Page 71: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/71.jpg)
the delegateMonday, 21 October, 13
![Page 72: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/72.jpg)
Monday, 21 October, 13
![Page 73: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/73.jpg)
DEMO!
Monday, 21 October, 13
![Page 74: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/74.jpg)
That’s All, Folks!
• Questions? Comments?
• https://github.com/wndxlori/WNDXRubyMotion/tree/master/FailedBankCD
• @wndxlori
• http://www.linkedin.com/in/loriolson
• http://www.wndx.com
Monday, 21 October, 13
![Page 75: Core Data in Motion](https://reader034.vdocument.in/reader034/viewer/2022042623/544311acb1af9f310a8b4873/html5/thumbnails/75.jpg)
Core Data in Motion
• It’s an ebook!
• Early pre-release
• http://coredatainmotion.com
Monday, 21 October, 13