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

No Realm, No Life

No Realm, No Life

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

yohei sugigami

August 26, 2015
Tweet

More Decks by yohei sugigami

Other Decks in Technology

Transcript

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

    㱺࣮੷͕͋ΓɺυΩϡϝϯτ΋ἧ͍ͬͯΔ ɹ࢖͍΍͍͢ɺύϑΥʔϚϯε͕ྑ͍ɺαϙʔτ͕ް͍
  2. 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Λ࡞੒
  3. 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Ͱදݱ
  4. 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Λઃఆ
  5. 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Λ࡞੒
  6. 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ͷ($%Λϥοϓ͢ΔϥΠϒϥϦ
  7. 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ͱ࿈ಈͯࣗ͠ಈԽ