Example #1
0
        public void Insert(TRealmObject item)                            // 0
        {
            var listaItem = _currentRealm.All <TRealmObject>().ToList(); // 1

            var contadorId = 0;                                          // 2

            if (listaItem.Count != 0)                                    // 3
            {
                contadorId = listaItem.Max(r => r.ID);
            }

            item.ID = contadorId + 1; // 4

            _currentRealm.Write(() => // 5
                                _currentRealm.Add(item));

            /*
             * 0 - Objeto genérico que receberá o objeto do banco local.
             * 1 - Lista de itens armazenada no banco local.
             * 2 - Variável contador de IDs iniciada com zero.
             * 3 - Se lista de itens NÃO for zero, então retorna o maior
             *     valor da lista da propriedade ID e salva na variável contador
             * 4 - ID recebe o valor do contado + 1.
             * 5 - O objeto é salvo no banco local.
             */
        }
        public void AddOrUpdate <T>(IEnumerable <T> items) where T : RealmObject
        {
            if (items.Count() == 0)
            {
                return;
            }

            ConfigureInstance();
            _realm.Write(() =>
            {
                foreach (var item in items)
                {
                    _realm.Add <T>(item, true);
                }
            });
        }
        public App(params INinjectModule[] patformModules)
        {
            InitializeComponent();
            var mainPage = new NavigationPage(new MainPage());

            // setup and get an instance to our current Realm
            CurrentRealm = Realms.Realm.GetInstance(new RealmConfiguration
            {
                SchemaVersion     = RealmConfigure.SchemaVersion,
                MigrationCallback = RealmConfigure.MigrationCallback,
            });

            CurrentRealm.Write(() =>
            {
                CurrentRealm.RemoveAll();
            });

            // Register all the our core services with the kernal
            Kernal = new StandardKernel(new CoreModule(), new NavigationModule(mainPage.Navigation));
            // Register all of our platform specific modules with the kernal
            Kernal.Load(patformModules);

            mainPage.BindingContext = Kernal.Get <MainViewModel>();
            MainPage = mainPage;
        }
Example #4
0
        /// <summary>
        /// Writes the specified action.
        /// This preforms a realm transaction.
        /// If an exception occurs during the write then the transaction is disposed of and is never commited.
        /// </summary>
        /// <param name="writeAction">The action to preform to write.</param>
        /// <exception cref="System.ArgumentNullException">writeAction</exception>
        public void Write([NotNull] Action writeAction)
        {
            if (writeAction == null)
            {
                throw new ArgumentNullException(nameof(writeAction));
            }

            _currentRealm.Write(writeAction);
        }
Example #5
0
 internal IEnumerable <BookInfo> GetBookInfo()
 {
     realm.Write(() =>
     {
         for (int i = 0; i < BookNames.Count(); i++)
         {
             var book = new BookInfo()
             {
                 BookName        = BookNames[i],
                 BookDescription = BookDescriptions[i],
                 BookAuthor      = BookAuthers[i],
             };
             realm.Add(book);
         }
     });
     return(realm.All <BookInfo>().AsRealmCollection());
 }