Repository: realm/realm-swift
File: examples/iOSTutorial/AppDelegate.m (Objective-C)

Code before:
        NSLog(@"%@ is %lld years old", c.Name, c.Age);

    // @@EndExample@@

}

void sharedGroupFunc() {

    // @@Example: transaction @@
    TDBSharedGroup *sharedGroup = [TDBSharedGroup sharedGroupWithFile:@"people.tightdb"
                                                            withError:nil];

    // Start a write transaction
    [sharedGroup writeWithBlock:^(TDBGroup *group) {
        // Get a specific table from the group
        PeopleTable *table = [group getOrCreateTableWithName:@"employees"
                                                asTableClass:[PeopleTable class]
                                                       error:nil];

        // Add a row
        [table addName:@"Bill" Age:53 Hired:YES];
        NSLog(@"Row added!");
        return YES; // Commit (NO would rollback)
    } withError:nil];

    // Start a read transaction
    [sharedGroup readWithBlock:^(TDBGroup *group) {
        // Get the table
        PeopleTable *table = [group getOrCreateTableWithName:@"employees"
                                                asTableClass:[PeopleTable class]
                                                       error:nil];

        // Interate over all rows in table
        for (PeopleTable_Cursor *row in table) {
            NSLog(@"Name: %@", row.Name);
        }
    }];
    // @@EndExample@@
}

void groupFunc() {

    // @@Example: serialisation @@
    TDBSharedGroup *sharedGroup = [TDBSharedGroup sharedGroupWithFile:@"people.tightdb"
                                                              withError:nil];

    // Within a single read transaction we can write a copy of the entire db to a new file.
    // This is usefull both for backups and for transfering datasets to other machines.
    [sharedGroup readWithBlock:^(TDBGroup *group) {
        // Write entire db to disk (in a new file)
        [group writeToFile:@"people_backup.tightdb" withError:nil];

Review comment (suggestion):
I think this was introduced to make it possible to execute the program more than once without errors.
But we should likely just delete the file before the transaction, outside the Example tags.


Code after:

}

void sharedGroupFunc() {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    [fileManager removeItemAtPath:@"people.tightdb" error:&error];

    // @@Example: transaction @@
    TDBSharedGroup *sharedGroup = [TDBSharedGroup sharedGroupWithFile:@"people.tightdb"
                                                            withError:nil];

    // Start a write transaction
    [sharedGroup writeWithBlock:^(TDBGroup *group) {
        // Get a specific table from the group
        PeopleTable *table = [group getOrCreateTableWithName:@"employees"
                                                asTableClass:[PeopleTable class]
                                                       error:nil];

        // Add a row
        [table addName:@"Bill" Age:53 Hired:YES];
        NSLog(@"Row added!");
        return YES; // Commit (NO would rollback)
    } withError:nil];

    // Start a read transaction
    [sharedGroup readWithBlock:^(TDBGroup *group) {
        // Get the table
        PeopleTable *table = [group getOrCreateTableWithName:@"employees"
                                                asTableClass:[PeopleTable class]
                                                       error:nil];

        // Interate over all rows in table
        for (PeopleTable_Cursor *row in table) {
            NSLog(@"Name: %@", row.Name);
        }
    }];
    // @@EndExample@@
}

void groupFunc() {

    // @@Example: serialisation @@
    TDBSharedGroup *sharedGroup = [TDBSharedGroup sharedGroupWithFile:@"people.tightdb"
                                                              withError:nil];

    // Within a single read transaction we can write a copy of the entire db to a new file.
    // This is usefull both for backups and for transfering datasets to other machines.
    [sharedGroup readWithBlock:^(TDBGroup *group) {
        // Write entire db to disk (in a new file)