0

I have some offline Data saved in my CoreData and I want to send those data into the Backend via a service call, without visiting the viewController like Auto Sync in Gmail while there is an internet connection available. Struggling from last week please provide me any solution for this. Advance Thanks.

srsstyle
  • 99
  • 1
  • 9
  • Look at: https://stackoverflow.com/a/49571819/4757272 and you can detect when the user comes online. Then you will just trigger some event on wifi / celluar cases – Vollan Aug 06 '19 at 11:12
  • I know how to Check network availability via Reachability, But my Question was not only detecting its something about sending the stored data from my local DB into the backend without visiting the ViewController. And there are multiple data saved in my DB.@Vollan – srsstyle Aug 06 '19 at 11:21
  • Then i don't understand your question. Because if you have the listener in a `non-ViewController` and you know how to fetch data from CoreData. You just fetch the data and send it to your server. – Vollan Aug 06 '19 at 11:29
  • @Vollan For the timing, let's say I want to send it to backend when my app is opening in my "HomeVc", but only if there are some data available in my Core DB and internet connection is available as well, the data was previously filled up from the "OfflineVC" during no internet connectivity into my Core DB. I hope you understand this time. – srsstyle Aug 06 '19 at 12:21

1 Answers1

0
  1. Import Reachability.swift file from https://github.com/ashleymills/Reachability.swift/archive/master.zip in your XCode project

  2. Create a new Swift class

class ConnectionManager {

static let sharedInstance = ConnectionManager()
private var reachability : Reachability!
var state: Reachability.Connection = .wifi

func observeReachability(){
    NotificationCenter.default.addObserver(self, selector:#selector(self.reachabilityChanged), name: NSNotification.Name.reachabilityChanged, object: nil)
    do {
        self.reachability = try Reachability()
        try self.reachability.startNotifier()
    }
    catch(let error) {
        print("Error occured while starting reachability notifications : \(error.localizedDescription)")
    }
}

@objc func reachabilityChanged(note: Notification) {
    let reachability = note.object as! Reachability
    ConnectionManager.sharedInstance.state = reachability.connection
    switch reachability.connection {
    case .cellular:
        state = reachability.connection
        isOffline = false
        print("Network available via Cellular Data.")
        break
    case .wifi:
        isOffline = false
        print("Network available via WiFi.")
        break
    case .none, .unavailable:
        isOffline = true
        print("Network is not available.")
        break
    }

}

}

That for some reason wont be full code...

Add:

var isOffline: Bool = false {
        didSet {
            if !isOffline {
                gotBackOnline()
            }
        }
    }

Add:

private func gotBackOnline() {

    // Check if there are any offline data in CoreData and upload to server.   
}
  1. Call upon ConnectionManager.sharedInstance.observeReachability() whenever you want to begin watching the network status. In one of my apps i have it in the viewDidLoad of VC after login as i need to user to be logged in. But you can call it from AppDelegate or anything.

Chunks of the answer taken from here

Vollan
  • 1,887
  • 11
  • 26
  • Thanks for this resolution, but I need to work on only `gotBackOnline()` as I have already checked when the internet is available via reachability and real-time network detection also. My problem is I do not want to show the user which data is there in offline rather the data need to be sent automatically when there is internet availability that means auto fetching from the CORE DB and sending into the Backend, handling the error if any. And there are a lot of data available in my core DB which I need to sent individually for each row-wise.Anyway thanks for your time. – srsstyle Aug 07 '19 at 05:46