Skip to content

Instantly share code, notes, and snippets.

@laiso
Created February 13, 2015 01:54
Show Gist options
  • Save laiso/c6ab988c406049c8c706 to your computer and use it in GitHub Desktop.
Save laiso/c6ab988c406049c8c706 to your computer and use it in GitHub Desktop.
//
// ViewController.m
#import "ViewController.h"
#import <Realm/Realm.h>
@interface QiitaTag : RLMObject
@property NSString *id;
@property NSString *iconUrl;
@property NSInteger followersCount;
@property NSInteger itemsCount;
@end
RLM_ARRAY_TYPE(QiitaTag);
@implementation QiitaTag
+ (NSString *)primaryKey {
return @"id";
}
+(NSDictionary *)defaultPropertyValues
{
return @{@"iconUrl": @"", @"followersCount": @0, @"itemsCount": @0};
}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
RLMRealm *realm = [RLMRealm defaultRealm];
RLMResults *tags = [QiitaTag allObjects];
NSLog(@"Load %@ tag items.", @(tags.count));
[realm transactionWithBlock:^{
for (int i=1; i < 100; i++) {
NSData *data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"/tmp/qiitags/%i.json", i]];
NSArray *tags = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
for (NSDictionary *object in tags) {
QiitaTag *tag = [QiitaTag new];
tag.id = object[@"id"];
if ([object[@"icon_url"] isKindOfClass:[NSString class]]) {
tag.iconUrl = object[@"icon_url"];
}
tag.itemsCount = [object[@"items_count"] integerValue];
tag.followersCount = [object[@"followers_count"] integerValue];
[realm addOrUpdateObject:tag];
NSLog(@"Put: %@", tag.id);
}
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment