Slide 1

Slide 1 text

Core Data in Motion NSScotland 2013 - Oct 19/20 Monday, 21 October, 13

Slide 2

Slide 2 text

Who? • Lori M Olson • @wndxlori • on Github too • and ADN Monday, 21 October, 13

Slide 3

Slide 3 text

i Develop • Ruby (mostly) • Rails • iOS (RubyMotion) • JRuby • Javascript (some) • jQuery • Maps/Charts • Javascript Testing Monday, 21 October, 13

Slide 4

Slide 4 text

i Teach • Ruby on Rails for Real Developers (RoR4Real) • Rails for iOS Developers (Rails4iOS) • Ladies Learning Code - Ruby Introduction Monday, 21 October, 13

Slide 5

Slide 5 text

i Speak • 2013 • RubyConf AU, NSScotland • 2012 • Confoo, RailsConf, Aloha RubyConf • 2011 • jQueryConf, Madison Ruby • 2010 • Conferencia Rails • 2009 • RailsConf Monday, 21 October, 13

Slide 6

Slide 6 text

Warning: LOTS of code Monday, 21 October, 13

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

The Challenges • LOTS of data • On a Map • In a list (table view) • Filtering by location Monday, 21 October, 13

Slide 9

Slide 9 text

Oh, not much data Monday, 21 October, 13

Slide 10

Slide 10 text

RubyMotion gems/DSL’s • Nitron • MotionDataWrapper • MotionModel • MotionMigrate Monday, 21 October, 13

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

It’s not as scary as it looks Monday, 21 October, 13

Slide 15

Slide 15 text

What? • Models (entities) in code • Relationships in code • Loading data • Optimization Monday, 21 October, 13

Slide 16

Slide 16 text

Models in code Monday, 21 October, 13

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

location.rb (model) Monday, 21 October, 13

Slide 19

Slide 19 text

location.rb (entity) Monday, 21 October, 13

Slide 20

Slide 20 text

location.rb (properties) Monday, 21 October, 13

Slide 21

Slide 21 text

location_store.rb (store) Monday, 21 October, 13

Slide 22

Slide 22 text

location_store.rb (MoM) Monday, 21 October, 13

Slide 23

Slide 23 text

location_store.rb (psc) Monday, 21 October, 13

Slide 24

Slide 24 text

location_store.rb (MoC) Monday, 21 October, 13

Slide 25

Slide 25 text

That was easy Monday, 21 October, 13

Slide 26

Slide 26 text

Or was it? Monday, 21 October, 13

Slide 27

Slide 27 text

Monday, 21 October, 13

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Chicken & Egg problem Monday, 21 October, 13

Slide 30

Slide 30 text

Relationships in code http://www.slowfamilyonline.com/2011/12/slow-news-day-hooray-for-low-tech-toys/tinkertoys/ Monday, 21 October, 13

Slide 31

Slide 31 text

Solution • Define your entities. First. • Lazily! define your entities’ properties • properties = (attributes and relationships) Monday, 21 October, 13

Slide 32

Slide 32 text

Another example • Ray Wenderlich’s Failed Bank example has 2 models • So let’s just start there Monday, 21 October, 13

Slide 33

Slide 33 text

Just the entity... Monday, 21 October, 13

Slide 34

Slide 34 text

MoM, lazily defined properties Monday, 21 October, 13

Slide 35

Slide 35 text

attributes Monday, 21 October, 13

Slide 36

Slide 36 text

attributes Monday, 21 October, 13

Slide 37

Slide 37 text

relationships Monday, 21 October, 13

Slide 38

Slide 38 text

relationships Monday, 21 October, 13

Slide 39

Slide 39 text

The “other” model Monday, 21 October, 13

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Data Loading https://twitter.com/usmanm/status/388407160159211520/photo/1 Monday, 21 October, 13

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

The RubyMotion way Monday, 21 October, 13

Slide 44

Slide 44 text

Read the data file (JSON) Monday, 21 October, 13

Slide 45

Slide 45 text

Add data to store Monday, 21 October, 13

Slide 46

Slide 46 text

add_bank Monday, 21 October, 13

Slide 47

Slide 47 text

That was easy Monday, 21 October, 13

Slide 48

Slide 48 text

Or was it? Monday, 21 October, 13

Slide 49

Slide 49 text

How many wells do I have to load again? Monday, 21 October, 13

Slide 50

Slide 50 text

244,292 Oh. Monday, 21 October, 13

Slide 51

Slide 51 text

save for each add Monday, 21 October, 13

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

Wait, what? Monday, 21 October, 13

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

New, improved load Monday, 21 October, 13

Slide 56

Slide 56 text

streaming (CSV) load Monday, 21 October, 13

Slide 57

Slide 57 text

create_bank (no save) Monday, 21 October, 13

Slide 58

Slide 58 text

saves, every 100 progress every 100/1000 Monday, 21 October, 13

Slide 59

Slide 59 text

Catch the odd ones Monday, 21 October, 13

Slide 60

Slide 60 text

Ta da! 244,292 wells loaded Monday, 21 October, 13

Slide 61

Slide 61 text

And then your table view chokes on 244k items Monday, 21 October, 13

Slide 62

Slide 62 text

Optimization! http://fineartamerica.com/featured/dont-cross-the-streams-trever-miller.html Monday, 21 October, 13

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

Why? • NSFetchedResultsController gives us huge performance benefits • Avoids loading all the data in memory at once Monday, 21 October, 13

Slide 65

Slide 65 text

bank_store.rb Monday, 21 October, 13

Slide 66

Slide 66 text

failed_bank_table_view_controller.rb Monday, 21 October, 13

Slide 67

Slide 67 text

failed_bank_table_view_controller.rb Monday, 21 October, 13

Slide 68

Slide 68 text

number of rows Monday, 21 October, 13

Slide 69

Slide 69 text

cell for row Monday, 21 October, 13

Slide 70

Slide 70 text

configure cell Monday, 21 October, 13

Slide 71

Slide 71 text

the delegate Monday, 21 October, 13

Slide 72

Slide 72 text

Monday, 21 October, 13

Slide 73

Slide 73 text

DEMO! Monday, 21 October, 13

Slide 74

Slide 74 text

That’s All, Folks! • Questions? Comments? • https://github.com/wndxlori/ WNDXRubyMotion/tree/master/FailedBankCD • @wndxlori • [email protected] • http://www.linkedin.com/in/loriolson • http://www.wndx.com Monday, 21 October, 13

Slide 75

Slide 75 text

Core Data in Motion • It’s an ebook! • Early pre-release • http://coredatainmotion.com Monday, 21 October, 13