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


Performing CRUD operations

We can modify the content of a data store.


Overview

For more details on create, retrieve, update, and delete (CRUD) operations, see CDTDatastore CRUD documentation.

For create, retrieve, update, and delete (CRUD) operations on a remote store, see the Cloudant® Document API

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


Creating data


Procedure

Save data.

AndroidBEFORE (with IMFData/CloudantToolkit):

// Use an existing store CDTStore *store = existingStore; // Create your Automobile to save Automobile *automobile = [[Automobile alloc] initWithMake:@"Toyota" model:@"Corolla" year: 2006]; [store save:automobile completionHandler:^(id savedObject, NSError *error) { if (error) { // save was not successful, handler received an error } else { // use the result Automobile *savedAutomobile = savedObject; NSLog(@"saved revision: %@", savedAutomobile); } }];

// Use an existing store let store:CDTStore = existingStore // Create your object to save let automobile = Automobile(make: "Toyota", model: "Corolla", year: 2006) store.save(automobile, completionHandler: { (savedObject:AnyObject!, error:NSError!) -> Void in if nil != error { //Save was not successful, handler received an error } else { // Use the result print("Saved revision: \(savedObject)") } }) Android

// Use an existing store Store store = existingStore; // Create your object to save Automobile automobile = new Automobile("Toyota", "Corolla", 2006); // Save automobile to store Task<Object> saveTask = store.save(automobile); saveTask.continueWith(new Continuation<Object, Void>() { @Override public Void then(Task<Object> task) throws Exception { if (task.isFaulted()) { // save was not successful, task.getError() contains the error } else { // use the result Automobile savedAutomobile = (Automobile) task.getResult(); } return null; } });

AndroidAFTER (with Cloudant Sync):

// Use an existing store CDTDatastore *datastore = existingDatastore; // Create document body CDTMutableDocumentRevision * revision = [CDTMutableDocumentRevision revision]; revision.body = @{@"@datatype" : @"Automobile", @"make" :@"Toyota", @"model": @"Corolla", @"year" : @2006}; NSError *error = nil; CDTDocumentRevision *createdRevision = [datastore createDocumentFromRevision:revision error:&error]; if (error) { // save was not successful, handler received an error } else { // use the result NSLog(@"Revision: %@", createdRevision); }

// Use an existing store let datastore:CDTDatastore = existingDatastore // Create document body let revision = CDTMutableDocumentRevision() revision.setBody(["make":"Toyota","model":"Corolla","year":2006]) var createdRevision:CDTDocumentRevision? do{ createdRevision = try datastore.createDocumentFromRevision(revision) NSLog("Revision: \(createdRevision)"); }catch let error as NSError{ // Handle error } Android

// Use an existing store Datastore datastore = existingStore; // Create document body Map<String, Object> body = new HashMap<String, Object>(); body.put("@datatype", "Automobile"); body.put("make", "Toyota"); body.put("model", "Corolla"); body.put("year", 2006); // Create revision and set body MutableDocumentRevision revision = new MutableDocumentRevision(); revision.body = DocumentBodyFactory.create(body); // Save revision to store DocumentRevision savedRevision = datastore.createDocumentFromRevision(revision);


Reading data

We can fetch data.


Procedure

Read data.

AndroidBEFORE (with IMFData/CloudantToolkit):

CDTStore *store = existingStore; NSString *automobileId = existingAutomobileId; // Fetch Autombile from Store [store fetchById:automobileId completionHandler:^(id object, NSError *error) { if (error) { // fetch was not successful, handler received an error } else { // use the result Automobile *savedAutomobile = object; NSLog(@"fetched automobile: %@", savedAutomobile); } }];

// Using an existing store and Automobile let store:CDTStore = existingStore let automobileId:String = existingAutomobileId // Fetch Autombile from Store store.fetchById(automobileId, completionHandler: { (object:AnyObject!, error:NSError!) -> Void in if nil != error { // Fetch was not successful, handler received an error } else { // Use the result let savedAutomobile:Automobile = object as! Automobile print("Fetched automobile: \(savedAutomobile)") } }) Android

// Use an existing store and documentId Store store = existingStore; String automobileId = existingAutomobileId; // Fetch the automobile from the store Task<Object> fetchTask = store.fetchById(automobileId); fetchTask.continueWith(new Continuation<Object, Void>() { @Override public Void then(Task<Object> task) throws Exception { if (task.isFaulted()) { // fetch was not successful, task.getError() contains the error } else { // use the result Automobile fetchedAutomobile = (Automobile) task.getResult(); } return null; } });

AndroidAFTER (with Cloudant Sync):

// Use an existing store and documentId CDTDatastore *datastore = existingDatastore; NSString *documentId = existingDocumentId; // Fetch the CDTDocumentRevision from the store NSError *error = nil; CDTDocumentRevision *fetchedRevision = [datastore getDocumentWithId:documentId error:&error]; if (error) { // fetch was not successful, handler received an error } else { // use the result NSLog(@"Revision: %@", fetchedRevision); }

// Use an existing store and documentId let datastore:CDTDatastore = existingDatastore let documentId:String = existingDocumentId var fetchedRevision:CDTDocumentRevision? do{ fetchedRevision = try datastore.getDocumentWithId(documentId) NSLog("Revision: \(fetchedRevision)"); }catch let error as NSError{ // Handle error } Android

// Use an existing store and documentId Datastore datastore = existingStore; String documentId = existingDocumentId; // Fetch the revision from the store DocumentRevision fetchedRevision = datastore.getDocument(documentId);


Updating data

To update an object, run a save on an existing object. Because the item exists, it is updated.


Procedure

Update objects.

AndroidBEFORE (with IMFData/CloudantToolkit):

// Use an existing store and Automobile CDTStore *store = existingStore; Automobile *automobile = existingAutomobile; // Update some of the values in the Automobile automobile.year = 2015; // Save Autombile to the store [store save:automobile completionHandler:^(id savedObject, NSError *error) { if (error) { // sasve was not successful, handler received an error } else { // use the result Automobile *savedAutomobile = savedObject; NSLog(@"saved automobile: %@", savedAutomobile); } }];

// Use an existing store and Automobile let store:CDTStore = existingStore let automobile:Automobile = existingAutomobile // Update some of the values in the Automobile automobile.year = 2015 // Save Autombile to the store store.save(automobile, completionHandler: { (savedObject:AnyObject!, error:NSError!) -> Void in if nil != error { // Update was not successful, handler received an error } else { // Use the result let savedAutomobile:Automobile = savedObject as! Automobile print("Updated automobile: \(savedAutomobile)") } }) Android

// Use an existing store and Automobile Store store = existingStore; Automobile automobile = existingAutomobile; // Update some of the values in the Automobile automobile.setYear(2015); // Save automobile to store Task<Object> saveTask = store.save(automobile); saveTask.continueWith(new Continuation<Object, Void>() { @Override public Void then(Task<Object> task) throws Exception { if (task.isFaulted()) { // save was not successful, task.getError() contains the error } else { // use the result Automobile savedAutomobile = (Automobile) task.getResult(); } return null; } });

AndroidAFTER (with Cloudant Sync):

// Use an existing store and document CDTDatastore *datastore = existingDatastore; CDTMutableDocumentRevision *documentRevision = [existingDocumentRevision mutableCopy]; // Update some of the values in the revision [documentRevision.body setValue:@2015 forKey:@"year"]; NSError *error = nil; CDTDocumentRevision *updatedRevision = [datastore updateDocumentFromRevision:documentRevision error:&error]; if (error) { // save was not successful, handler received an error } else { // use the result NSLog(@"Revision: %@", updatedRevision); }

// Use an existing store and document let datastore:CDTDatastore = existingDatastore let documentRevision:CDTMutableDocumentRevision = existingDocumentRevision.mutableCopy() // Update some of the values in the revision documentRevision.body()["year"] = 2015 var updatedRevision:CDTDocumentRevision? do{ updatedRevision = try datastore.updateDocumentFromRevision(documentRevision) NSLog("Revision: \(updatedRevision)"); }catch let error as NSError{ // Handle error } Android

// Use an existing store and documentId // Use an existing store Datastore datastore = existingStore; // Make a MutableDocumentRevision from the existing revision MutableDocumentRevision revision = existingRevision.mutableCopy(); // Update some of the values in the revision Map<String, Object> body = revision.getBody().asMap(); body.put("year", 2015); revision.body = DocumentBodyFactory.create(body); // Save revision to store DocumentRevision savedRevision = datastore.updateDocumentFromRevision(revision);


Deleting data

To delete an object, pass the object that we want to delete to the store.


Procedure

Delete objects.

AndroidBEFORE (with IMFData/CloudantToolkit):

// Using an existing store and Automobile CDTStore *store = existingStore; Automobile *automobile = existingAutomobile; // Delete the Automobile object from the store [store delete:automobile completionHandler:^(NSString *deletedObjectId, NSString *deletedRevisionId, NSError *error) { if (error) { // delete was not successful, handler received an error } else { // use the result NSLog(@"deleted Automobile doc-%@-rev-%@", deletedObjectId, deletedRevisionId); } }];

// Using an existing store and Automobile let store:CDTStore = existingStore let automobile:Automobile = existingAutomobile // Delete the Automobile object store.delete(automobile, completionHandler: { (deletedObjectId:String!, deletedRevisionId:String!, error:NSError!) -> Void in if nil != error { // delete was not successful, handler received an error } else { // use the result print("deleted document doc-\(deletedObjectId)-rev-\(deletedRevisionId)") } }) Android

// Use an existing store and automobile Store store = existingStore; Automobile automobile = existingAutomobile; // Delete the automobile from the store Task<String> deleteTask = store.delete(automobile); deleteTask.continueWith(new Continuation<String, Void>() { @Override public Void then(Task<String> task) throws Exception { if (task.isFaulted()) { // delete was not successful, task.getError() contains the error } else { // use the result String deletedAutomobileId = task.getResult(); } return null; } });

AndroidAFTER (with Cloudant Sync):

// Use an existing store and revision CDTDatastore *datastore = existingDatastore; CDTDocumentRevision *documentRevision = existingDocumentRevision; // Delete the CDTDocumentRevision from the store NSError *error = nil; CDTDocumentRevision *deletedRevision = [datastore deleteDocumentFromRevision:documentRevision error:&error]; if (error) { // delete was not successful, handler received an error } else { // use the result NSLog(@"deleted document: %@", deletedRevision); }

// Use an existing store and revision let datastore:CDTDatastore = existingDatastore let documentRevision:CDTDocumentRevision = existingDocumentRevision var deletedRevision:CDTDocumentRevision? do{ deletedRevision = try datastore.deleteDocumentFromRevision(documentRevision) NSLog("Revision: \(deletedRevision)"); }catch let error as NSError{ // Handle error } Android

// Use an existing store and revision Datastore datastore = existingStore; BasicDocumentRevision documentRevision = (BasicDocumentRevision) existingDocumentRevision; // Delete revision from store DocumentRevision deletedRevision = datastore.deleteDocumentFromRevision(documentRevision);