0

I'm creating an app that allows the user to create a project, take photos and create labels in that project (using structs), then create a PDF later on. I've got to figure out how to save this data without connection, then backup it up to the server later. I want the app to be able to work both online and offline - whether the user has cell service or not. I've seen many apps where when you load them up, if you have no signal, you can select "Run Offline" or "Continue Offline."

How should I save the data locally until the user comes back into cellular signal and the data can be backed up to Firebase Firestore and Storage? I was considering using UserDefaults to save the data in a cache essentially, then whenever the phone is back within signal, the app automatically runs through this cache and backs everything up to the server. Is this the correct way to accomplish what I'm needing done? Is there a better way?

In the end, the data MUST be able to be saved whether connected to the internet or not - online or offline. I just need to figure out the best way to save the data until it can be uploaded to the server when back online. I want the data the user is creating to be safe in case something happened to the phone or to the app (maybe their phone shut off or there was a random app crash). By the way, the app is project based... so there's multiple files (projects) in other words. They may need to set up multiple files (projects) while offline and access one of them later on while still offline. It needs to put all of these offline projects in a container to be automatically backed up to the server upon returning to signal. These files (projects), with all the data in them, MUST be safe at all times until backed up - even if the user closes the app.

Please leave a detailed answer. I'm a beginner, as you can tell, and need specifics. If you reference a certain way of doing something, kindly leave a link to a tutorial or significant documentation, or explain it thoroughly.

Thanks!

Christian

Christian W
  • 353
  • 2
  • 12
  • You are correct that you'll need a system (especially in the case of Firebase Storage) to track whether an upload has started and try it again later if there is no connection. For Firebase Firestore, there's an offline persistence mode that will do most of the work for you (https://firebase.google.com/docs/firestore/manage-data/enable-offline). However, your questions about how to achieve this are pretty broad and don't have a definitive correct answer. – jnpdx Jan 22 '21 at 17:24
  • Most likely you'll need to store the data to disk (in the case of images, you'll want to be using files, not UserDefaults) and track somehow the state of the uploads (this could be UserDefaults or a database, etc). – jnpdx Jan 22 '21 at 17:26
  • What would you need to know specifically in order to help provide an answer? – Christian W Jan 22 '21 at 17:26
  • there is no 'correct' answer to this -- that's the problem. It's an app architecture decision of which there are many solutions. And, the code that will create solutions to these types of problems are not insignificant. StackOverflow generally works best when you try something, show your code, and then ask for help with a specific issue. I know it's hard to get started on a big problem like this, but try breaking it down into smaller tasks (like writing your images to disk) and starting there. – jnpdx Jan 22 '21 at 17:31
  • I see what you're saying. Really I'm just needing to be pointed in a general direction because I don't even know where to begin looking. I don't want to run with something that turns out to be the wrong, but I already implemented it into the entire app. Writing to the disk is a good idea and I'll start looking into that. As far as being more specific with the app goes, it allows the user to create a project, take photos and create labels in that project (using structs), then create a PDF later on. I just gotta figure out how to save this data without connection, then backup to server later. – Christian W Jan 22 '21 at 17:40
  • 2
    I suggest you write the storage layer as if you don't have Firebase at all. In other words, write it as if the only thing you have is the filesystem. Then, once that all works, write another layer (for lack of a better term) that handles uploading/syncing that data to Firebase – jnpdx Jan 22 '21 at 17:42
  • 1
    Firestore already does this by default. The offline persistent is enabled by default and everything you do offline will save locally, and when it detects it's online, it will sync the data automatically with the cloud. I suggest you watch the videos in the Firestore documentation along with reading the documentation, cause it explains all of this with code samples and even sample apps to download. – gmogames Jan 22 '21 at 22:55
  • As mentioned by @jnpdx and gmogames, firesbase firestore has [this functionality](https://firebase.google.com/docs/firestore/manage-data/enable-offline) by default. On iOS you only need to enable it on the AppDelegate as [mentioned here](https://stackoverflow.com/questions/24447085/how-to-persist-firebase-objects-to-disk-in-ios). – Louis C Feb 03 '21 at 01:42
  • 1
    @gmogames I think the OP is more asking about Storage than Firestore (?). OP, Firestore offers persistence built in. See Dougs answer [here](https://stackoverflow.com/questions/60997522/data-persistence-with-firebse-storage-like-firestore). However Storage does not offer persistence. A solution is to leverage the [Presence System](https://firebase.google.com/docs/database/ios/offline-capabilities#section-presence), part of the Real Time Database. It knows about online/offline so you can then property handle when to store images locally when offline, and then upload them when it's online. – Jay Feb 11 '21 at 20:44
  • @Jay you are correct, Storage does not have persistence and auto syncing, but I did think based on his description of working offline and saving data to send to server when online, that he was mainly looking at Firestore, but I should've been clearer on that. – gmogames Feb 12 '21 at 01:57

0 Answers0