Pro Yearly is on sale from $80 to $50! »

No Realm, No Life

No Realm, No Life

Realm meetup #6 で発表したSync iOSでのRealmの活用についてです

Acbf3391de0494432a92221ffe89f34e?s=128

yohei sugigami

August 26, 2015
Tweet

Transcript

  1. No Realm, No Life yohei SUGIGAMI Realm 08/25 Realm Meetup

    #6
  2. None
  3. iOS DBวྺ ɾ'.%# ɾ$PSF%BUB ɾ.BHJDBM3FDPSE ɾ:BQ%BUBCBTF ɾ0CKFDUJWF-FWFM%# ɾ$PVDICBTF-JUF ɾ3FBMN 3FBMN

    㱺࣮੷͕͋ΓɺυΩϡϝϯτ΋ἧ͍ͬͯΔ ɹ࢖͍΍͍͢ɺύϑΥʔϚϯε͕ྑ͍ɺαϙʔτ͕ް͍
  4. ઃܭ͕Ϟμϯ

  5. ଎͞͸ਖ਼ٛ

  6. υΩϡϝϯτ͕ἧ͍ͬͯΔ

  7. αϙʔτ͕ް͍

  8. How using Realm

  9. Sync iOS ϏδωείϛϡχέʔγϣϯಛԽϝοηʔδϯάαʔϏε Powered by RealmCocoa

  10. For our UX requirements Ϣʔβʹ௨৴தͷ଴ͪΛײͤ͡͞ͳ͍Α͏ʹ 㱺ઌಡΈऔಘɺ௨ৗऔಘͷΩϟογϡͷ׆༻ Realm Ωϟγϡ

  11. For our UX requirements ೖྗதɺૹ৴த ࣦഊʣͳͲͷ్தঢ়ଶͷ෮ݩ 㱺ঢ়ଶͷӬଓԽͷ׆༻ Realm ӬଓԽ

  12. ̎ Realm Databases 3FBMN͸ҟͳΔϑΝΠϧύεͰ%#Λ ෳ਺ಉ࣌ʹѻ͏͜ͱ͕Ͱ͖Δ Realm ӬଓԽ༻ Ωϟγϡ༻ 㱺εΩʔϚఆٛ͸શ%#ڞ௨ʹͳΔͷͰ ݸผʹϚΠάϨʔγϣϯΛߦ͑ͳ͔ͬͨͨΊࠓճ͸ݟૹΓ

  13. $BSUpMF HJUIVCSFBMNSFBMNDPDPB ɹɹ How install realm-cocoa $BSUIBHFܦ༝ͰΠϯετʔϧ $PDPB1PETͩͱ4XJGUϥΠϒϥϦͷίϯύΠϧ͕௕͍ͨΊ
 4XJGUܥϥΠϒϥϦ͸ੵۃతʹ$BSUIBHFͰΠϯετʔϧ 3FBMN4XJGUΛར༻

    ݄̔̎̏೔࣌఺ͷ࠷৽͸W
  14. Realm Objects (SPVQ 6TFS 6TFS .FTTBHF 6TFS *NBHF 'JMF <.FNCFSTIJQ>

    -JOL <.FOUJPO> 5PUBMNPEFMT
  15. Introduce Realm ։ൃॳظ͔Β3FBMNΛಋೖͨ͠Θ͚Ͱ͸ͳ͘ ։ൃͷ్த͔ΒಋೖʢΩϟγϡɺӬଓԽػߏͷͳ͍ঢ়ଶʣ 1VSF$MBTTΛ3FBMN0CKFDUܧঝ ϓϩύςΟΛ3FBMNܗࣜʹ ؆୯ʹಋೖͰ͖·ͨ͠

  16. RealmObject final class Message: Object { dynamic var user =

    User() dynamic var image = MessageImage() dynamic var file = MessageFile() dynamic var link = MessageLink() dynamic var channelId = "" dynamic var content = "" var avatarImageURL: NSURL { return NSURL(string: user.avatarImageURL)! } class func fromJSONInner(json: JSON) -> Message { let message = Message() message.messageId = json["message_id"].stringValue message.content = json["content"].stringValue if json["image"].null == nil { message.image = MessageImage.fromJSON(json["image"]) } if json["file"].null == nil { message.file = MessageFile.fromJSON(json["file"]) } /463-͸1SJNJUJWF 4USJOH Ͱอଘͯ͠$PNQVUFE1SPQFSUZͰදݱ +40/͔Β0CKFDUΛ࡞੒
  17. RealmObject enum MobileNotificationPreference: String { case AllActivities = "all_activities" case

    OnlyMentions = "only_mentions" case Nothing = "nothing" } final class Group: Object { dynamic var mobileNotificationPreferenceRaw: String = MobileNotificationPreference.OnlyMentions.rawValue var mobileNotificationPreference: MobileNotificationPreference { return MobileNotificationPreference(rawValue: mobileNotificationPreferenceRaw) } FOVN΋ͦͷ··อଘͰ͖ͳ͍ͷͰ3BXͰอଘͯ͠ $PNQVUFE1SPQFSUZͰදݱ
  18. RealmObject final class Message: Object { …
 override class func

    primaryKey() -> String { return "messageId" } override class func indexedProperties() -> [String] { return ["groupId"] } override class func ignoredProperties() -> [String] { return ["groupName"] } QSJNBSZ,FZ JOEFYFE1SPQFSUJFT JHOPSFE1SPQFSUJFTΛઃఆ
  19. RealmObject final class Message: Object { …
 class func find(pkey:

    String) -> Message? { return Realm().objectForPrimaryKey(Message.self, key: pkey) } class func findByChannel(groupId: String) -> Results<Message> { let predicate = NSPredicate(format: "groupId = %@", groupId) return Realm().objects(Message).filter(predicate) .sorted("createdAtRaw", ascending: false) } 3FBMNͷPCKFDUܥϝιουΛϥοϓͯ͠4VHBS4ZOUBYΛ࡞੒
  20. Background // Queue let RealmBackgroundSaveQueue = dispatch_queue_create ("com.wantedly.sync.realm", DISPATCH_QUEUE_SERIAL) class

    func saveJSON(json: JSON, closure: (group: Group) -> Void) { Async.customQueue(RealmBackgroundSaveQueue) { Realm().write { let group = Group.fromJSON(json) Realm().add(group, update: true) } Async.main { let group = Group.fromJSON(json) closure(group: group) } } } 3FBMNߋ৽ઐ༻ͷ4FSJBM2VFVFΛ࡞੒ͯ͠ஞ࣍తʹߋ৽ #BDLHSPVOEͰ࡞੒ͨ͠3FBMN0CKFDU͸.BJOͰ৮ΔͱΫϥογϡ อଘ༻ͱར༻༻Ͱ෼͚ͯΠϯελϯεΛ࡞੒ "TZOD͸4XJGUͷ($%Λϥοϓ͢ΔϥΠϒϥϦ
  21. Migration // MARK: Initialize extension AppDelegate { func initializeRealm() {

    let BuildVersion = NSBundle.mainBundle() .objectForInfoDictionaryKey("CFBundleVersion") as! String let thisVersion = UInt64(BuildVersion.toInt()!) setSchemaVersion(thisVersion, Realm.defaultPath) { migration, oldSchemaVersion in if oldSchemaVersion < thisVersion { // Nothing to do } } logger.debug("Realm Default Path [\(Realm.defaultPath)]") } } େ఍ͷ.JHSBUJPO͸ͱͬͯ΋؆୯ 4DIFNB7FSTJPOΛ্͛Δ͚ͩ 4ZODͰ͸$'#VOEMF7FSTJPOͱ࿈ಈͯࣗ͠ಈԽ

  22. ɾ։ൃ͠΍͍͢ ɾӡ༻͠΍͍͢ ɾΤϯδχΞʹ΍͍͞͠ Conclusion ɾ΋͏$PSF%BUBʹ͸໭Εͳ͍

  23. Enjoy Realm life.