Registering a Soup through Code
To register a soup, you provide a soup name and a list of one or more index specifications. If, for some reason, you can't use a configuration file to define your soup structures, here's how you can do the same thing through code. Each of the following code examples builds an index spec array consisting of name, ID, and owner (or parent) ID fields.
You index a soup on one or more fields found in its entries. SmartStore makes sure that these indexes reflect any insert, update, and delete operations. Always specify at least one index field when registering a soup. For example, if you are using the soup as a simple key-value store, use a single index specification with a string type.
Hybrid Apps
The JavaScript function for registering a soup requires callback functions for success and error conditions.
1var sfSmartstore = function() {
2 return cordova.require("com.salesforce.plugin.smartstore");
3};
4sfSmartstore().registerSoup(soupName, indexSpecs, successCallback, errorCallback);If the soup does not exist, this function creates it. If the soup exists, registering lets you access it.
- indexSpecs
-
Use the indexSpecs array to create the soup with predefined indexing. Entries in the indexSpecs array specify how to index the soup. Each entry consists of a path:type pair. path is the name of an index field; type is either “string”, “integer”, “floating”, “full_text”, or “json1”.
1var indexSpecs = [ 2 {path:"Name",type:"string"}, 3 {path:"Id",type:"string"} 4]; - successCallback
-
The success callback function you supply takes one argument: the soup name. For example:
1function(soupName) { alert("Soup " + soupName + " was successfully created"); }When the soup is successfully created, registerSoup() calls the success callback function to indicate that the soup is ready. Wait to complete the transaction and receive the callback before you begin any activity. If you register a soup under the passed name, the success callback function returns the soup.
- errorCallback
-
The error callback function takes one argument: the error description string.
1function(err) { alert ("registerSoup failed with error: " + err); }
1navigator.smartstore.soupExists(soupName, successCallback, errorCallback);Android Native Apps
- string
- integer
- floating
- full_text
- json1
1public class OfflineStorage extends SalesforceActivity {
2 private SmartStore smartStore;
3 final IndexSpec[] ACCOUNTS_INDEX_SPEC = {
4 new IndexSpec("Name", SmartStore.Type.string),
5 new IndexSpec("Id", SmartStore.Type.string),
6 new IndexSpec("OwnerId", SmartStore.Type.string)
7 };
8
9 public OfflineStorage() {
10 smartStore = SmartStoreSDKManager.getInstance().getSmartStore();
11 smartStore.registerSoup("Account", ACCOUNTS_INDEX_SPEC);
12 }
13
14 // ...
15}iOS Native Apps
- kSoupIndexTypeString
- kSoupIndexTypeInteger
- kSoupIndexTypeFloating
- kSoupIndexTypeFullText
- kSoupIndexTypeJSON1
- Swift
-
In Mobile SDK 8.0 and later, a SmartStore native Swift extension provides the following soup registration method:
1func createAccountsSoup(name: String) { 2 guard let user = UserAccountManager.shared.currentUserAccount, 3 let store = SmartStore.shared(withName: SmartStore.defaultStoreName, 4 forUserAccount: user), 5 let index1 = SoupIndex(path: "Name", indexType: "String", 6 columnName: "Name"), 7 let index2 = SoupIndex(path: "Id", indexType: "String", 8 columnName: "Id") 9 else { 10 return 11 } 12 13 do { 14 try store.registerSoup(withName: name, withIndices:[index1,index2]) 15 } catch (let error) { 16 SalesforceLogger.d(RootViewController.self, 17 message:"Couldn’t create soup \(name). 18 Error: \(error.localizedDescription)") 19 return 20 } 21} - Objective-C
-
1NSString* const kAccountSoupName = @"Account"; 2 3... 4- (SFSmartStore *)store 5{ 6 return [SFSmartStore sharedStoreWithName:kDefaultSmartStoreName]; 7} 8 9... 10- (void)createAccountsSoup { 11 if (![self.store soupExists:kAccountSoupName]) { 12 NSArray *keys = @[@"path", @"type"]; 13 NSArray *nameValues = @[@"Name", kSoupIndexTypeString]; 14 NSDictionary *nameDictionary = [NSDictionary 15 dictionaryWithObjects:nameValues forKeys:keys]; 16 17 NSArray *idValues = @[@"Id", kSoupIndexTypeString]; 18 NSDictionary *idDictionary = 19 [NSDictionary dictionaryWithObjects:idValues forKeys:keys]; 20 21 NSArray *ownerIdValues = @[@"OwnerId", kSoupIndexTypeString]; 22 NSDictionary *ownerIdDictionary = 23 [NSDictionary dictionaryWithObjects:ownerIdValues 24 forKeys:keys]; 25 26 NSArray *accountIndexSpecs = 27 [SFSoupIndex asArraySoupIndexes:@[nameDictionary, 28 idDictionary, ownerIdDictionary]]; 29 30 NSError* error = nil; 31 [self.store registerSoup:kAccountSoupName 32 withIndexSpecs:accountIndexSpecs 33 error:&error]; 34 if (error) { 35 NSLog(@"Cannot create SmartStore soup '%@'\nError: '%@'", 36 kAccountSoupName, error.localizedDescription); 37 } 38 } 39}