For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.
Create indexes
To perform queries, we must create an index.
For more details, see CDTDatastore Query documentation. For query operations on a remote store, see the Cloudant® Query API.
For more details, see Cloudant Sync Query documentation. For CRUD operations on a remote store, see Cloudant's Query API.
Procedure
- Create an index that includes the data type. Indexing with the data type is useful when an object mapper is set on the data store.
BEFORE (with IMFData/CloudantToolkit):
// Use an existing data store CDTStore *store = existingStore; // The data type to use for the Automobile class NSString *dataType = [store.mapper dataTypeForClassName:NSStringFromClass([Automobile class])]; // Create the index [store createIndexWithDataType:dataType fields:@[@"year", @"make"] completionHandler:^(NSError *error) { if(error){ // Handle error }else{ // Continue application flow } }];
// A store that has been previously created. let store:CDTStore = existingStore // The data type to use for the Automobile class let dataType:String = store.mapper.dataTypeForClassName(NSStringFromClass(Automobile.classForCoder())) // Create the index store.createIndexWithDataType(dataType, fields: ["year","make"]) { (error:NSError!) -> Void in if nil != error { // Handle error } else { // Continue application flow } }
// Use an existing data store Store store = existingStore; // The data type to use for the Automobile class String dataType = store.getMapper().getDataTypeForClassName(Automobile.class.getCanonicalName()); // The fields to index. List<IndexField> indexFields = new ArrayList<IndexField>(); indexFields.add(new IndexField("year")); indexFields.add(new IndexField("make")); // Create the index Task<Void> indexTask = store.createIndexWithDataType(dataType, indexFields); indexTask.continueWith(new Continuation<Void, Void>() { @Override public Void then(Task<Void> task) throws Exception { if(task.isFaulted()){ // Handle error }else{ // Continue application flow } return null; } });
AFTER (with Cloudant Sync):
// A store that has been previously created. CDTDatastore *datastore = existingDatastore; NSString *indexName = [datastore ensureIndexed:@[@"@datatype", @"year", @"make"] withName:@"automobileindex"]; if(!indexName){ // Handle error }
// A store that has been previously created. let datastore:CDTDatastore = existingDatastore // Create the index let indexName:String? = datastore.ensureIndexed(["@datatype","year","make"], withName: "automobileindex") if(indexName == nil){ // Handle error }
// Use an existing store Datastore datastore = existingStore; // Create an IndexManager IndexManager indexManager = new IndexManager(datastore); // The fields to index. List<Object> indexFields = new ArrayList<Object>(); indexFields.add("@datatype"); indexFields.add("year"); indexFields.add("make"); // Create the index indexManager.ensureIndexed(indexFields, "automobile_index");
Delete indexes. BEFORE (with IMFData/CloudantToolkit):
// Use an existing data store CDTStore *store = existingStore; NSString *indexName = existingIndexName; // Delete the index [store deleteIndexWithName:indexName completionHandler:^(NSError *error) { if(error){ // Handle error }else{ // Continue application flow } }];
// Use an existing store let store:CDTStore = existingStore // The data type to use for the Automobile class let dataType:String = store.mapper.dataTypeForClassName(NSStringFromClass(Automobile.classForCoder())) // Delete the index store.deleteIndexWithDataType(dataType, completionHandler: { (error:NSError!) -> Void in if nil != error { // Handle error } else { // Continue application flow } })
// Use an existing data store Store store = existingStore; String indexName = existingIndexName; // Delete the index Task<Void> indexTask = store.deleteIndex(indexName); indexTask.continueWith(new Continuation<Void, Void>() { @Override public Void then(Task<Void> task) throws Exception { if(task.isFaulted()){ // Handle error }else{ // Continue application flow } return null; } });
AFTER (with Cloudant Sync):
// Use an existing store CDTDatastore *datastore = existingDatastore; NSString *indexName = existingIndexName; // Delete the index BOOL success = [datastore deleteIndexNamed:indexName]; if(!success){ // Handle error }
// A store that has been previously created. let datastore:CDTDatastore = existingDatastore let indexName:String = existingIndexName // Delete the index let success:Bool = datastore.deleteIndexNamed(indexName) if(!success){ // Handle error }
// Use an existing store Datastore datastore = existingStore; String indexName = existingIndexName; IndexManager indexManager = existingIndexManager; // Delete the index indexManager.deleteIndexNamed(indexName);
Parent topic: Migrating apps storing mobile data in Cloudant with IMFData or Cloudant SDK