Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 
Product and Topic Expert
Product and Topic Expert
0 Kudos
Previous (Online OData)   Home   Next (SAP Fiori for iOS)

A mobile application may not always be used in areas where a network connection is present or if it is, it may have high latency. The Offline OData framework enables the application to work against a local database instead of making online requests to read or update data.

The following are some additional sources of documentation on using proxy classes to access OData while offline.
SAPOfflineOData Reference
Developing Offline Applications
Implementing Offline OData in your Application
Handle Error Scenarios with Offline OData (Tutorial)
Offline OData in iOS Native Mobile Interactive Tutorials

The following steps demonstrate how to enable offline reads without network connectivity.

  1. Add the below import to the top of ViewController.swift.
    import SAPOfflineOData

  2. Add the following method.
        func getProductsOffline(_ serviceRoot: URL, _ urlSession: SAPURLSession) {
    var storeParams: OfflineODataParameters = OfflineODataParameters()
    storeParams.storeName = "ESPMOfflineDB"
    do {
    let oDataProvider = try OfflineODataProvider(serviceRoot: serviceRoot, parameters: storeParams, sapURLSession: urlSession)
    let espmContainer = ESPMContainer(provider: oDataProvider)
    let productsDQ = OfflineODataDefiningQuery(name: "ProductsQuery", query: "Products", automaticallyRetrievesStreams: false)
    try oDataProvider.add(definingQuery: productsDQ) { ( _ error: OfflineODataError? ) -> Void in
    if (error == nil) {
    print("Store successfully opened")
    espmContainer.fetchProducts() { products, error in
    guard let products = products else {
    self.logger.error("Error fetching products: \(error!.localizedDescription)")
    print("Offline: got \(products.count) products and the first product name is \(products[0].name!)")
    //try! oDataProvider.close()
    } else {
    self.logger.error("Store open failed")
    } )
    catch {

  3. In the buttonPressed method, call the newly added getProductsOffline method after the call to getProducts.
    getProductsOffline(serviceURL, myContext.sapURLSession)

  4. Run the example with a network connection. The first time the store is opened, a database will be created on the server and downloaded to the device.The expected output is shown below.
    Button pressed 1 times
    Currently connected by WiFi
    Got 123 products and the first product name is Surround Sound
    Store successfully opened
    Offline: got 123 products and the first product name is Gaming Monster Pro

  5. Turn off Wi-Fi on your Mac if running on a simulator or turn on airplane mode if on a device and press the button again. The expected output is shown below. Notice that the online request fails while the offline request succeeds.
    Button pressed 1 times
    Currently offline with no network connection
    2017-11-11 14:31:21.046636-0500 GettingStarted[48051:2665101] TIC TCP Conn Failed [2:0x608000362280]: 1:50 Err(50)
    2017-11-11 14:31:21.047412-0500 GettingStarted[48051:2665101] Task FB2FEB2D-4415-4830-B894-B1027975BDA1. 2 HTTP load failed (error code: -1009 [1:50])
    2017-11-11 14:31:21.047595-0500 GettingStarted[48051:2666620] Task FB2FEB2D-4415-4830-B894-B1027975BDA1. 2 finished with error - code: -1009
    Error fetching products: Optional(HTTPError: The Internet connection appears to be offline.)
    Store successfully opened
    Offline: 123 products and the first product name is Gaming Monster Pro

Previous (Online OData)   Home   Next (SAP Fiori for iOS)