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.json
file with the following content. -
Open your app's
.xcworkspace
file 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.json
definitions 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
RootViewController
class to use SmartStore. -
In
RootViewController.swift
, importSmartStore
: -
At the top of the
RootViewController
class, declare a variable for aSmartStore
instance. -
On the next line, declare a constant that defines an
OSLog
component. -
In the
loadView()
method, find the call to.query
and add theId
field to the SOQL statement. -
In the
handleSuccess(_:_:)
method, immediately after theguard
block, 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 + z
if 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.json
file.
-
-
After the
handleSuccess(_:_:)
method, add a method namedloadFromStore()
. -
In
loadFromStore()
, define anif
block 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
query
method throws an exception, call it from ado...try...catch
block. -
Transfer the names returned by the SmartStore query to the view’s
dataRows
member . -
Using the
DispatchQueue
system 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.