How to handle persistence on iOS app that allows users update objects from a web app (Rest-API Backend)

How to handle persistence if my app needs to constantly making GET/POST requests to backend REST-API based web service?

I have a list of cars related to a specific user, that list could be modified from the web app or the iOS app(like adding new car, deleting it from the list or update the image of existing car).

Right now I’m using URLSession for handle the network layer, and FileManager to make data persistent on disk.

Problems I have encountered so far are:

The only way the iOS app to notice any changes on the “Data Source/Backend” is by the user triggering the viewWillAppear in Main Screen/VC that has a table view containing all Cars related to said user.

Everytime viewWillAppear gets triggered a new GET request starts and decode the received response. If there is data received and no errors at all it ALWAYS tries to save it to the .caches directory and then pass the data to the ViewController vars.

But if there is an error on the response. Then the view loads it’s data from the disk.

The process is too slow because is constantly using data and disk resources.

But what I can’t figure out is, how should abstract data persistence from networking and save/update/delete the received data in another thread or moment while the app is running(or not?).

And if the user makes a new change to the car list or specific car how the persistence logic should be approached?

From updates (crUd) right now I’m only making a POST request to the backend and deleting the related file. If the user tries to list all cars again another GET request start and the process is repeated

Hi @vinodkumark,
a couple of questions for you and a few suggestions,

  1. You can detail why you need to get the entire data, say you add a new car or add a service on a particular car, you will have to first upload that data and then download it in the screen.
  2. You can have a call that loads only bits instead of the entire data depending on what has changed.
  3. If you had the same running from a database, you would still be running it off the device. So persistence will use a combination of both on-device and remote.

How about load data from the cached data (on-disk) and send a request (GET) and when you get the data, update the cached data and also the UI. This way though it will take a bit of time, the user will not see the difference.

cheers,

Jayant

This topic was automatically closed after 166 days. New replies are no longer allowed.