Taking Your App Offline With the Salesforce Mobile SDK | by John Vester | May, 2022

Learn on for a straightforward approach to lower your app’s wire

Final yr, my spouse and I gained the first-time expertise of constructing a brand-new house. The method was enjoyable and thrilling, however we additionally skilled the surprising web service interruptions that usually accompany new house subdivisions.

Whereas these outages impacted my household’s capacity to stream providers, similar to Amazon Prime, Hulu, and Netflix, I continued engaged on my present venture as a result of I designed it to work in an remoted (or offline) state. This has at all times been useful throughout airline journey once I work from Nevada and Florida throughout the yr.

This made me marvel why extra cell apps are usually not designed to proceed working throughout web service interruptions.

On this publication, I’ll reveal simply how simple it’s to make your cell app usable in offline mode. Since I lately hung out with the Salesforce Mobile SDK, I needed to proceed exploring that cell app choice.

Earlier than we get into writing code, let’s briefly contact on how offline performance works with the Salesforce Cellular SDK.

Conventional Salesforce Utilization

The left aspect of the illustration presents the normal method by which the Salesforce utility is utilized. The appropriate aspect offers a high-level overview of the Salesforce Cellular SDK.

Native purposes that run on Android or iOS gadgets could be written in Android Studio, Xcode, or React Native. These purposes embody the consumer interface, some degree of enterprise logic, and mannequin objects representing components that in the end reside in Salesforce.

The Salesforce Cellular SDK features a SmartStore facet, which makes information accessible when the cell utility can’t entry the Salesforce service. The mannequin layer can simply be designed to leverage the SmartStore when offline mode is required (or most popular), and it additionally retains it able to synchronize all modifications with Salesforce when the web standing is on the market.

If you wish to learn extra about this design, take a look at “Using SmartStore to Securely Store Offline Data.”

Earlier this yr, I explored the Salesforce Cellular SDK and created a fictional app referred to as Finny’s Meals. I created the app 3 times utilizing Android, iOS, and React Native. I assumed I might begin from the publication that used the iOS/Xcode (Swift) model of the Salesforce Cellular SDK, then construct upon it to supply offline performance.

The GitLab repository for the unique article is listed under:

https://gitlab.com/johnjvester/finnys-foods-ios

Step one is to determine the Meal__c object within the userstore.json file that’s utilized by the SmartStore performance.

Subsequent, replace the usersyncs.json file to make the present checklist of meals accessible offline:

You may learn all in regards to the SmartStore sync configuration in “Registering Soups with Configuration Files.”

With the SmartStore configuration in place, I refactored the MealsListModel.swift class as proven under:

I up to date the fetchMeals() perform to work together with the SmartStore by default, calling Salesforce when wanted however permitting information saved within the utility for use if the system is in offline mode.

Imagine it or not, that’s all that’s required.

After launching Finny’s Meals from Xcode and an iPhone 8 emulator, we see the next display screen displayed because it did within the authentic publication.

Now, if I disconnect my system from the web and reload the appliance, the next logs seem in Xcode:

2022-03-14 16:14:15.544412-0400 FinnysFoodsIOS[88679:6546397] [MobileSync] CLASS: SFSyncDownTask runSync failed: ...  trigger:Server name for sync down failed errorError Area=NSURLErrorDomain Code=-1009 "The Web connection seems to be offline."

Nonetheless, the appliance nonetheless hundreds and offers the info from the SmartStore. Actually, with out trying on the logs in Xcode, I might have had no concept that I used to be working with an area model of the info.

For offline purposes that enable information to be added or up to date, the SmartStore has the flexibility to merge offline and on-line information with a Sync Supervisor. See the Syncing Data documentation for added data.

Though web connectivity continues to enhance, a dependable connection isn’t 100% assured. Ahead-thinking characteristic builders ought to think about taking the suitable steps to permit their apps to proceed working when an web connection isn’t accessible. Ask your self: How can my app be higher when working in a disconnected state?

Since 2021, I’ve been making an attempt to reside by the next mission assertion, which I really feel can apply to any IT skilled:

“Focus your time on delivering options/performance which extends the worth of your mental property. Leverage frameworks, merchandise, and providers for all the things else.”
— J. Vester

On this article, I offered an instance of how simple it’s to implement offline performance utilizing the Salesforce Cellular SDK. Salesforce engineering designed the combination level on the mannequin layer, and that’s the very best place to find out if reside information could be retrieved or if SmartStore information ought to be returned.

Clearly, Salesforce adheres to my private mission assertion by providing a design that’s simple to make use of with out quite a lot of boilerplate code.

In case you are within the supply code for this text, yow will discover it on GitLab right here:

https://gitlab.com/johnjvester/finnys-foods-ios

Have a extremely nice day!

More Posts