Using SmartStore in Swift Apps
You can easily install the basic plumbing for SmartStore in a forceios native Swift project.
In this example, you create a SmartStore soup and upsert the queried list of contact names into that soup. You then change the Swift template app flow to populate the table view from the soup instead of directly from the REST response. If you’re not familiar with Xcode project structure, consult the Xcode Help.
-
Using forceios, create a native Swift project similar to the following example:
-
In your project’s root directory, create a
userstore.jsonfile with the following content. -
Open your app's
.xcworkspacefile in Xcode. -
Add your configuration file to your project.
- In the Xcode Project navigator, select the project node.
- In the Editor window, select Build Phases.
- Expand Copy Bundle Resources.
- Click + (”Add items”).
- Select your soup configuration file. If your file is not already in an Xcode project folder:
- To select your file in Finder, click Add Other....
- Click Open, then click Finish.
-
In your project’s source code folder, select
Classes/AppDelegate.swift. -
In the
application(_:didFinishLaunchingWithOptions:)callback method, loaduserstore.jsondefinitions in the call toAuthHelper.loginIfRequired.Your app is now set up to load your SmartStore configuration file at startup. This action creates the soups you specified as empty tables. Let's configure the
RootViewControllerclass to use SmartStore. -
In
RootViewController.swift, importSmartStore: -
At the top of the
RootViewControllerclass, declare a variable for aSmartStoreinstance. -
On the next line, declare a constant that defines an
OSLogcomponent. -
In the
loadView()method, find the call to.queryand add theIdfield to the SOQL statement. -
In the
handleSuccess(_:_:)method, immediately after theguardblock, add the following code.This code checks whether the Contact soup exists. If the soup exists, the code clears all data from the soup, and then upserts the retrieved records.
-
Launch the app, then check your work using the Dev Tools menu.
-
To bring up the menu, type
control + command + zif you’re using the iOS emulator, or shake your iOS device. -
Click Inspect SmartStore.
-
To list your Contact soup and number of records, click Soups.
If you get a "Query: No soups found" message, chances are you have an error in your
userstore.jsonfile.
-
-
After the
handleSuccess(_:_:)method, add a method namedloadFromStore(). -
In
loadFromStore(), define anifblock that builds a Smart SQLquery specification as its first condition. Configure the query to extract the first 10 Name values from the Contact soup. -
Add a second condition that verifies the SmartStore handle and a third condition that runs the SmartStore query. Since the
querymethod throws an exception, call it from ado...try...catchblock. -
Transfer the names returned by the SmartStore query to the view’s
dataRowsmember . -
Using the
DispatchQueuesystem object, switch to the main thread and refresh the view’s displayed data. -
Scroll back to the
handleSuccess(_:_:)method and remove the existing code that reloads the view’s data. -
Using
self, call your newloadFromStore()method immediately after theupsert(entries:forSoupNamed:)call.
When you retest your app, you see that the table view is populated as before, but from SmartStore rather than a live REST response. In the real world, you'd create an editing interface for the Contact list, and then upsert your customers' edits to SmartStore. The customer could then continue working on the Contact list even if the mobile device lost connectivity. When connectivity is restored, you could then merge the customer’s work to the server—and also resync SmartStore—using Mobile Sync.