Hi, I am developing an application that requires a synchronization operation (sending and receiving data) on a web server.
The user can submit forms offline (i.e. store data on a local db on the device). And whenever a network is available, the background service should send this data to the web server.
The background service detail requirement is similar to:
- The background service will first check if the network is available or not.
- If the network is available, it will collect the data store in local db (SQLite) on the device
- It sends data to the server
- request any new data from the server, and if available, get this data and update the local database on the device.
I am new to iOS and xamarin / monotouch and would like to know how to do this?
I know about various background modes in iOS, such as background selection, nsurlsession, background transfer, etc.
I am trying to implement Background Fetch, which, in my opinion, is suitable for my situation. but he works on his own time.
I would also like to know that if a user killed my application, then also background fetch is called and still starts my application?
the code is similar to this in my appdelegate -> PerformFetch method:
if(networkService.IsNetworkAvailable()) { if(this.syncDataService.DownloadNewDataFromServer()) { Console.WriteLine("Data downloaded successfully from server.."); } if(this.syncDataService.UploadDataToServer()) { Console.WriteLine("Data submitted successfully to server..."); } completionHandler(UIBackgroundFetchResult.NewData); } else { completionHandler(UIBackgroundFetchResult.NoData); }
Update: Finally, I implemented it this way (hope this can be useful for someone):
public class LocationUpdatedEventArgs : EventArgs { private CLLocation location; public LocationUpdatedEventArgs(CLLocation location) { this.location = location; } public CLLocation Location { get { return this.location; } } } public class LocationManager { private static DateTime lastServiceRun; private CLLocationManager locMgr; public LocationManager() { this.locMgr = new CLLocationManager(); this.LocationUpdated += this.PrintLocation; this.locMgr.Failed += (object sender, NSErrorEventArgs e) => { Console.WriteLine("didFailWithError " + e.Error); Console.WriteLine("didFailWithError coe " + e.Error.Code); }; } public event EventHandler<LocationUpdatedEventArgs> LocationUpdated = delegate { }; public static TimeSpan TimeDiff { get; set; } public CLLocationManager LocMgr { get { return this.locMgr; } } public void StartLocationUpdates() { if (CLLocationManager.LocationServicesEnabled) {