Skip to content

Instantly share code, notes, and snippets.

@Geertvdc
Last active May 17, 2016 13:37
Show Gist options
  • Save Geertvdc/8c9230058e782f6a276a948900f00652 to your computer and use it in GitHub Desktop.
Save Geertvdc/8c9230058e782f6a276a948900f00652 to your computer and use it in GitHub Desktop.
//Realm
_realm.Write(() =>
{
for (int i = 0; i < 1000; i++)
{
var order = _realm.CreateObject<RealmOrder>();
order.OrderNumber = $"Order{i}";
order.Price = i;
order.Title = Title = $"title {i}";
for (int j = 0; j < 5; j++)
{
var orderLine = _realm.CreateObject<RealmOrderLine>();
orderLine.Order = order;
orderLine.Amount = j;
orderLine.UnitPrice = j;
orderLine.Product = $"Product {i}{j}";
order.OrderLines.Add(orderLine);
}
}
});
//Sqlite
List<SqlOrder> orders = new List<SqlOrder>();
for (int i = 0; i < 1000; i++)
{
SqlOrder order = new SqlOrder() { OrderNumber = $"Order{i}",
Price = i,
Title = $"title {i}"};
order.OrderLines = new List<SqlOrderLine>();
for (int j = 0; j < 5; j++)
{
order.OrderLines.Add(new SqlOrderLine() { Amount = j,
Product = $"Product {i}{j}",
UnitPrice = j});
}
orders.Add(order);
}
//inserting.. 2 options:
//Option 1: slow easy way
//await sql.RunInTransactionAsync((SQLiteConnection conn) =>
//{
// conn.InsertAllWithChildren(orders);
//});
//Option 2: faster way with bit more code
await sql.RunInTransactionAsync((SQLiteConnection conn) =>
{
conn.InsertAll(orders);
// map order id's to orderlines for FK
var lines = orders.SelectMany(o =>
{
foreach (SqlOrderLine l in o.OrderLines)
{
l.OrderId = o.Id;
l.Order = o;
}
return o.OrderLines;
});
conn.InsertAll(lines);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment