My application has both offline (SQLite) and online (iCloud) storage. See: Offline First: How Do I Fetch Data?

Now, as stated in my past question above, the application is offline first, and I'm currently following the answer in that question.

How I fetch data: In first run (or rather in every appear of the screen), fetch the online data, and sync it with the local data.

My problem is how to properly generate IDs for both local and online storages.

In the local storage, I do auto-increment (Int) for the index IDs.

Here's what I'm thinking:

  1. When adding a new data, add it to the local storage (in case when internet connection gets off), and then let the (above) event do the syncing thing, see: How I fetch data.

  2. When adding a new data, add it to local, and then fetch the latest ID from the local storage, and increment it by 1 and use it as the iCloudID, like so:

    iCloudID = localStorageLastID + 1

    When saving a new data to SQLite, I can't get the ID (Autoincrement) of the latest local data. So I do a new query for this.

  3. I think this last idea is the best, but will take few more hours sigh:

    Make a new column to the local database for a customId say UUID (String). And make one too for the online storage. UUID are generated using a String class on iOS.

    UUID's description:

    Returns a string created from the UUID, such as “E621E1F8-C36C-495A-93FC-0C247A3E6E5F”

Also, if someone could help me with the proper tags for this question, that would be awesome.

  • My problem is how to properly generate IDs for both local and online storages. Use GUIDS. Sep 25, 2019 at 18:13
  • 1
    Basically use UUID / proceed with #3, right?
    – Glenn
    Sep 25, 2019 at 18:17
  • 1
    Of course, that's the only way to handle online and offline IDs equally well. Sep 25, 2019 at 18:43
  • Okay. I got it. Thanks a lot! Perhaps you would consider posting this as an answer.
    – Glenn
    Sep 26, 2019 at 3:33


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.