For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.


Create databases

Parent topic: Migrating apps storing mobile data in Cloudant with IMFData or Cloudant SDK

Android


Accessing local data stores

We can use a local data store to store data on the client device for fast access, even when offline.


Procedure

To create Store objects to access a local database, supply a name for the data store.

Important: The database name must be in lowercase.

BEFORE (with IMFData/CloudantToolkit):

//Get reference to data manager IMFDataManager *manager = [IMFDataManager sharedInstance]; NSString *name = @"automobiledb"; NSError *error = nil; //Create local store CDTStore *store = [manager localStore:name error:&error];

let manager = IMFDataManager.sharedInstance() let name = "automobiledb" var store:CDTStore? do { store = try manager.localStore(name) } catch let error as NSError { // Handle error } Android

// Get reference to DataManager DataManager manager = DataManager.getInstance(); // Create local store String name = "automobiledb"; Task<Store> storeTask = manager.localStore(name); storeTask.continueWith(new Continuation<Store, Void>() { @Override public Void then(Task<Store> task) throws Exception { if(task.isFaulted()){ // Handle error }else{ // Do something with Store Store store = task.getResult(); } return null; } }); AFTER (with Cloudant Sync):

// Get reference to datastore manager CDTDatastoreManager *datastoreManager = existingDatastoreManager; NSString *name = @"automobiledb"; NSError *error = nil; //Create datastore CDTDatastore *datastore = [datastoreManager datastoreNamed:name error:&error];

// Get reference to datastore manager let datastoreManager:CDTDatastoreManager = existingDatastoreManager let name:String = "automobiledb" //Create local store var datastore:CDTDatastore? do{ datastore = try datastoreManager.datastoreNamed(name) }catch let error as NSError{ // Handle error } Android

// Create DatastoreManager File path = context.getDir("databasedir", Context.MODE_PRIVATE); DatastoreManager manager = new DatastoreManager(path.getAbsolutePath()); // Create a Datastore String name = "automobiledb"; Datastore datastore = manager.openDatastore(name);


Creating remote data stores


Procedure

To save data in the remote store, supply the data store name.

BEFORE (with IMFData/CloudantToolkit):

// Get reference to data manager IMFDataManager *manager = [IMFDataManager sharedInstance]; NSString *name = @"automobiledb"; // Create remote store [manager remoteStore:name completionHandler:^(CDTStore *createdStore, NSError *error) { if(error){ // Handle error }else{ CDTStore *store = createdStore; NSLog(@"Successfully created store: %@", store.name); } }];

let manager = IMFDataManager.sharedInstance() let name = "automobiledb" manager.remoteStore(name, completionHandler: { (createdStore:CDTStore!, error:NSError!) -> Void in if nil != error { //Handle error } else { let store:CDTStore = createdStore print("Successfully created store: \(store.name)") } }) Android

// Get reference to DataManager DataManager manager = DataManager.getInstance(); // Create remote store String name = "automobiledb"; Task<Store> storeTask = manager.remoteStore(name); storeTask.continueWith(new Continuation<Store, Void>() { @Override public Void then(Task<Store> task) throws Exception { if(task.isFaulted()){ // Handle error }else{ // Do something with Store Store store = task.getResult(); } return null; } });

AndroidAFTER (with Cloudant Sync):

See Cloudant REST documentation for Database Create.