From b231437add9c1ecdd2ae10140f1abcdb88e2ab62 Mon Sep 17 00:00:00 2001 From: Trevor Stone Date: Sun, 3 Jan 2016 19:00:53 -0800 Subject: [PATCH 1/3] Add mobile instructions to the Readme --- README.md | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/README.md b/README.md index cc968ce..f477fba 100644 --- a/README.md +++ b/README.md @@ -545,6 +545,79 @@ if err != nil { } ``` +### Mobile Use (iOS/Android) + +Bolt is able to run on mobile devices by leveraging the binding feature of the +[gomobile](https://github.com/golang/mobile) tool. Create a struct that will +contain your database logic and a reference to a *bolt.DB with a initializing +contstructor that takes in a filepath where the database file will be stored. +Neither Android nor iOS require extra permissions or cleanup from using this method. + +```go +func NewBoltDB(filepath string) *BoltDB { + db, err := bolt.Open(filepath+"/demo.db", 0600, nil) + if err != nil { + log.Fatal(err) + } + + return &BoltDB{db} +} + +type BoltDB struct { + db *bolt.DB + ... +} + +func (b *BoltDB) Path() string { + return b.db.Path() +} + +func (b *BoltDB) Close() { + b.db.Close() +} +``` + +Database logic should be defined as methods on this wrapper struct. + +To initialize this struct from the native language (both platforms now sync +their local storage to the cloud. These snippits disable that functionality for the +database file): +####Android +```java +String path; +if (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.LOLLIPOP){ + path = getNoBackupFilesDir().getAbsolutePath(); +} else{ + path = getFilesDir().getAbsolutePath(); +} +Boltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path) +``` + +####iOS +```objc +- (void)demo { + NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; + GoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path); + [self addSkipBackupAttributeToItemAtPath:demo.path]; + //Some DB Logic would go here + [demo close]; +} + +- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString +{ + NSURL* URL= [NSURL fileURLWithPath: filePathString]; + assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]); + + NSError *error = nil; + BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES] + forKey: NSURLIsExcludedFromBackupKey error: &error]; + if(!success){ + NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error); + } + return success; +} + +``` ## Resources From e04ef19dab0557315938708a6bec6c245c01a1bb Mon Sep 17 00:00:00 2001 From: Trevor Stone Date: Sun, 3 Jan 2016 19:03:55 -0800 Subject: [PATCH 2/3] Fix Readme typo and line length --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f477fba..b478e57 100644 --- a/README.md +++ b/README.md @@ -580,7 +580,7 @@ func (b *BoltDB) Close() { Database logic should be defined as methods on this wrapper struct. To initialize this struct from the native language (both platforms now sync -their local storage to the cloud. These snippits disable that functionality for the +their local storage to the cloud. These snippets disable that functionality for the database file): ####Android ```java @@ -596,7 +596,9 @@ Boltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path) ####iOS ```objc - (void)demo { - NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; + NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, + NSUserDomainMask, + YES) objectAtIndex:0]; GoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path); [self addSkipBackupAttributeToItemAtPath:demo.path]; //Some DB Logic would go here From 4171c1783c4864dc9e528dffa3447932285014db Mon Sep 17 00:00:00 2001 From: Trevor Stone Date: Sun, 3 Jan 2016 19:23:43 -0800 Subject: [PATCH 3/3] add table of contents entry from mobile --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b478e57..a582e86 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ services every day. - [Database backups](#database-backups) - [Statistics](#statistics) - [Read-Only Mode](#read-only-mode) + - [Mobile Use (iOS/Android)](#mobile-use-iosandroid) - [Resources](#resources) - [Comparison with other databases](#comparison-with-other-databases) - [Postgres, MySQL, & other relational databases](#postgres-mysql--other-relational-databases)