Creating and Accessing User-based Stores
When an app initializes SmartStore, it creates an instance of a
store. It then uses the store to register and populate soups and manipulate soup data. For a
user-based store, SmartStore manages the
store’s life cycle—you don’t need to think about cleaning up after the user’s session ends.
For global stores, though, your app is responsible for deleting the store’s data when the app
terminates.
Android Native Apps
Android requires you to first get an instance of SmartStoreSDKManager which you then use to create
stores.
A
call to SmartStoreSDKManager.getSmartStore() without arguments always accesses the default anonymous store. To
create a named user-based store, call the following
method.
Both
account and communityId can be null. You can call these methods as many times as
necessary to create additional stores.
1SmartStoreSDKManager sdkManager =
2 SmartStoreSDKManager.getInstance();
3
4SmartStore smartStore = sdkManager.getSmartStore(); // Creates a default store for the current user1public SmartStore getSmartStore(String dbNamePrefix, UserAccount account, String communityId)iOS Native Apps
For creating stores, iOS provides the sharedStoreWithName: class
message.
In
Swift:
You
can create a store with a custom name by passing in any string other than kDefaultSmartStoreName. You can call this method as many times as necessary to create
additional stores.
1- (SFSmartStore *)store
2{
3 return [SFSmartStore sharedStoreWithName:kDefaultSmartStoreName]; // Creates a default store for the current user
4}1var store = SmartStore.shared(withName: storeName)Hybrid Apps
In hybrid apps, you access user-based stores and global stores the same way. Rather than
creating stores explicitly, you automatically create stores that don’t already exist when
you call registerSoup(). To use a named
store—for subsequent direct references, for example—you call this function
with a StoreConfig object as the first argument. This
function object takes a store name and a Boolean value that indicates whether the store is
global.
You
can pass this object as the optional first argument to most soup functions. If used, the
StoreConfig object configures the execution context.
Either storeName or isGlobalStore can be optional—you can specify one or both. SmartStore evaluates StoreConfig objects as follows:
1var StoreConfig = function (storeName, isGlobalStore) {
2 this.storeName = storeName;
3 this.isGlobalStore = isGlobalStore;
4};- If storeName is not specified, this.storeName is set to the SmartStore default store name.
- If isGlobalStore is not specified, this.isGlobalStore is set to false.
- Store names aren’t necessarily unique. A single store name can be used twice—once for a user-based store, and once for a global store.
- If you provide a store name that doesn’t exist in the space indicated by your isGlobalStore setting, SmartStore creates it.
The following example creates a user-based store named “Store1” that contains the soupName
soup.
1navigator.smartstore.registerSoup({storeName: "Store1", isGlobalStore:false}, soupName,
2 indexSpecs, successCallback, errorCallback)You can call registerSoup() with as many different
soup names as necessary. If you call a soup function without passing in StoreConfig, SmartStore always performs the
operation on the default user-based (non-global) store. This behavior applies even if you’ve
created named stores. The following example creates a soup named soupName, with the provided index specs, in the current user’s default
store.
1var sfSmartstore = function() {
2 return cordova.require("com.salesforce.plugin.smartstore");
3};
4sfSmartstore().registerSoup(soupName, indexSpecs, successCallback, errorCallback);