Getting All from Realm Hangs or Crashes App with No Exception


#1

I have some code that was previously working but now either hangs indefinitely or crashes the application without raising an exception. The model used is;

using Realms;
using System;

namespace RealmSx.Models
{
	public class ServiceResponse : RealmObject
	{
		[Required]
		[PrimaryKey]
		[MapTo("id")]
		public string Id { get; set; }

		[Required]
		[MapTo("serviceId")]
		public string ServiceId { get; set; }

		[Required]
		[MapTo("jobId")]
		public string JobId { get; set; }

		[Required]
		[MapTo("runId")]
		public string RunId { get; set; }

		[Required]
		[MapTo("driverId")]
		public string DriverId { get; set; }

		[MapTo("serviced")]
		public int? Serviced { get; set; }

		[MapTo("contaminated")]
		public int? Contaminated { get; set; }

		[MapTo("weight")]
		public float? Weight { get; set; }

		[MapTo("completed")]
		public bool Completed { get; set; }

		[MapTo("reasonId")]
		public int? ReasonId { get; set; }

		[MapTo("reason")]
		public string Reason { get; set; }

		[MapTo("driverNote")]
		public string DriverNote { get; set; }

		[MapTo("image")]
		public byte[] Image { get; set; }

		[MapTo("responseDtTm")]
		public DateTimeOffset ResponseDtTm { get; set; } = DateTimeOffset.Now;

		[MapTo("processedDtTm")]
		public DateTimeOffset? ProcessedDtTm { get; set; }
	}
}

My service is;

using System.Linq;
namespace RealmSx
{
	public interface IServices<T> where T : class
	{
		T Add(T model);
		void Remove(T model);
		IQueryable<T> GetAll { get; }
		T GetById(string id);
	}
}

using Realms;
using RealmSx.Models;
using System;
using System.Diagnostics;
using System.Linq;

namespace RealmSx
{
	public class ServiceResponseService : IServices<ServiceResponse>
	{
		private readonly Realm realm;
		public ServiceResponseService(Realm realm)
		{
			this.realm = realm ?? throw new ArgumentNullException(nameof(realm));
		}
		public ServiceResponse GetLastByServiceId(string serviceId)
		{
			try
			{
				var results = realm.All<ServiceResponse>();
				results = results.Where(x => x.ServiceId == serviceId);
				results = results.OrderByDescending(x => x.ResponseDtTm);
				return results.FirstOrDefault();

				//result = realm.All<ServiceResponse>()
				//	.Where(x => x.ServiceId == serviceId)
				//	.OrderByDescending(x => x.ResponseDtTm)
				//	.FirstOrDefault();
				//return result;
			}
			catch(Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return null;
			}
		}
	}
}

The method GetLastByServiceId was originally working with the commented code. I modified the code to enable me to step through to try and determine where the error was occurring. When I step through the first line var results = realm.All<ServiceResponse>(); my application either hangs indefinitely or crashes without hitting the catch block.

The following similar service executes successfully as do several other services;

using Realms;
using RealmSx.Models;
using System;
using System.Diagnostics;
using System.Linq;

namespace RealmSx
{
	public class RunServices : IServices<Run>
	{
		private readonly Realm realm;
		public RunServices(Realm realm)
		{
			this.realm = realm ?? throw new ArgumentNullException(nameof(realm));
		}
		public IQueryable<Run> GetAll
		{
			get
			{
				var results = realm.All<Run>();
				return results;
			}
		}
	}
}

I have tried deleting all of the Realm files on the ROS and resetting my emulator to factory defaults in case there was a schema change that was causing the problem. I have also tried this on the emulator and on a Samsung phone but both have the same problem.

I then tried updating to Realm v4.0.1 but there were numerous breaking changes so I reverted to v3.4.0.

Any suggestions on what I can try next would be greatly appreciated.


#2

I have done some more work on this and found that if I pass a different Realm object to the service it then runs successfully;

namespace SrxRunSheet.Views
{
	[XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class RunSheet : ContentPage
	{
		private Realm realm;
		private Realm srRealm;
		private readonly User user = null;
		private readonly string userName;

		public RunSheet(User user, string userName)
		{
			InitializeComponent();
			this.user = user;
			this.userName = userName;
			BindingContext = this;
		}

		private void OpenRealm()
		{
			var realmFile = "~/RunSheet";
			var config = ConnectionServices.GetRealmConfiguration(realmFile, user);
			realm = ConnectionServices.ConnectToSyncServer(config);
			srRealm = ConnectionServices.ConnectToSyncServer(config);
		}

		private RealmSx.Models.Driver GetDriver(Realm realm, string driverName)
		{
			var driverService = new DriverServices(realm);
			var driver = driverService.GetByName(driverName.ToLower());
			return driver;
		}

		private async void OnStartJob(object sender, EventArgs e)
		{
			var button = sender as Button;
			var job = button.CommandParameter as ViewModels.JobVm;
			//jobPage = new JobPage(realm, job);
			jobPage = new JobPage(srRealm, job);
			jobPage.JobFinished += HandleJobFinished;
			await Navigation.PushModalAsync(jobPage);
		}
	}
}

Previously I was using the realm object and passing this around in my code. If I also create the srRealm object and use this for the JobPage the code works. If I pass the realm object it doesn’t?


#3

Do you think you can isolate this behavior in a small repro case?