Using the Sync Manager
The sync manager object handles simple sync up and sync down operations. For sync down, it sends authenticated requests to the server on your behalf, and stores response data locally in SmartStore. For sync up, it collects the records you specify from SmartStore and merges them with corresponding server records according to your instructions. Sync managers know how to handle authentication for Salesforce users and community users. Sync managers can store records in any user or global SmartStore instance—the default instance, or a named instance.
Once you've created an instance, you can use it to call typical sync manager functionality:
- Sync down
- Sync up
- Resync
- Stop
- Restart
Sync managers can perform three types of actions on SmartStore soup entries and Salesforce records:
- Create
- Update
- Delete
If you provide custom targets, sync managers can use them to synchronize data at arbitrary REST endpoints.
Sync Manager States
- accepting_syncs—The sync manager can start sync operations.
- stopping—The sync manager’s stop method has been called.
- stopped—All sync operations have stopped.
Sync Manager Instantiation (iOS)
| Swift Class | Objective-C Class |
|---|---|
| SyncManager | SFMobileSyncSyncManager |
In iOS, you use pairs of access and removal methods. You call the sharedInstance class methods on the SFMobileSyncSyncManager class to access a preconfigured shared instance for each scenario. When you're finished using the shared instance for a particular use case, remove it with the corresponding removeSharedInstance method.
- For a specified user:
-
- Swift
-
1SFMobileSyncSyncManager.sharedInstance(user: userAccount) 2SFMobileSyncSyncManager.removeSharedInstance(user: userAccount) - Objective-C
-
1+ (instancetype)sharedInstance:(SFUserAccount *)user; 2+ (void)removeSharedInstance:(SFUserAccount *)user;
- For a specified user using the specified SmartStore database:
-
- Swift
-
1SFMobileSyncSyncManager.sharedInstance(forUser: userAccount, storeName: "StoreName") 2SFMobileSyncSyncManager.removeSharedInstance(forUser: userAccount, storeName: "StoreName") - Objective-C
-
1+ (instancetype)sharedInstanceForUser:(SFUserAccount *)user 2 storeName:(NSString *)storeName; 3 4+ (void)removeSharedInstanceForUser:(SFUserAccount *)user 5 storeName:(NSString *)storeName;
- For the current user and a specified SmartStore database:
-
- Swift
-
1SFMobileSyncSyncManager.sharedInstance(for: store) 2SFMobileSyncSyncManager.removeSharedInstance(for: store)
- Objective-C
-
1+ (instancetype)sharedInstanceForStore:(SFSmartStore *)store; 2+ (void)removeSharedInstanceForStore:(SFSmartStore *)store;
Sync Manager Instantiation (Android)
In Android, you use a different factory method for each of the following scenarios:
- For the current user:
-
1public static synchronized SyncManager getInstance(); - For a specified user:
-
1public static synchronized SyncManager 2getInstance(UserAccount account); - For a specified user in a specified community:
-
1public static synchronized SyncManager 2getInstance(UserAccount account, String communityId); - For a specified user in a specified community using the specified SmartStore database:
-
1public static synchronized SyncManager 2getInstance(UserAccount account, String communityId, SmartStore smartStore);
Example
1store = SFSmartStore.sharedStore(withName: kDefaultSmartStoreName) as? SFSmartStore
2syncManager = SFMobileSyncSyncManager.sharedInstance(for:store!)