Skip to content

Instantly share code, notes, and snippets.

@yuuki1224
Created February 11, 2015 06:14
Show Gist options
  • Save yuuki1224/8ae09d13f6c5bc890f9b to your computer and use it in GitHub Desktop.
Save yuuki1224/8ae09d13f6c5bc890f9b to your computer and use it in GitHub Desktop.
- invalidate method (in Realm)
// - (void)invalidate
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
User *user = [[User alloc] init];
user.age = 22;
[realm addObject:user];
}];
// データをfetchしてくると、ここでのデータの状況を保つため、オブジェクトの中間バージョンファイルが作成され、
// RLMRealmオブジェクトによって管理される.
User *fetchedUser = [[User allObjects [fetchedUsers firstObject]];
// 他のスレッド
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
RLMRealm *sameRealm = [RLMRealm defaultRealm];
// ここでアップデートする
[sameRealm transactionWithBlock:^{
User *user = [[User allObjects] firstObject];
user.age = 30;
}];
});
// 別スレッドで処理が終わるのを待つ
[NSThread sleepForTimeInterval:10];
NSLog(@"user.age = @d", fetchedUser.age); // => "user.age = 22"
// 中間バージョンファイルを解放
[realm invalidate];
NSLog(@"user.age = @d", fetchedUser.age); // => "user.age = 30"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment