Repository: realm/realm-swift
File: examples/RLMDemo/RLMDemo/RLMTableViewController.m (Objective-C)

Code before:

@end

@implementation RLMTableViewController

#pragma mark - View Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                           target:self
                                                                                           action:@selector(add)];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellID];
    
    [self setupRealm];
}

#pragma mark - Realm

- (void)setupRealm {
    // @@Example: setup_contexts @@
    // Set up read/write contexts
    self.realm   = [RLMRealm realmWithDefaultPersistence];
    self.context = [RLMContext contextWithDefaultPersistence];
    // @@EndExample@@
    
    // @@Example: create_table @@
    // Create table if it doesn't exist
    [self.context writeUsingBlock:^(RLMRealm *realm) {
        if (realm.isEmpty) {
            [realm createTableWithName:kTableName asTableClass:[RLMDemoTable class]];
        }
    }];
    // @@EndExample@@
    
    // @@Example: setup_notifications @@
    // Observe Realm Notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(realmContextDidChange)
                                                 name:RLMContextDidChangeNotification
                                               object:nil];
}

- (void)realmContextDidChange {
    [self.tableView reloadData];
}
// @@EndExample@@

- (RLMDemoTable *)table {
    if (!_table) {
        // @@Example: get_table @@

Review comment (suggestion):
It's definitely possible to use just a single `RLMContext` for both reading and writing, in blocks, everywhere. But as you said, the `RLMRealm` allows us to do inline reads, cleaning up the code a bit. There won't be nearly as much confusion once we can write with `RLMRealm`s.


Code after:
@implementation RLMTableViewController

#pragma mark - View Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                           target:self
                                                                                           action:@selector(add)];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellID];
    
    [self setupRealm];
}

#pragma mark - Realm

- (void)setupRealm {
    // @@Example: setup_contexts @@
    // Set up read/write contexts
    self.realm = [RLMRealm realmWithDefaultPersistenceAndInitBlock:^(RLMRealm *realm) {
        // Create table if it doesn't exist
        if (realm.isEmpty) {
            [realm createTableWithName:kTableName asTableClass:[RLMDemoTable class]];
        }
    }];
    self.context = [RLMContext contextWithDefaultPersistence];
    // @@EndExample@@
    
    // @@Example: setup_notifications @@
    // Observe Realm Notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(realmContextDidChange)
                                                 name:RLMContextDidChangeNotification
                                               object:nil];
}

- (void)realmContextDidChange {
    [self.tableView reloadData];
}
// @@EndExample@@

- (RLMDemoTable *)table {
    if (!_table) {
        // @@Example: get_table @@
        // Get table with specified name and class from the realm
        _table = [self.realm tableWithName:kTableName asTableClass:[RLMDemoTable class]];
        // @@EndExample@@
    }
    return _table;
}