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

AWA with Realm

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

AWA with Realm

Avatar for Yuji Hato

Yuji Hato

June 25, 2015
Tweet

More Decks by Yuji Hato

Other Decks in Technology

Transcript

  1. ೾ށ ༐ೋ [Yuji Hato] INTRODUCTION ࣗݾ঺հ System Engineerͱͯ͠SIerΛܦͯɺ2011೥αΠ όʔΤʔδΣϯτʹೖࣾɻ ෳ਺ͷAmebaαʔϏεʹͯɺαʔόαΠυ/

    Android/iOSΛ୲౰͠ɺݱࡏ͸AWAͷiOSΤϯδ χΞɻ Engineer 5XJUUFSIUUQTUXJUUFSDPNEFLBUPUPSP (JU)VCIUUQTHJUIVCDPNEFLBUPUPSP
  2. Models @interface EntityHoge : RLMObject @property NSString *hogeId; @property NSString

    *name; @property NSInteger updatedAt; @property NSInteger storedAt; // Finders + (EntityHoge *) findById: (NSString *) hogeId; @end @implementation EntityHoge + (NSString *)primaryKey { return @"hogeId"; } + (EntityHoge *) findById:(NSString *)hogeId { RLMRealm *realm = [RealmManager realm]; EntityHoge *hoge = [self objectInRealm:realm forPrimaryKey:hogeId]; return hoge; } @end ModelΫϥεʹFinderͳͲ΋ఆ͓ٛͯ͘͠ͱศརɻ
  3. Service @implementation HogeService - (EntityHoge *)read: (NSString *)hogeId { [[RealmManager

    realm] refresh]; return [EntityHoge findById:hogeId]; } - (BFTask *)fetch:(NSString *)hogeId withModifiedSince:(NSInteger)modifiedSince { return [[APIClient hogeData:hogeId modifiedSince:modifiedSince] continueWithExecutor: [RealmManager writeExecutor] withSuccessBlock:^id(BFTask *task) { // APIͷ໭ΓΛνΣοΫͯࠩ͠෼ΛRealmʹॻ͖ࠐΈ EntityHoge *hoge = [DataConverter toHoge:task.result]; [RealmManager commit:^(RLMRealm *realm) { [realm addOrUpdateObject:hoge]; }]; }]; } @end ServiceʹreadɺfetchͳͲܾ·ͬͨIFΛ༻ҙ͢Δ͜ͱͰѻ͍΍͘͢ͳΔɻ ߋ৽೔࣌Λ౉͢͜ͱͰAPI͔Β͸มߋ/ࠩ෼σʔλͷΈΛऔಘͯ͠Realmʹอଘɻ read͸εϨουΛލ͙͜ͱΛߟྀͯ͠ຖճrefreshΛݺͼग़͔ͯ͠Βऔಘɻ
  4. ViewController @property (nonatomic, strong) EntityHoge *hoge; - (void)loadFromRealm { self.hoge

    = [[HogeService shared] read:self.hogeId]; if (!self.hoge) { // no cache return; } // Show cache } - (void)loadFromAPI { NSInteger modifiedSince = (self.hoge) ? self.hoge.updatedAt : 0; [[[HogeService sharedService] fetch:self.hogeId withModifiedSince:modifiedSince] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) { // Realm͔Βऔಘ͠௚ͯ͠ViewΛߋ৽ [self loadFromRealm]; return nil; }]; } Realmʹσʔλ͕͋Δ৔߹͸ɺAPIʹมߋ೔Λ౉ͯ͠มߋ/ࠩ෼σʔλͷΈऔಘͯ͠ ߋ৽͕͋ͬͨσʔλͷΈViewΛߋ৽͢Δɻ Bolts͸ඇಉظॲཧΛϝιουνΣʔϯͰهड़Ͱ͖ɺThread΋ࢦఆͰ͖ΔͷͰRealmΛ ѻ͍΍͍͢ɻNotification͸࢖ͬͯͳ͍ɻ
  5. RLMRealm 3FBMNͷQBUIɺॳظԽɺ3FBMNΠϯελϯεͷऔಘɺ.JHSBUJPOɺ8SJUF༻ εϨουRVFVFऔಘͳͲ͸XSBQQFSDMBTTΛ࡞ͬͯవΊ͓ͯ͘ͱΑ͍ … + (void)prepareRealms { static dispatch_once_t once;

    dispatch_once(&once, ^{ defaultDataPath = [RealmManager dataRealmPath:kRealmDataFileName]; defaultCachePath = [RealmManager cacheRealmPath:kRealmCacheFileName]; [RLMRealm setDefaultRealmPath:defaultCachePath]; [RLMRealm setDefaultRealmSchemaVersion:kCurrentSchemeVersion withMigrationBlock:^(RLMMigration *migration, uint64_t oldSchemaVersion) { }]; [RLMRealm defaultRealm]; }); } + (RLMRealm *) realm { return [RLMRealm defaultRealm]; } …