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

Realm iOS Code Review

Realm iOS Code Review

0b26590a0a8b0f1da140ed5de9b68379?s=128

USAMI Kosuke

March 26, 2017
Tweet

Transcript

  1. Realm iOS Λ࢖͏ͱ͖ͷ ϋϚΓϙΠϯτ USAMI Kosuke (Fenrir Inc.)

  2. ࣗݾ঺հ » Ӊࠤݟ ެีʢ@usamik26ʣ » ϑΣϯϦϧגࣜձࣾ » iOS ΞϓϦ։ൃΤϯδχΞ »

    Swift » Xamarin.iOS » ޷͖ͳϥΠϒϥϦ͸ RxSwift
  3. Realm » ࠷ۙ 1ʙ2 ೥ͰؔΘ͍ͬͯΔ։ൃҊ݅ͷ΄ͱΜͲͰ Realm ࠾༻ » Realm Objective-C

    » Realm Swift » Realm Xamarin
  4. ͳͥ Realm ͳͷ͔ʁ

  5. ͳͥ Realm ͳͷ͔ʁ ͦͷલʹ… » ͦ΋ͦ΋ɺΞϓϦʹσʔλϕʔεʢDBʣΛ಺ଂ͢Δඞཁ͕͋Δ͔ʁ » DB ͳΜͯ໘౗ͦ͏… »

    ଞʹ΋ͬͱ؆୯ͳํ๏͕͋ΔͷͰ͸…
  6. σʔλϕʔε͸໰୊ղܾͷҰखஈ » ͜Μͳ͜ͱ͕͍ͨ͠ » αʔόʔ͔Βऔಘͨ͠σʔλΛ͠͹Β͘ஔ͍͓͖͍ͯͨ » ΦϑϥΠϯͰ΋લճऔಘͨ͠σʔλͰಈ࡞͍ͤͨ͞ » ϚελʔσʔλΛ಺ଂ͍ͨ͠ »

    σʔλϕʔε͸ͦΕΛղܾ͢Δखஈͷͻͱͭʹ͗͢ͳ͍
  7. Realm ͸ͦΕΒͷ༏Εͨղܾखஈ » ૊ΈࠐΈ͕؆୯ » ίʔσΟϯά͕؆୯ » ͔ͩΒɺΧδϡΞϧͳ༻్ʹ࢖͏ͷ͕ۤʹͳΒͳ͍

  8. ϋϚΓϙΠϯτ » Realm Λ࠾༻͢ΔϓϩδΣΫτ͕૿͑ͨ » ͕ͨͬͯ͠ɺͦΕΛίʔυϨϏϡʔ͢Δػձ͕૿͑ͨ » ϨϏϡʔͨ͠தͰɺΑ͋͘ΔϋϚΓϙΠϯτΛڍ͛ͯΈ͍ͨ » ࣮ࡍʹ͸υΩϡϝϯτಡΜͰਖ਼͘͠ѻ͑͹େৎ෉

  9. ϑΝΠϧͷஔ͖৔ॴ

  10. Realm ϑΝΠϧͷஔ͖৔ॴ » σϑΥϧτͰ͸ Documents ϑΥϧμ಺ʹ࡞ΒΕΔ » ͍Ζ͍Ζࢼ͍ͨ࣌͠ʹศར » ͨͩ͠ɺσϑΥϧτͷ··Կ΋ߟ͑ͣʹϦϦʔεͯ͠͸μϝ

  11. iCloud όοΫΞοϓ » Documents ϑΥϧμ͸ iCloud όοΫΞοϓͷର৅ » iCloud όοΫΞοϓͷඞཁ༰ྔ͕๲Ε্͕ͬͯ͠·͏

    » ؾ͔ͮͣʹϦϦʔεͯ͠͠·͏ͱҰେࣄ
  12. ରॲํ๏ » ҎԼͷͲΕ͔Ͱରॲ͢Δ » (1) ϑΝΠϧͷஔ͖৔ॴΛม͑Δ » (1-a) defaultRealm ͷઃఆΛม͑Δ

    » (1-b) σϑΥϧτҎ֎ͷ Realm Λ࢖͏ » (2) ϑΝΠϧͷஔ͖৔ॴ͸ͦͷ··ͰɺόοΫΞοϓର৅͔Βআ֎
  13. Realm.Configuration var config = Realm.Configuration() config.fileURL = ... // (1-a)

    defaultRealm ͷઃఆΛม͑Δ Realm.Configuration.defaultConfiguration = config // (1-b) σϑΥϧτҎ֎ͷ Realm Λ࢖͏ let realm = try! Realm(configuration: config)
  14. όοΫΞοϓର৅͔Βআ֎ let realmURL = Realm.Configuration.defaultConfiguration.fileURL! let realmURLs = [ //

    ஫ҙɿର৅ϑΝΠϧ͸ Realm ͷόʔδϣϯʹΑΓมΘΔ realmURL, realmURL.appendingPathExtension("lock"), realmURL.appendingPathExtension("note"), realmURL.appendingPathExtension("management") ] // (2) όοΫΞοϓର৅͔Βআ֎ʢ஫ҙɿઌʹ Realm ΛҰ౓࡞੒͓ͯ͘͜͠ͱʣ realmURLs.forEach { fileURL in fileURL.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey) }
  15. Ͳ͏͢΂͖ʁ » جຊతʹ͸ϑΝΠϧͷஔ͖৔ॴΛม͑Δͷ͕ྑ͍ » Ͳ͜ʹஔ͔͘͸ͦͷΞϓϦͷઃܭ࣍ୈ » όοΫΞοϓର৅͔Βআ֎͢Δํ๏͸ඇৗखஈ

  16. τϥϯβΫγϣϯΛ ·ͱΊΔ

  17. τϥϯβΫγϣϯ » σʔλͷॻ͖ࠐΈ͸ realm.write Λ࢖͏ » realm.write ͷͨͼʹτϥϯβΫγϣϯ͕ൃੜ » ʢ͜ͷ͋ͨΓɺRealm

    ͕σʔλϕʔεͰ͋Δ͜ͱΛ࠶ೝࣝʣ
  18. τϥϯβΫγϣϯ͸·ͱΊΔ΂͖ » ϑΝΠϧΞΫηεͷͨΊɺΦʔόʔϔου͕͋Δ » ·ͨɺRealm ϑΝΠϧ͕ංେԽ͢ΔݪҼʹ΋ͳΔ » τϥϯβΫγϣϯϩά͕ཷ·͍ͬͯͨ͘Ί » Realm

    ϑΝΠϧ༰ྔ͕͙͢େ͖͘ͳΔͳΒٙͬͯΈΔ
  19. τϥϯβΫγϣϯ͕૿͑Δύλʔϯ(1) » ϧʔϓͷதͰຖճ realm.write ΛݺΜͰ͍ͳ͍͔ʁ » Reactive ܥͰ realm.write ͷճ਺Λҙ͍ࣝͯ͠Δ͔ʁ

    » খ͞ͳ do ΍ map Ͱࡉ͔͘ realm.write ͍ͯͨ͠Γͱ͔
  20. τϥϯβΫγϣϯ͕૿͑Δύλʔϯ(2) » Realm ʹϥούʔΛඃ͍ͤͯΔ৔߹ʢଞ DB ͱͷޓ׵ੑΛߟྀͳ Ͳʣ » ࡉ͔͍มߋͷͨͼʹ realm.write

    ΛݺΜͰ͍ͳ͍͔ʁ
  21. Ͳ͏͢΂͖ʁ » realm.write ͷճ਺Λҙࣝͯ͠ݮΒ͢ » Realm ͷϥούʔ͸ͦ΋ͦ΋࡞Βͳ͍ͷ͕ϕλʔ

  22. ίϯύΫγϣϯ

  23. ϑΝΠϧංେԽͷରࡦ » ϑΝΠϧαΠζ͕େ͖͘ͳΔͷ͕ؾʹͳΔ৔߹΋͋Δ » ରࡦ » τϥϯβΫγϣϯΛ·ͱΊΔʢલड़ʣ » Realm ϑΝΠϧΛ࠷దԽ͢ΔʢίϯύΫγϣϯʣ

  24. ίϯύΫγϣϯͷํ๏ » realm.writeCopy(toFile:) Ͱߦ͑Δ » ଞͷՕॴͰ Realm Λ࢖͍ͬͯͳ͍λΠϛϯάͰߦ͏

  25. ίϯύΫγϣϯͷॲཧ autoreleasepool { let realm = try! Realm() try! realm.writeCopy(toFile:

    tempURL) try! FileManager.default.removeItem(at: realmURL) try! FileManager.default.moveItem(at: tempURL, to: realmURL) }
  26. Ͳ͏͢΂͖ʁ » ίϯύΫγϣϯ͸ඞਢͰ͸ͳ͍ » ؾʹͳΔͳΒΞϓϦىಈ࣌ͳͲ Realm Λ࢖͍ͬͯͳ͍ՕॴͰ࣮ߦ

  27. ΦϒδΣΫτͷड͚౉͠

  28. Realm ΦϒδΣΫτͷड͚౉͠ » ϝιουͷؒͷड͚౉͠ » ModelɺViewModelɺView ͷؒͷड͚౉͠ » εϨουͷؒͷड͚౉͠ »

    εϨου͕ҟͳΔ৔߹͸ͦͷ··౉͢͜ͱ͸Ͱ͖ͳ͍
  29. ड͚౉͢ํ๏ » ΦϒδΣΫτΛͦͷ··౉͢ » struct ʹม׵ͯ͠౉͢ » ϓϥΠϚϦΩʔΛ౉͢ » ThreadSafeReference

    Λ࢖͏
  30. ΦϒδΣΫτΛͦͷ··౉͢ » ಉ͡εϨουͰ͋Δ͜ͱ͕໌Β͔Ͱ͋Δ৔߹ » ͨͩɺ͋·Γ֊૚Λ·͕ͨͬͯ౉͗͢͠Δͱ෼͔Γʹ͘͘ͳΔ

  31. struct ʹม׵ͯ͠౉͢ » Realm ΦϒδΣΫτͰ͸ͳ͍΋ͷʹม׵͢Δ » ม׵ͷख͕͔͔ؒΔ » ΦϒδΣΫτΛͦͷ··ͷܗͰͳ͘৭ʑखΛՃ͍͑ͨ৔߹ʹ͸͋Γ

  32. ϓϥΠϚϦΩʔΛ౉͢ » εϨουΛ·͕ͨΔ৔߹ͷखܰͳํ๏ͷͻͱͭ // ड͚औΔଆ let realm = try! Realm()

    let person = realm.object(ofType: Person.self, forPrimaryKey: key)
  33. ThreadSafeReference Λ࢖͏ » ৽͍͠ํ๏ɺϓϥΠϚϦΩʔ͕ͳͯ͘΋ྑ͍ // ౉͢ଆ let personRef = ThreadSafeReference(to:

    person) // ड͚औΔଆ let realm = try! Realm() let person = realm.resolve(personRef)
  34. ิ଍ : Realm Πϯελϯεͷऔಘ » let realm = try! Realm()

    ͸ඞཁͳ࣌ʹͦͷ౎౓࣮ߦͯ͠ྑ͍ » ֤ϝιουͰߦ͏ » ֤ΫϩʔδϟͰߦ͏ » ίετ͕͔͔ΔॲཧͰ͸ͳ͍ » realm Λڞ༗ͯ͠࢖͓͏ͱ͢Δͱ͔͑ͬͯ໘౗
  35. Ͳ͏͢΂͖ʁ » ϓϥΠϚϦΩʔ·ͨ͸ ThreadSafeReference ͕ྑ͍ » ΦϒδΣΫτͦͷ΋ͷΛ౉͢ͷ͸ൣғΛݶఆ͍ͨ͠ » struct ʹม׵͢Δͷ͸ɺͦͷม׵ࣗମʹҙຯ͕͋Δ৔߹ͷΈ

  36. ΫΤϦΛͲ͜Ͱߦ͏͔

  37. ΫΤϦΛͲͷ֊૚Ͱߦ͏͔ » Ͳͷ֊૚ͷ੹຿͔ʁ » Model » ViewModel » View (ViewController)

  38. Model » Model ૚ʹϏδωεϩδοΫΛ࣋ͨͤΔ » ͜͜ͰΫΤϦ͕ߦΘΕΔ » ϏδωεϩδοΫΛ ViewModel ΍

    View ʹஔ͔ͳ͍
  39. ViewModel » ViewModel ૚͸ը໘͝ͱͷදࣔ༻ίϯςϯπΛ࡞Δ » ͜͜Ͱ΋ΫΤϦΛҰ෦׆༻͢Δ » Model ͕มΘͬͨ࣌ʹ Live

    Object ͷػೳར༻ » View ͸༩͑ΒΕͨίϯςϯπΛϨΠΞ΢τ͢Δ͚ͩ
  40. Ͳ͏͢΂͖ʁ » Model : ϏδωεϩδοΫΛ࣋ͭ » ViewModel : දࣔ༻ίϯςϯπΛ࡞Δ »

    View : ϨΠΞ΢τͷΈ
  41. ·ͱΊ

  42. ·ͱΊ » ϑΝΠϧͷஔ͖৔ॴ » τϥϯβΫγϣϯΛ·ͱΊΔ » ίϯύΫγϣϯ » ΦϒδΣΫτͷड͚౉͠ »

    ΫΤϦΛͲ͜Ͱߦ͏͔