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 Insert(TRealmObject item) { using (var transaction = _currentRealm.BeginWrite()) { try { _currentRealm.Add(item); transaction.Commit(); } catch (Exception) { transaction.Rollback(); } } }
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 async Task <T> AddAsync <T>(T item, ITransaction transaction) where T : RealmObject { T?result = default; Realms.Realm realm = await GetRealmAsync(); await this.RunInTransactionAsync(_ => { result = realm.Add(item); }, transaction); return(result !); }
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()); }
public async Task UpdateItemAsync <T>(string id, Func <T, Task> updateAction, ITransaction transaction) where T : RealmObject { Realms.Realm realm = await GetRealmAsync(); await this.RunInTransactionAsync(async _ => { T?item = null; var newItem = _domainServiceLocator.Resolve <T>(); if (!string.IsNullOrWhiteSpace(id)) { item = realm.Find <T>(id); } if (item != null) { await updateAction(item); } else { await updateAction(newItem); realm.Add(newItem); } }, transaction); }
/// <summary> /// Inserts the specified item into the realm object to have realm start treating this a persistent object. /// </summary> /// <param name="item">The item.</param> /// <returns>The object as a realm object.</returns> public TRealmObject Insert(TRealmObject item) { return(_currentRealm.Add(item)); }