Xamarin.forms Xamarin forms AbstractThreadedSyncAdapter关闭,无错误或异常
我们有一个Xamarin表单应用程序,它在后台API调用期间关闭。我没有在这个项目上写太多的同步代码,但我只能对它进行故障排除。下面是代码所做工作的摘要:Xamarin.forms Xamarin forms AbstractThreadedSyncAdapter关闭,无错误或异常,xamarin.forms,Xamarin.forms,我们有一个Xamarin表单应用程序,它在后台API调用期间关闭。我没有在这个项目上写太多的同步代码,但我只能对它进行故障排除。下面是代码所做工作的摘要: api调用发出并成功返回 json字符串被正确地序列化为C#对象列表 对象被传递给一个方法,该方法将对象映射到一个本地类,该类只包含我们在应用程序上关心的属性 在此映射之后,我们将这些对象插入到SQLLite数据库 在第3阶段的某个时候,当循环浏览对象列表并映射它们时,应用程序只是关闭,没有抛出任何类型的异常。我尝试对对象进行排序,以查看是否
the framework will attempt to determine whether or not an adapter is making
progress by monitoring its network activity over the course of a minute. If
the network traffic over this window is close enough to zero the sync will
be cancelled.
我的怀疑是,在应用程序关闭的时候,sync早已完成了所有网络调用,但仍然在处理所有映射和数据库插入以及生成的数据。因此:
public class SyncAdapterImpl : AbstractThreadedSyncAdapter
{
public SyncAdapterImpl(Context context, bool autoInitialize)
: base(context, autoInitialize)
{
}
public override void OnSyncCanceled()
{
base.OnSyncCanceled();
Console.WriteLine("Canceled");
}
public override void OnPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)
{
IKernel kernel = new StandardKernel(new PlatformModule(), new CoreModule());
var syncService = kernel.Get<SyncService>();
SyncResultSummary syncResultSummary = new SyncResultSummary();
if (!extras.GetBoolean(ContentResolver.SyncExtrasUpload))
{
syncService.UpdateProfile().Wait();
var task = syncService.DownloadJobs();
task.Wait();
syncResultSummary = task.Result;
}
// send notifications with results
}
}
我的理论是,在SyncAdapter完成API调用后,它会生成任何网络流量,但仍然会留下大量处理和db写入。Xamarin没有看到任何网络流量,他说“嘿,同步完成了”,然后在进程仍在运行时终止了进程,整个应用程序都是在这个进程下运行的。当这种情况发生时,没有任何明确的警告,它只是发生了 因此,我所做的是为每个记录提取一个小对象,其中包含id和更新的时间,然后逐个循环(您无法并行执行,因为您会遇到相同的问题)-看起来解决方案是在您仍然需要工作的整个时间内保持一些网络活动
如果能够收回数据并将其传递给其他人可能会更好,但我必须进一步研究xamarin的工作方式,才能弄清楚我的理论是,在SyncAdapter完成API调用后,它完成了任何网络通信,但仍然留下了大量的处理和db写入。Xamarin没有看到任何网络流量,他说“嘿,同步完成了”,然后在进程仍在运行时终止了进程,整个应用程序都是在这个进程下运行的。当这种情况发生时,没有任何明确的警告,它只是发生了 因此,我所做的是为每个记录提取一个小对象,其中包含id和更新的时间,然后逐个循环(您无法并行执行,因为您会遇到相同的问题)-看起来解决方案是在您仍然需要工作的整个时间内保持一些网络活动
如果能够提取数据并将其传递给其他对象,可能会更好,但我必须进一步研究xamarin的工作方式,以了解您是否检查了应用程序输出/设备日志以获取任何崩溃信息?您确定这不是由未处理的异常引起的吗?我会开始注释代码,看看是什么引起的,然后从插入DB开始。您是否检查了应用程序输出/设备日志以获取任何崩溃信息?你确定这不是由未处理的异常引起的吗?我会开始注释代码,看看是什么引起的,然后从插入DB开始。
[Service(Label = "SyncAdapterService", Exported = true, Process = ":sync")]
[IntentFilter(new String[] { "android.content.SyncAdapter" })]
[MetaData("android.content.SyncAdapter", Resource = "@xml/syncadapter")]
public class SyncAdapterService : Service
{
private SyncAdapterImpl _syncAdapter;
private object _syncAdapterMutex = new object();
public override void OnCreate()
{
base.OnCreate();
lock(_syncAdapterMutex)
{
if (_syncAdapter == null)
_syncAdapter = new SyncAdapterImpl(this.ApplicationContext, true);
}
}
public override IBinder OnBind(Intent intent)
{
return _syncAdapter.SyncAdapterBinder;
}
}