Sof*_*San 8 c# background xamarin.ios ios xamarin
嗨,我正在开发一个App,它要求在Web服务器上进行同步操作(数据提交和检索).
用户可以离线提交表单(即将数据存储到设备上的本地数据库).只要网络可用,后台服务就应该将这些数据提交给Web服务器.
后台服务的详细要求如下:
我是iOS和xamarin/monotouch的新手,想知道如何实现这个目标吗?
我了解iOS中的各种背景模式,例如背景提取,nsurlsession,后台传输等.
我尝试实现后台提取,我认为适合我的情况.但它依靠自己的时间运行.
还想知道如果用户已杀死我的应用程序,那么后台提取也将调用并仍然运行我的应用程序?
我的appdelegate - > PerformFetch方法中的代码是这样的:
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);
}
Run Code Online (Sandbox Code Playgroud)
更新:最后我已经像这样实现了它(希望它对某些人有帮助):
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)
{
// sets the accuracy that we want in meters
this.LocMgr.DesiredAccuracy = 1;
//// Location updates are handled differently pre-iOS 6. If we want to support older versions of iOS,
//// we want to do perform this check and let our LocationManager know how to handle location updates.
if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0))
{
this.LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
{
//// fire our custom Location Updated event
this.LocationUpdated(this, new LocationUpdatedEventArgs(e.Locations[e.Locations.Length - 1]));
};
}
else
{
//// this won't be called on iOS 6 (deprecated). We will get a warning here when we build.
this.LocMgr.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) =>
{
this.LocationUpdated(this, new LocationUpdatedEventArgs(e.NewLocation));
};
}
//// Start our location updates
this.LocMgr.StartUpdatingLocation();
lastServiceRun = DateTime.Now;
// Get some output from our manager in case of failure
this.LocMgr.Failed += (object sender, NSErrorEventArgs e) =>
{
Console.WriteLine(e.Error);
};
}
else
{
//// Let the user know that they need to enable LocationServices
Console.WriteLine("Location services not enabled, please enable this in your Settings");
}
}
/// <summary>
/// The stop updating location.
/// </summary>
public void StopUpdatingLocation()
{
this.locMgr.StopUpdatingLocation();
}
/// <summary>
/// The print location. (This will keep going in the background)
/// </summary>
/// <param name="sender"> The sender. </param>
/// <param name="e"> Location updated event argument </param>
public void PrintLocation(object sender, LocationUpdatedEventArgs e)
{
CLLocation location = e.Location;
Console.WriteLine("Longitude: " + location.Coordinate.Longitude);
Console.WriteLine("Latitude: " + location.Coordinate.Latitude);
var diff = DateTime.Now - lastServiceRun;
TimeDiff = diff;
if (TimeDiff.Minutes == 2)
{
// RunInBackground() is my method which call the service to upload/download data from server
if (this.RunInBackground())
{
lastServiceRun = DateTime.Now;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
计时器永远不会在后台工作,并且您不能在此方法中添加另一个线程。DidEnterBackground用于完成待处理的任务。这通常会持续大约 30 秒 - 1 分钟。
您可以在此方法中添加循环或添加长时间运行的任务。
| 归档时间: |
|
| 查看次数: |
10056 次 |
| 最近记录: |