How to limit number of requests to a database

Viewed 205

I have an app with a settings page where the settings of each user are stored in a MySQL database. I was wondering what is the best way to update the database for every setting the user changes while sending the minimal number of requests as I'm worried that it will crash if it sends too many( it has happened before).

I was thinking about setting a timer for ~5 seconds when the user first changes a setting, and then reset the timer to 5 seconds again if another setting is changed. Once that timer is finished it will send a request to the server to update all the settings at once. It would also constantly store the new values locally to the app, so if the user closes the app before the 5 seconds are up it will send the request once/if the app loads up again.

Is this viable/what's the best way to go about this?

2 Answers

You need to make some logic functions in your app, so i will try make an pseudo codes below. Hope it will give you an idea. I don`t know the MySQL details but i am trying to explain native Swift way.

First of all you should fetch data partly, I mean if you try to fetch all data at the same time your app can work very slow.. That is why we are doing pagination in the app.

As well as pagination you want to refresh the data fresh 5 seconds so i will use Timer object, which will trigger every 5 seconds the API function and catch data based on pagination. Check your below codes and implement step by step to your project. Happy Coding.

var timer: Timer?

func scheduledTimerWithTimeInterval(){
    // Scheduling timer to Call the function "loadNewDataAutomatically" with the interval of 5 seconds
    timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.loadNewDataAutomatically), userInfo: nil, repeats: true)
}

@objc func loadNewDataAutomatically(_ pageNumber: Int, _ pageSize: Int, onSuccess: ((Bool) -> Void)?, onError: ((Error) -> Void)?){

    // Call your api here
    // Send true in onSuccess in case new data exists, sending false will disable pagination
    
    // If page number is first, reset the list
    if pageNumber == 1 { self.list = [YourDataModel]() }
    
    // else append the data to list
    self.list.append(apiResponseList)
    
    // If Api responds with error
    onError?(apiError)

    // Else end success with flag true if more data available
    let moreDataAvailable = !apiResponseList.isEmpty
    onSuccess?(moreDataAvailable)

}

Assuming that the Database (MySQL) is on a server
You can try using WorkManager for this requirement.

  1. When the user changes their settings, save them locally (which you are already doing)

  2. enqueue a Unique Periodic Work Request using WorkManager & set up the time at what interval should the response be sent to the server.
    Minimum time interval is around 15 min. but not guaranteed at exactly 15 minutes,
    the system fires it when it seems fit, also according to the Constraints that you set on your Work Request.

    You can also use a OneTimeWorkRequest, if you don't need periodic, with an Initial Delay of whatever time you need.


Edit: This question was later edited and ios, swift tags were added where previously it was for android. If anyone comes here searching for something similar for Android, this would work.

Related