示例#1
0
        public async Task <bool> Sync()
        {
            //var changed = profiles.Where(p => p.LastUpdate >= LastSync || (p.Deleted != null && p.Deleted >= LastSync));

            bool   ret = false;
            string url = ConstantHolder.AllProfileQuestions;
            List <ProfileQuestion> profileQuestions = null;

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var response = await client.GetAsync(url);

                    if (response.IsSuccessStatusCode)
                    {
                        var json = await response.Content.ReadAsStringAsync();

                        ret = true;

                        try
                        {
                            profileQuestions = await Task.Run(() =>
                                                              JsonConvert.DeserializeObject <List <ProfileQuestion> >(json));
                        }
                        catch (Exception e)
                        {
                            ret = false;
                        }


                        if (profileQuestions != null)
                        {
                            //drop and recreate database
                            ProfileData.Instance.CreateDatabase();

                            using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DatabaseLocation))
                            {
                                conn.InsertOrReplaceAllWithChildren(profileQuestions, recursive: true);
                            }

                            ////fresh data to memory
                            await Task.Factory.StartNew(() => ProfileData.Instance.SqliteToMemory());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                e.GetBaseException();
            }

            return(ret);
        }