【发布时间】:2016-12-07 12:24:05
【问题描述】:
我正在 Xamarin 中学习 Realm。
我正在尝试使用Thread 插入一些示例数据。我没有遇到任何问题,直到我在多个线程中调用相同的函数。领域文档说要在 executeTransactionAsync 中执行插入操作,但我在 Realm-Xamarin 中看不到任何类似的方法。
这里是代码。
Thread thread1 = new Thread(() => TestThread.CountTo10("Thread 1"));
thread1.Start();
Thread thread2 = new Thread(() => TestThread.CountTo10("Thread 2"));
thread2.Start();
线程类:
public class TestThread
{
public static Realm realm;
public static void CountTo10(string _threadName)
{
realm = Realm.GetInstance();
for (int i = 0; i < 5; i++)
{
realm.Write(() =>
{
RandomNumber random = new RandomNumber();
System.Console.WriteLine("Iteration: " + i.ToString() + " Random No: " + random.number.ToString() + " from " + _threadName);
realm.Manage(random);
});
Thread.Sleep(500);
}
}
}
领域对象:
public class RandomNumber : RealmObject
{
public int number { get; set; }
public RandomNumber()
{
number = (new Random()).Next();
}
}
【问题讨论】:
-
xamarin 文档:realm.io/docs/xamarin/latest
标签: c# multithreading xamarin realm