The mobilesync.js library implements a cache named StoreCache that stores its data in SmartStore. Although Mobile Sync uses StoreCache as its default cache, StoreCache is a stand-alone component. Even if you don’t use Mobile Sync, you can still leverage StoreCache for SmartStore operations.
Although StoreCache is intended for use with Mobile Sync, you can use any cache mechanism with Mobile Sync that meets the requirements described in Offline Caching.
Note
Construction and Initialization
StoreCache objects work internally with SmartStore soups. To create a StoreCache object backed by the soup soupName, use the following constructor:
Required. The name of the underlying SmartStore soup.
additionalIndexSpecs
Fields to include in the cache index in addition to default index fields. See Registering a Soup for formatting instructions.
keyField
Name of field containing the record ID. If not specified, StoreCache expects to find the ID in a field named "Id."
Soup items in a StoreCache object include four additional boolean fields for tracking offline edits:
__locally_created__
__locally_updated__
__locally_deleted__
__local__ (set to true if any of the previous three are true)
These fields are for internal use but can also be used by apps. If your app uses the Mobile Sync plugin to sync up to the server, you’re probably required to create these fields in the source soup. See Preparing Soups for Mobile Sync for instructions.
StoreCache indexes each soup on the__local__ field and its ID field. You can use the additionalIndexSpecs parameter to specify additional fields to include in the index.
To register the underlying soup, call init() on the StoreCache object. This function returns a jQuery promise that resolves once soup registration is complete.
StoreCache Methods
init()
Registers the underlying SmartStore soup. Returns a jQuery promise that resolves when soup registration is complete.
retrieve(key [, fieldlist])
Returns a jQuery promise that resolves to the record with key in the keyField returned by the SmartStore. The promise resolves to null when no record is found or when the found record does not include all the fields in the fieldlist parameter.
key
The key value of the record to be retrieved.
fieldlist
(Optional) A JavaScript array of required fields. For example:
1["field1","field2","field3"]
save(record [, noMerge])
Returns a jQuery promise that resolves to the saved record once the SmartStore upsert completes. If noMerge is not specified or is false, the passed record is merged with the server record with the same key, if one exists.
The following example shows how to create, initialize, and use a StoreCache object.
1var cache = new Force.StoreCache("agents", [{path:"Mission", type:"string"} ]);2// initialization of the cache / underlying soup3cache.init()4.then(function() {5 // saving a record to the cache6 return cache.save({Id:"007", Name:"JamesBond", Mission:"TopSecret"});7})8.then(function(savedRecord) {9 // retrieving a record from the cache10 return cache.retrieve("007");11})12.then(function(retrievedRecord) {13 // searching for records in the cache14 return cache.find({queryType:"like", indexPath:"Mission", likeKey:"Top%", order:"ascending", pageSize:1});15})16.then(function(result) {17 // removing a record from the cache18 return cache.remove("007");19});
The next example shows how to use the saveAll() function and the results of the find() function.