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

Realm iOS Code Review

Realm iOS Code Review

https://realm.connpass.com/event/49669/

USAMI Kosuke

March 26, 2017
Tweet

More Decks by USAMI Kosuke

Other Decks in Programming

Transcript

  1. ରॲํ๏ » ҎԼͷͲΕ͔Ͱରॲ͢Δ » (1) ϑΝΠϧͷஔ͖৔ॴΛม͑Δ » (1-a) defaultRealm ͷઃఆΛม͑Δ

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

    defaultRealm ͷઃఆΛม͑Δ Realm.Configuration.defaultConfiguration = config // (1-b) σϑΥϧτҎ֎ͷ Realm Λ࢖͏ let realm = try! Realm(configuration: config)
  3. όοΫΞοϓର৅͔Βআ֎ 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) }
  4. ίϯύΫγϣϯͷॲཧ autoreleasepool { let realm = try! Realm() try! realm.writeCopy(toFile:

    tempURL) try! FileManager.default.removeItem(at: realmURL) try! FileManager.default.moveItem(at: tempURL, to: realmURL) }
  5. ThreadSafeReference Λ࢖͏ » ৽͍͠ํ๏ɺϓϥΠϚϦΩʔ͕ͳͯ͘΋ྑ͍ // ౉͢ଆ let personRef = ThreadSafeReference(to:

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

    ͸ඞཁͳ࣌ʹͦͷ౎౓࣮ߦͯ͠ྑ͍ » ֤ϝιουͰߦ͏ » ֤ΫϩʔδϟͰߦ͏ » ίετ͕͔͔ΔॲཧͰ͸ͳ͍ » realm Λڞ༗ͯ͠࢖͓͏ͱ͢Δͱ͔͑ͬͯ໘౗