AWA with Realm

AWA with Realm

F01523d30d0f0b7c154914a886406bc4?s=128

Yuji Hato

June 25, 2015
Tweet

Transcript

  1. 2.

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

    Android/iOSΛ୲౰͠ɺݱࡏ͸AWAͷiOSΤϯδ χΞɻ Engineer 5XJUUFSIUUQTUXJUUFSDPNEFLBUPUPSP (JU)VCIUUQTHJUIVCDPNEFLBUPUPSP
  2. 5.
  3. 20.

    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ͳͲ΋ఆ͓ٛͯ͘͠ͱศརɻ
  4. 22.

    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Λݺͼग़͔ͯ͠Βऔಘɻ
  5. 24.

    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͸࢖ͬͯͳ͍ɻ
  6. 26.

    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]; } …