首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OnStart方法中调用时,Akavache或Sqlite崩溃

在OnStart方法中调用时,Akavache或Sqlite崩溃
EN

Stack Overflow用户
提问于 2017-11-13 01:26:28
回答 1查看 832关注 0票数 1

当我在xamarin.forms项目中使用下面的代码检查用户对象是否存在于akavache缓存中时,我将得到下面的异常。相同的代码或任何akavache查询都在其他地方工作,但只在onStart方法中崩溃。我相信我已经在构造函数中初始化akavache了。我尝试使用移动中心( mobile )查询本地(本地sqlite)用户数据,并得到了相同的异常。我认为这应该与sqlite有关,因为akavache和移动中心都使用类似的sqlite库。有人知道为什么它在OnStart方法中不起作用吗?

代码语言:javascript
复制
 public App()
        {
            Microsoft.Azure.Mobile.MobileCenter.Start("android=key" +
                   "uwp=key",
                   typeof(Microsoft.Azure.Mobile.Analytics.Analytics), typeof(Microsoft.Azure.Mobile.Crashes.Crashes));

            Akavache.BlobCache.ApplicationName = "myApp";
            Akavache.BlobCache.EnsureInitialized();

            ServiceLocator.Add<ICloudService, AzureCloudService>();

            InitializeComponent();

            }

  protected async override void OnStart()
        { 

            try
            {
               var User= await BlobCache.UserAccount.GetObject<User>("User");

                if (User != null)
                    Helpers.Settings.IsLoggedIn = true;
                else
                    Helpers.Settings.IsLoggedIn = false;
            }
            catch (Exception)
            {
                Helpers.Settings.IsLoggedIn = false;
            }
           ShowMainPageOrLoginPage();
        }

11-13 02:08:14.761 E/MobileCenterCrashes( 6308):未处理异常:11-1302:08:14.761 E/MobileCenterCrashes( 6308):System.NullReferenceException:对象引用未设置为对象实例。11-13 02:08:14.761 E/MobileCenterCrashes( 6308):at Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page页面,System.Int32宽度,D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:291 11-13 02:08:14.761 E/MobileCenterCrashes( 6308):at Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (System.Boolean changed,System.Int32 l,System.Int32 t,System.Int32 r,D:\agent_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:199 11-13 02:08:14.761 E/MobileCenterCrashes( 6308):at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (System.Boolean changed,System.Int32 l,System.Int32 t,System.Int32 r,D:\agent_work\1\s\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:73 11-13 02:08:14.761 E/MobileCenterCrashes( 6308):at Android.Views.ViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv,System.IntPtr native__this,System.Boolean changed,System.Int32 l,System.Int32 t,System.Int32 r,System.Int32 b) 0x00008 in :0

编辑:这个问题肯定是由akavache引起的。信息真的很奇怪。看起来,akavache和LayoutRootPage有某种关系。

参见上面的代码,我从akavache缓存中获得了用户对象,并且用户对象定义了我是应该显示登录页还是主页。如果我将ShowMainPageOrLoginPage();函数移动到akavache调用之上,它就能正常工作。因此,在设置或加载rootlayoutpage -主页之前,您似乎无法使用akavache进行任何查询。

EN

回答 1

Stack Overflow用户

发布于 2017-11-13 03:58:45

我以前也遇到过同样的问题,由于某种原因,如果您使用静态方法进行初始化,它不会一直工作。

不管用

代码语言:javascript
复制
IBlobCache _cache = BlobCache.LocalMachine;

行得通

代码语言:javascript
复制
IBlobCache _cache = new SQLitePersistentBlobCache(systemPath + "/CacheUtils.db");

如果你想找到systemPath,你可以在你的安卓或者iOS中使用它。

代码语言:javascript
复制
systemPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47255888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档