Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling from Jesper (and much more) #30

Merged
merged 6 commits into from
Feb 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions doc/error_handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Category 1: NSError for "expected" problems
===========================================
Characterized by: File access problem, network access problems.... User should be informed. Problems which cannot be eliminated during development.

- Writing a group to a file, with file access problems.


Category 2: NSException for "unexpected" problems - no recovery
===============================================================
Characterized by: Fatal errors. Wrong use of API. Application should rightfully crash. These problems should be eliminated during development. Feedback should be fast and direct in the form of an exception. The
application should not attempt to catch the exception.

- Indexing out of bounds.
- Passing illegal parameter type.
- Writing to read only table (working assumption).
- Core library exceptions.


Category 3: NSException for "unexpected" problems - with recovery
=================================================================
Technically possible with ARC safe exceptions flag. Leads to excessive release code (automatically inserted). Should not be needed.








19 changes: 9 additions & 10 deletions doc/ref/examples/ex_objc_group_intro.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ int main()

/* Creates a group and uses it to create a new table. */

TightdbGroup *group = [TightdbGroup group];
PeopleTable *table = [group getTable:@"people" withClass:[PeopleTable class]];
TightdbGroup* group = [TightdbGroup group];
PeopleTable* table = [group getTable:@"people" withClass:[PeopleTable class] error:nil];

/* Adds values to the table. */

Expand All @@ -31,26 +31,25 @@ int main()
/* Write the group (and the contained table) to a specified file. */

[[NSFileManager defaultManager] removeItemAtPath:@"filename.tightdb" error:nil];
[group write:@"filename.tightdb"];
[group writeToFile:@"filename.tightdb" withError:nil];

/* Adds another row to the table. Note the update is NOT persisted
automatically (delete the old file and use write again). */

[table addName:@"Sam" Age:17];

[[NSFileManager defaultManager] removeItemAtPath:@"filename.tightdb" error:nil];
[group write:@"filename.tightdb"];
[group writeToFile:@"filename.tightdb" withError:nil];

/* Retrieves an in memory buffer from the group. */

size_t size;
const char *buffer = [group writeToMem:&size];
TightdbBinary* buffer = [group writeToBuffer];

TightdbGroup *groupFromMemory = [TightdbGroup groupWithBuffer:buffer size:size];
PeopleTable *tableFromMemery = [groupFromMemory getTable:@"people" withClass:[PeopleTable class] error:nil];
/* Creates a group from an im memory buffer */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo im => in

TightdbGroup* groupFromMemory = [TightdbGroup groupWithBuffer:buffer withError:nil];
PeopleTable* tableFromMemery = [groupFromMemory getTable:@"people" withClass:[PeopleTable class] error:nil];

for (PeopleTable_Cursor *cursor in tableFromMemery)
{
for (PeopleTable_Cursor* cursor in tableFromMemery) {
NSLog(@"Name: %@", cursor.Name);
}

Expand Down
60 changes: 51 additions & 9 deletions doc/ref/examples/ex_objc_sharedgroup_intro.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,94 @@
#import <tightdb/objc/table.h>
#import <tightdb/objc/tightdb.h>


TIGHTDB_TABLE_3(PeopleTable,
Name, String,
Age, Int,
Hired, Bool);




int main()
{
@autoreleasepool {

/* Creates a group and uses it to create a new table. */

TightdbSharedGroup *shared = [TightdbSharedGroup groupWithFilename:@"sharedgroup.tightdb"];
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtPath:@"sharedgrouptest.tightdb" error:nil];
[fm removeItemAtPath:@"sharedgrouptest.tightdb.lock" error:nil];

TightdbSharedGroup *shared = [TightdbSharedGroup sharedGroupWithFile:@"sharedgrouptest.tightdb" withError:nil];
if (!shared) {
NSLog(@"Error");
}
else {
NSLog(@"%@", shared);
}

/* A write transaction (with rollback if not first writer to employees table). */
/* A write transaction (with commit). */

[shared writeTransaction:^(TightdbGroup *group) {
NSError *error = nil;
BOOL success;

success = [shared writeTransactionWithError:&error withBlock:^(TightdbGroup *group) {

/* Write transactions with the shared group are possible via the provided variable binding named group. */

PeopleTable *table = [group getTable:@"employees" withClass:[PeopleTable class]];
PeopleTable *table = [group getTable:@"employees" withClass:[PeopleTable class] error:nil];

if ([table count] > 0) {
NSLog(@"Not empty!");
return NO; /* Rollback */
}

[table addName:@"Bill" Age:53 Hired:YES];
NSLog(@"Row added!");
NSLog(@"Commit!");
return YES; /* Commit */
} ];

if(!success)
NSLog(@"Error : %@", [error localizedDescription]);

/* A write transaction (with rollback). */

success = [shared writeTransactionWithError:&error withBlock:^(TightdbGroup *group) {

/* Write transactions with the shared group are possible via the provided variable binding named group. */

PeopleTable *table = [group getTable:@"employees" withClass:[PeopleTable class] error:nil];

if ([table count] > 0) {
NSLog(@"Roll back!");
return NO; /* Rollback */
}

[table addName:@"Bill" Age:53 Hired:YES];
NSLog(@"Commit!");
return YES; /* Commit */
}];

if(!success)
NSLog(@"Error : %@", [error localizedDescription]);

}];

/* A read transaction */

[shared readTransaction:^(TightdbGroup *group) {
[shared readTransactionWithBlock:^(TightdbGroup *group) {

/* Read transactions with the shared group are possible via the provided variable binding named group. */

PeopleTable *table = [group getTable:@"employees" withClass:[PeopleTable class]];
PeopleTable *table = [group getTable:@"employees" withClass:[PeopleTable class] error:nil];

for (PeopleTable_Cursor *curser in table) {
NSLog(@"Name: %@", [curser Name]);
}
}];


}

}

/* @@EndExample@@ */
2 changes: 0 additions & 2 deletions doc/ref/examples/ex_objc_table_dynamic_intro.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ int main()
TightdbCursor *c2 = [table cursorAtIndex:[table count]];
if (c2 != nil)
NSLog(@"Should not get here.");


}
}

Expand Down
15 changes: 9 additions & 6 deletions doc/ref/examples/ex_objc_table_typed_intro.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* @@Example: ex_objc_table_typed_intro @@ */


#import <tightdb/objc/table.h>
#import <tightdb/objc/tightdb.h>

Expand Down Expand Up @@ -28,6 +27,15 @@ int main()
cursor.Name = @"Sofie";
cursor.Age = 40;

/*
[table addOrInsertRowAtIndex:[table count]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove this from now.

Name:@"Jesper"
Age:200];
*/
cursor = [table addRow];
cursor.Name = @"Jesper";
cursor.Age = 200;

NSLog(@"The size of the table is now %zd", [table count]);

for (PeopleTable_Cursor *ite in table) {
Expand All @@ -50,12 +58,7 @@ int main()
TightdbCursor *c3 = [table cursorAtIndex:[table count]];
if (c3 != nil)
NSLog(@"Should not get here.");


}
}




/* @@EndExample@@ */
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main()
for (PeopleTable_Cursor *curser in query)
NSLog(@"Name: %lld", [curser Age]);

/* To avoid repeating the same quiry, the result may be stored in
/* To avoid repeating the same query, the result may be stored in
* a table view for multiple access. The following code executes the
* query once and saves the result in a table view. */

Expand Down
53 changes: 18 additions & 35 deletions src/tightdb/objc/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,15 @@
-(void)setNdx:(size_t)ndx;
-(size_t)index;

-(BOOL)setInt:(int64_t)value inColumn:(size_t)colNdx;
-(BOOL)setInt:(int64_t)value inColumn:(size_t)colNdx error:(NSError *__autoreleasing *)error;
-(BOOL)setString:(NSString *)value inColumn:(size_t)colNdx;
-(BOOL)setString:(NSString *)value inColumn:(size_t)colNdx error:(NSError *__autoreleasing *)error;
-(BOOL)setBool:(BOOL)value inColumn:(size_t)colNdx;
-(BOOL)setBool:(BOOL)value inColumn:(size_t)colNdx error:(NSError *__autoreleasing *)error;
-(BOOL)setFloat:(float)value inColumn:(size_t)colNdx;
-(BOOL)setFloat:(float)value inColumn:(size_t)colNdx error:(NSError *__autoreleasing *)error;
-(BOOL)setDouble:(double)value inColumn:(size_t)colNdx;
-(BOOL)setDouble:(double)value inColumn:(size_t)colNdx error:(NSError *__autoreleasing *)error;
-(BOOL)setDate:(time_t)value inColumn:(size_t)colNdx;
-(BOOL)setBinary:(TightdbBinary *)value inColumn:(size_t)colNdx;
-(BOOL)setBinary:(TightdbBinary *)value inColumn:(size_t)colNdx error:(NSError *__autoreleasing *)error;
-(BOOL)setMixed:(TightdbMixed *)value inColumn:(size_t)colNdx;
-(BOOL)setMixed:(TightdbMixed *)value inColumn:(size_t)colNdx error:(NSError *__autoreleasing *)error;
-(BOOL)setTable:(TightdbTable *)value inColumn:(size_t)colNdx;
-(void)setInt:(int64_t)value inColumn:(size_t)colNdx;
-(void)setString:(NSString *)value inColumn:(size_t)colNdx;
-(void)setBool:(BOOL)value inColumn:(size_t)colNdx;
-(void)setFloat:(float)value inColumn:(size_t)colNdx;
-(void)setDouble:(double)value inColumn:(size_t)colNdx;
-(void)setDate:(time_t)value inColumn:(size_t)colNdx;
-(void)setBinary:(TightdbBinary *)value inColumn:(size_t)colNdx;
-(void)setMixed:(TightdbMixed *)value inColumn:(size_t)colNdx;
-(void)setTable:(TightdbTable *)value inColumn:(size_t)colNdx;

-(int64_t)getIntInColumn:(size_t)colNdx;
-(NSString *)getStringInColumn:(size_t)colNdx;
Expand All @@ -66,31 +59,21 @@
@interface TightdbAccessor: NSObject
-(id)initWithCursor:(TightdbCursor *)cursor columnId:(size_t)columnId;
-(BOOL)getBool;
-(BOOL)setBool:(BOOL)value;
-(BOOL)setBool:(BOOL)value error:(NSError *__autoreleasing *)error;
-(void)setBool:(BOOL)value;
-(int64_t)getInt;
-(BOOL)setInt:(int64_t)value;
-(BOOL)setInt:(int64_t)value error:(NSError *__autoreleasing *)error;
-(void)setInt:(int64_t)value;
-(float)getFloat;
-(BOOL)setFloat:(float)value;
-(BOOL)setFloat:(float)value error:(NSError *__autoreleasing *)error;
-(void)setFloat:(float)value;
-(double)getDouble;
-(BOOL)setDouble:(double)value;
-(BOOL)setDouble:(double)value error:(NSError *__autoreleasing *)error;
-(void)setDouble:(double)value;
-(NSString *)getString;
-(BOOL)setString:(NSString *)value;
-(BOOL)setString:(NSString *)value error:(NSError *__autoreleasing *)error;
-(void)setString:(NSString *)value;
-(TightdbBinary *)getBinary;
-(BOOL)setBinary:(TightdbBinary *)value;
-(BOOL)setBinary:(TightdbBinary *)value error:(NSError *__autoreleasing *)error;
-(BOOL)setBinary:(const char *)data size:(size_t)size;
-(BOOL)setBinary:(const char *)data size:(size_t)size error:(NSError *__autoreleasing *)error;
-(void)setBinary:(TightdbBinary *)value;
-(time_t)getDate;
-(BOOL)setDate:(time_t)value;
-(BOOL)setDate:(time_t)value error:(NSError *__autoreleasing *)error;
-(BOOL)setSubtable:(TightdbTable *)subtable;
-(void)setDate:(time_t)value;
-(void)setSubtable:(TightdbTable *)value;
-(id)getSubtable:(Class)obj;
-(TightdbMixed *)getMixed;
-(BOOL)setMixed:(TightdbMixed *)value;
-(BOOL)setMixed:(TightdbMixed *)value error:(NSError *__autoreleasing *)error;
-(void)setMixed:(TightdbMixed *)value;
@end
Loading