LiveQuery data getting mixed up on bulk adding of Documents


#1

I have an Android app developed in C# with Xamarin. The App is essentially a Contact book. There is a REST API service that downloads contacts and adds them as documents into the database. The user can also create Groups of contacts on their device and mark a contact as a Favourite. To represent these 4 types of data each document has a key called ‘DocumentTypeKey’. These types can be Contact, Favourite, Group or GroupMember.

I then have 4 views that that hold the Contact, Favourites, Groups and Group Members. I can query these views fine on demand and the correct data is returned. However, the problems start they are converted into LiveQueries (so I can trigger events back in the UI). As all the Contacts download and insert themselves into the Database the Changed EventHandler seems to fire on all 4 LiveQueries even though only Contact data is being inserted into the database. What I essentially get Contact records inside the Group Live Query, even though the filter to seperate them is correct. The issue is random does not always occur.

What is even more strange is that a couple of seconds after all the Documents have been added to the database then everything seems to have settled down and the LiveQueries now contain the correct data. It is almost as if there is some period of sorting needs to happen. However, the Changed EventHandler seems to get called during this period,

The issue is not present when single records are added and managed, only when a lot of records get added at once and only in LiveQueries.

Is there some sort of time lapse between documents being added and the LiveQuery reporting back?

see this code:

private void SetupGroupIdsQuery()
{
IEnumerable emptyGroups = Enumerable.Empty();
_groupIdsSubject = new BehaviorSubject<IEnumerable>(emptyGroups);

		_scheduler.Schedule(() => {
			_groupIdsView = SetupGroudIdsView();
			_groupMembershipIdsView = SetupGroupMembershipIdsView();

			var query = _groupIdsView.CreateQuery();

			var rows = query.Run();
			var allGroupIds = from row in rows
				select row.DocumentId;
			_groupIdsSubject.OnNext(allGroupIds);

			_groupIdsQuery = _groupIdsView.CreateQuery().ToLiveQuery();
			_groupIdsQuery.Changed += (object sender, QueryChangeEventArgs e) => {
				var updatedIds = (from row in e.Rows
				select row.DocumentId).ToList();

				_groupIdsSubject.OnNext(updatedIds);
			};
			_groupIdsQuery.Start();
		});

	}

In _groupIdsQuery.Changed I will sometimes get non Group data, even though the Filtering is correct.

Any advice on how to fix this?