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

はじめてのCDQ

 はじめてのCDQ

RubyMotion Kaigi 2014 (http://connpass.com/event/8254/) で発表した資料です。

Ea4f647d2ebc49e087fb84739194b19c?s=128

Katsuyoshi Ito

September 17, 2014
Tweet

Transcript

  1. ͸͡Ίͯͷ$%2 3VCZ.PUJPO,BJHJ ,BUTVZPTIJ*UP!HVUTLVO 

  2. ࣗݾ঺հ w ҏ౻উྑ ,BUTVZPTIJ*UP  w ༗ݶձࣾҏ౻ιϑτσβΠϯ w ळాݝେઋࢢ w

    "LJUBN8FCؔ৺͋Δਓͱͷަྲྀձ w "LJUB3VCZ.FFUJOH3VCZ ΋͘΋͘ ษڧձ
  3. େۂͷՖՐ ࣸਅఏڙɿେۂ঎޻ձٞॴ

  4. ୅ද࡞ w J3VCZ,BJHJ   b b ࠓ೥͸͋Γ·ͤΜɹPS[ 3VCZ.PUJPO

  5. ୅ද࡞ 3VCZ.PUJPO w $VTUPN## ͖͘·ͳͿ ϑΥτखཪ݋ JS#PBSE&OUFSQSJTF

  6. DER

  7. DERͱ͸ʁ w $PSF%BUB2VFSZ w *OpOJUF3FE w $PSF%BUBͷϥούʔ w ͪΐͬͱ3BJMTΆ͘ w

    SVCZYDENซ༻͢ΔͱϞσϧ؅ཧ͕ςΩετͰ w J$MPVEʹ΋ରԠ
  8. ४උ HFNbDER` HFNbSVCZYDEN` CVOEMF DERJOJU (FNpMF ίϚϯυ࣮ߦ

  9. ४උ UBTLCVJMETJNVMBUPSTDIFNBCVJME 3BLFpMF ੜ੒ ௥Ճ

  10. TDIFNBఆٛ schema "0001" do entity "Movie" do string :title end

    end 4DIFNBT@JOJUJBMSC
  11. SVCZYDEN SBLFTDIFNBCVJME

  12. SVCZYDEN w $VTUPN.BOBHFE0CKFDU$MBTTFલఏ DERDSFBUFNPEFMNPWJF ίϚϯυ࣮ߦ

  13. $VTUPN.BOBHFE0CKFDU $MBTT class Movie < CDQManagedObject ! end

  14. *OJUJBMJ[F include CDQ def initilize_cdq # σϑΥϧτͰΑ͚Ε͹͠ͳͯ͘΋OK cdq.setup end

  15. *OJUJBMJ[F w .PEFM'JMF w BQQOBNFͰࢦఆ͞Ε໊ͨশ lYDEBUBNPEFMEz w 4UPSF'JMF w %PDNFOUʹ഑ஔ

    w BQQOBNFͰࢦఆ͞Ε໊ͨশ lTRMJUFz
  16. *OJUJBMJ[F w ΧελϜઃఆ w SFTPVSDFTDERZNM database_dir: NSApplicationSupportDirectory w .PEFM໊΍4UPSF'JMF໊ͳͲ΋

  17. $SFBUJPO include CDQ def create_objects Movie.create title:"Star Wars: A New

    Hope" Movie.create title:"Star Wars: The Empire Strikes Back " Movie.create title:"Star Wars: Return of the Jedi" cdq.save # ͜͜ͰॳΊͯอଘ͞ΕΔ end
  18. 'FUDI def fetch_objects Movie.all.to_a Movie.to_a Movie.first # last͸͋Γ·ͤΜ Movie[0] Movie.each{|m|

    p m} Movie.count end
  19. 2VFSZ def fetch_objects Movie .where(:title) .eq("Star Wars: A New Hope”)

    .first ! Movie .where(:title) .contains("Star Wars”)[0] ! Movie .where(:title) .contains("Star Wars”) .and .contains(“Hope") .first end
  20. 4DPQF class Movie < CDQManagedObject scope :starwars, where(:title).contains("Star Wars:") end

    def query_with_scope Movie.starwars.first end
  21. 0SEFSJOHT def query_with_orderings ! Movie.all .sort_by(:title) .map(&:title) ! Movie.all .sort_by(:title,

    order: :descending, case_insensitive: true) .map(&:title) ! end ͸·ΓͲ͜Ζ TPSU@CZ͚ͩͩͱιʔτ͠ͳ͍
  22. 2VFSZ w IUUQTHJUIVCDPNJOpOJUFSFEDERXJLJ$%2$IFBU4IFFU

  23. %FMFUJPO def delete_objects m = Movie.first m.destroy Movie.destroy_all cdq.save #

    ͜͜ͰॳΊͯอଘ͞ΕΔ end
  24. NJHSBUJPO schema "0002" do entity "Movie" do string :title has_many

    :characters, deletionRule: "Cascade" end entity "Character" do string :name belongs_to :movie end end 4DIFNB@BEE@DIBSBDUFSSC
  25. NJHSBUJPO ίϚϯυ࣮ߦ SBLFTDIFNBCVJME DERDSFBUFNPEFMDIBSBDUFS class Character < CDQManagedObject ! end

  26. 3FMBUJPOTIJQ $SFBUJPO def relation_create_objects m = Movie.new_hope.first m.characters.create name:"LUKE SKYWALKER"

    m.characters.create name:"OBI-WAN KENOBI" m.characters.create name:"DARTH VADER" cdq.save end
  27. 3FMBUJPOTIJQ 2VFSZ def relation_query_objects m = Movie.new_hope.first m.characters.where(:name).contains("LUKE").first end

  28. 3FMBUJPOTIJQ %FMFUJPO def relation_delete_objects m = Movie.new_hope.first m.characters.where(:name).contains("OBI-WAN").first.destroy #m.characters.where(:name).contains(“DARTH").destroy_all #

    NG m.characters.where(:name).contains("DARTH").each{|c| c.destroy} cdq.save end
  29. /4.BOBHFE0CKFDU$POUFYU w DERDPOUFYUTDVSSFOU def rollback_objects m = Movie.new_hope.first m.characters.create name:

    "Mr. Spock" context = cdq.contexts.current context.rollback end
  30. ελοΫߏ੒ IUUQXXXDPDPBOFUJDTDPNNVMUJDPOUFYUDPSFEBUB

  31. .VMUJ5ISFBEJOH def background_insert m = Movie.new_hope.first parent = cdq.contexts.current Dispatch::Queue.concurrent.async

    do cdq.contexts.push(parent) cdq.contexts.new(NSPrivateQueueConcurrencyType) do context = cdq.contexts.current m1 = context.objectWithID(m.objectID) m1.characters.create name: "C-3PO" m1.characters.create name: "R2-D2" cdq.save Dispatch::Queue.main.sync { p Movie.new_hope.first.characters.map(&:name) } end end end
  32. .VMUJ5ISFBEJOH m = Movie.new_hope.first parent = cdq.contexts.current # 1 Dispatch::Queue.concurrent.async

    do cdq.contexts.push(parent) # 2 cdq.contexts.new(NSPrivateQueueConcurrencyType) do # 3 context = cdq.contexts.current # 4 m1 = context.objectWithID(m.objectID) QBSFOUDPOUFYU OFXDPOUFYU QBSFOUDPOUFYU .BJOUISFBE 5ISFBE DERDPOUFYUTDVSSFOU    DERDPOUFYUTDVSSFOU 
  33. .VMUJ5ISFBEJOH w 5ISFBEؒΛ·͙ͨ৔߹͸PCKFDU*%Λ࢖͏ m = Movie.new_hope.first parent = cdq.contexts.current Dispatch::Queue.concurrent.async

    do cdq.contexts.push(parent) cdq.contexts.new(NSPrivateQueueConcurrencyType) do context = cdq.contexts.current m1 = context.objectWithID(m.objectID) # 5
  34. J$MPVE

  35. *OJUJBMJ[BUJPO w 3BLFpMFͰ&OUJUMFNFOUTͷઃఆඞཁ def initilize_cdq_with_icloud cdq.stores.new(iCloud:true) cdq.setup end

  36. ಉظ w J$MPVE4UPSF४උ w ͕͔࣌ؒΔ w ४උ׬ྃ w $%2$%24UPSF.BOBHFS4503&@%*%@*/*5 *"-*;&@/05*'*$"5*0/

  37. ·ͱΊ w ͦͦ͜͜࢖͑·͢ w $PSF%BUBΛ͋·Γҙࣝ͠ͳͯ͘΋͍͍ w (SPVQɺ)BWJOH͸ະରԠ w 7BMJEBUJPO͸ࣗલͰ 3PBENBQʹ͸͋Δ͚Ͳ

     w .JHSBUJPOSPMMCBDL͏·͍͔͘ͳ͍Έ͍ͨ w J$MPVEJ5VOFT$POOFDUʹ·ͩ4VCNJUͯ͠ͳ͍ w ଍Γͳ͍ػೳ1VMMSFRVFTUͩ͠·͠ΐ͏