Search code examples
phpiosobjective-cnsfilemanager

iOS and PHP: Check if server has updates/ database has changes


Background

I have an iOS app that retrieves data from a server and writes it to a file with NSFileManager, so that the user does not have to retrieve data again from the server every time they want to visit a page. The data that they are retrieving can change from time to time, so I would like to update the data in the app files when it does.

I am using php pages for my backend web services.

Question

What is the technique for checking if the data in my database has changed so that I know that I should update the data in the app? The only way I can think to do this is to create a php page that checks if the data is different from what I have stored, but this kind of defeats the purpose of saving information to files within the app. I want to minimize when the user communicates with the server, because in situations where the user has no service, I would still like there to be valid data that they can see within the app.

Please ask questions if you do not fully understand what I am trying to achieve. Thank you!


Solution

  • If you want to list the most recently added restaurants for example, you could :

    • Store the ID of the most recent restaurant on IOS and send it to the server when you want to check for updates.
      • If the ID isn't the same in the DB, then you have to update your client.
      • If it's the same, you can just send a response with a 304 status (Not modified) so you minimise the amount of data being exchange.

    If you want to cache computed data, like top 10 user-rated restaurants, you can :

    • Store a hash of the 10 restaurant IDs and send it to server when you want to check for updates.
    • The server redo the request and compute the current hash for the top 10.
      • If the hash is different, update your client
      • If the hash is the same, send a response with a 304 status

    NB : If the computation is expensive on server side, you might want to think about a more clever way to detect data changes.