예제 #1
0
        public void AddAnObject_WhenRealmIsDifferentInstanceOnSameThread_ShouldSucceed()
        {
            var firstObject = new IntPrimaryKeyWithValueObject();

            _realm.Write(() => _realm.Add(firstObject));

            using (var realm2 = Realm.GetInstance(_realm.Config))
            {
                Assert.That(firstObject.IsManaged);
                Assert.That(realm2.IsSameInstance(firstObject.Realm));
                realm2.Write(() => realm2.Add(firstObject));
            }
        }
예제 #2
0
        public void RefreshAsync_Tests()
        {
            AsyncContext.Run(async() =>
            {
                Assert.That(SynchronizationContext.Current != null);

                IntPrimaryKeyWithValueObject obj = null;
                _realm.Write(() =>
                {
                    obj = _realm.Add(new IntPrimaryKeyWithValueObject());
                });

                var reference = ThreadSafeReference.Create(obj);

                Task.Run(() =>
                {
                    using (var realm = Realm.GetInstance(_realm.Config))
                    {
                        var bgObj = realm.ResolveReference(reference);
                        realm.Write(() =>
                        {
                            bgObj.StringValue = "123";
                        });
                    }
                }).Wait(); // <- wait to avoid the main thread autoupdating while idle

                Assert.That(obj.StringValue, Is.Null);

                var changeTiming = await measureTiming(_realm.RefreshAsync);

                Assert.That(obj.StringValue, Is.EqualTo("123"));

                // Make sure when there are no changes RefreshAsync completes quickly
                var idleTiming = await measureTiming(_realm.RefreshAsync);

                Assert.That(changeTiming, Is.GreaterThan(idleTiming));

                async Task <long> measureTiming(Func <Task> func)
                {
                    var sw = new Stopwatch();
                    sw.Start();
                    await func();
                    sw.Stop();
                    return(sw.ElapsedTicks);
                }
            });
        }
예제 #3
0
        public void ModifiedIndices_ReportCorrectlyForOldAndNewVersions()
        {
            ChangeSet changes = null;

            void cb(IRealmCollection <IntPrimaryKeyWithValueObject> s, ChangeSet c, Exception e) => changes = c;

            var toDelete = new IntPrimaryKeyWithValueObject {
                Id = 1
            };
            var toModify = new IntPrimaryKeyWithValueObject {
                Id = 2
            };

            _realm.Write(() =>
            {
                _realm.Add(toDelete);
                _realm.Add(toModify);
            });

            var query = _realm.All <IntPrimaryKeyWithValueObject>().OrderBy(i => i.Id);

            using (query.SubscribeForNotifications(cb))
            {
                Assert.That(query.ElementAt(0).Equals(toDelete));
                Assert.That(query.ElementAt(1).Equals(toModify));

                _realm.Write(() =>
                {
                    _realm.Remove(toDelete);
                    toModify.StringValue = "newValue";
                });

                _realm.Refresh();
                Assert.That(changes, Is.Not.Null);
                Assert.That(changes.DeletedIndices, Is.EquivalentTo(new int[] { 0 }));

                // Modified should be in the old collection
                Assert.That(changes.ModifiedIndices, Is.EquivalentTo(new int[] { 1 }));

                // NewModified should be in the new collection that is just 1 element
                Assert.That(changes.NewModifiedIndices, Is.EquivalentTo(new int[] { 0 }));
                Assert.That(query.ElementAt(changes.NewModifiedIndices[0]).Equals(toModify));
            }
        }
예제 #4
0
        public void AsyncWrite_UpdateViaPrimaryKey()
        {
            AsyncContext.Run(async() =>
            {
                IntPrimaryKeyWithValueObject obj = null;
                _realm.Write(() =>
                {
                    obj = _realm.Add(new IntPrimaryKeyWithValueObject {
                        Id = 123
                    });
                });

                await _realm.WriteAsync(realm =>
                {
                    var dataObj         = realm.Find <IntPrimaryKeyWithValueObject>(123);
                    dataObj.StringValue = "foobar";
                });

                // Make sure the changes are immediately visible on the caller thread
                Assert.That(obj.StringValue, Is.EqualTo("foobar"));
            });
        }