Example #1
0
        protected override async void InitializeCore()
        {
            User user = null;

            try
            {
                user = User.Current;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }

            if (user == null)
            {
                try
                {
                    user = await NavigationService.Prompt <LoginViewModel, User>();
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
            else
            {
                var uri = user.ServerUri;
                Constants.Server.SyncHost = $"{uri.Host}:{uri.Port}";
            }

            try
            {
                var config = new SyncConfiguration(user, Constants.Server.SyncServerUri)
                {
                    ObjectClasses = new[] { typeof(Task), typeof(TaskList), typeof(TaskListList) }
                };

                _realm = Realm.GetInstance(config);

                TaskListList parent = null;
                _realm.Write(() =>
                {
                    // Eagerly acquire write-lock to ensure we don't get into
                    // race conditions with sync writing data in the background
                    parent = _realm.Find <TaskListList>(0);
                    if (parent == null)
                    {
                        parent = _realm.Add(new TaskListList());
                        parent.Items.Add(new TaskList
                        {
                            Id    = Constants.DefaultListId,
                            Title = Constants.DefaultListName
                        });
                    }
                });

                TaskLists = parent.Items;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
Example #2
0
        protected override async void InitializeCore()
        {
            User user = null;

            try
            {
                user = User.Current;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }

            if (user == null)
            {
                try
                {
                    user = await NavigationService.Prompt <LoginViewModel, User>();
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
            else
            {
                var uri = user.ServerUri;
                Constants.Server.SyncHost = $"{uri.Host}:{uri.Port}";
            }

            try
            {
                var config = new SyncConfiguration(user, Constants.Server.SyncServerUri)
                {
                    ObjectClasses = new[] { typeof(Task), typeof(TaskList), typeof(TaskListList) }
                };

                _realm = Realm.GetInstance(config);

                var parent = _realm.Find <TaskListList>(0);
                if (parent == null)
                {
                    try
                    {
                        _realm.Write(() =>
                        {
                            parent = new TaskListList();
                            parent.Items.Add(new TaskList
                            {
                                Id    = Constants.DefaultListId,
                                Title = Constants.DefaultListName
                            });

                            _realm.Add(parent);
                        });
                    }
                    catch (RealmDuplicatePrimaryKeyValueException)
                    {
                        // If sync went through too fast, we might already have that one.
                        // We don't care though, since we only use it as container.
                        parent = _realm.Find <TaskListList>(0);
                    }
                }

                TaskLists = parent.Items;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }