Before you try to access a soup, you’re required to register it.
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.
As of Mobile SDK 6.0, you can register soups in native apps through a JSON configuration file. Where possible, we recommend this strategy over coding. See Registering Soups with Configuration Files.
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”.
Index paths are case-sensitive and can include compound paths, such as Owner.Name.
Index entries that are missing any fields described in an indexSpecs array are not tracked in that index.
The type of the index applies only to the index. When you query an indexed field (for example, “select {soup:path} from {soup}”), the query returns data of the type that you specified in the index specification.
Index columns can contain null fields.
Beginning in Mobile SDK 4.1, you can specify index paths that point to internal (non-leaf) nodes. You can use these paths with like and match (full-text) queries. Use the string type when you define internal node paths.
For example, consider this element in a soup named “spies”:
In this case, “address” is an internal node because it has children. Through the index on the path “address”, you can use a like or match query to find the “city” value—“london”—in “address”. For example:
Beginning in Mobile SDK 4.1, you can include arrays in index paths, with some restrictions. See Using Arrays in Index Paths.
Still running into issues? See “Creating Index Specs” in the Mobile SDK Development Guide for advanced rules and restrictions.
Note
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); }
During soup creation, errors can happen for various reasons, including:
For Android, you define index specs in an array of type com.salesforce.androidsdk.smartstore.store.IndexSpec. Each index spec comprises a path—the name of an index field—and a type. Index spec types are defined in the SmartStore.Type enum and include the following values:
string
integer
floating
full_text
json1
1public class OfflineStorage extends SalesforceActivity {2 private smartStore;3 final IndexSpec[] ACCOUNTS_INDEX_SPEC = {4 new IndexSpec("Name", .Type.string),5 new IndexSpec("Id", .Type.string),6 new IndexSpec("OwnerId", .Type.string)7 };89 public OfflineStorage() {10 smartStore = SmartStoreSDKManager.getInstance().getSmartStore();11 smartStore.registerSoup("Account", ACCOUNTS_INDEX_SPEC);12 }1314 // ...15}
iOS Native Apps
For iOS, you define index specs in an array of SFSoupIndex objects. Each index spec comprises a path—the name of an index field—and a type. Index spec types are defined as constants in the SFSoupIndex class and include the following values:
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 return11}1213 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 return20}21}