6

I am using firebase in an iOS-Swift project in which I have to enable offline support for uploading posts, in the post there is a picture and caption just like Instagram, so what I want is when user is offline and he/she wants to upload a post, his/her picture get saved in cache and when user comes online that photo get uploaded and give back a download url that we can use for saving posts-details it in database.

sample code is:

                let photoIDString = UUID().uuidString
                let storageRef = Storage.storage().reference(forURL: "storage ref URL").child("posts").child(photoIDString)

                storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in

                guard let metadata = metadata else {
                    return
                }
                if error != nil {
                    return
                }
                storageRef.downloadURL(completion: { ( url, error ) in
                    guard let downloadURL = url else {
                        return
                    }
                    let photoUrl = downloadURL.absoluteString
                    self.sendDataToDatabase(photoUrl: photoUrl)
                    })
                }
                )

I want to know what changes should I make in my code to provide the offline capability. Any code snippet will help more.

  • Welcome to StackOverflow! Please share the code you have written so far so we can help you! – DAA Oct 08 '18 at 07:07

3 Answers3

0

The problem is better view as re-send to server when there is an error. For your offline case, you can check if the error return is a network error, or manually check network connection availability.

You can create a re-send array of object e.g

var resendList : [YourObjectType]

// when failed to send to server
resendList.append(yourFailedObject)

And then, 2 solutions:

  1. Check the network connectivity manually and reupload in when the app become active in func applicationDidBecomeActive(_ application: UIApplication) in appDelegate. For checking connectivity you can try the method here: Check for internet connection with Swift But this has a problem that, the user has to go out the app and back again with network connected

  2. Keep track(listen to notification) on the connectivity change, using a suggestion method by https://stackoverflow.com/a/27310748/4919289 and reupload it to server

and loop through all objects in resendList and re-upload again.

Edison Lo
  • 476
  • 8
  • 25
0

I am not an iOS developer, but I can share logical flow and some references.

When user clicks on upload: Check if network is available?

  • if yes: upload the post.
  • if no:
    • save the post to app storage or offline database
    • set broadcast receiver to receive broadcast when device comes online. This link may be helpful.
    • upload post when device comes online.

If you are looking for solution that is offered by Firebase, you may find more details here.

Pang
  • 9,564
  • 146
  • 81
  • 122
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
0

Firebase offers you plenty of ways to do this in their documentation. https://firebase.google.com/docs/database/ios/offline-capabilities

When uploading to the firebase server, it will queue itself and wait until it has a internet connection again to upload. If this happens to timeout or you want to do it your own way just attempt to upload with a completionHandler on the setValue or updateChild functions - if not successfully and the error message is because of internet, add it to a local cache to the phone with the data and the path to the firebase server.

onLoad, attempt the same upload again until it succeeds, once it succeeds - clear the local cache.

Torewin
  • 887
  • 8
  • 22