Uploaded image for project: 'Realm .NET SDK'
  1. Realm .NET SDK
  2. RNET-64

Sorting by an interface property fails with KeyNotFoundException

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None

      Hello:

      This code was working on previous xamarin version , but now on Realm Xamarin 1.2 fails.

      Goals

      Execute a simple query like this:

      T obj = realm.All<T>().OrderByDescending(x => x.Id).FirstOrDefault();

      Expected Results

      Get an object from database. This code was working on realm 1.1.1

      <!--- What happened instead? -->

      The query produces an exception

      Error --> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
      at System.ThrowHelper.ThrowKeyNotFoundException () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.8.0.175/src/mono/mcs/class/referencesource/mscorlib/system/throwhelper.cs:68
      at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x00021] in ....

      Steps to Reproduce

      Create a simple class, and run the code.

      Code Sample

      public class Dog : RealmObject, IDocument
          {
              [PrimaryKey]
              public Int32 Id { get; set; }
              public string Name { get; set; }
              public int Age { get; set; }
              
          }
      
      public interface IDocument
          {
               Int32 Id { get; set; }
          }
      //code
      var realm = Realm.GetInstance();
      
                  realm.Write(() =>
                  {
                      try
                      {
                          Int32 newId = NewId<Dog>(realm); 
                        
                          realm.Add(new Dog { Id = newId, Name = "Rex", Age = 1 });
                      }
                      catch(System.Exception ex)
                      {
                          Debug.WriteLine("Error --> " + ex.ToString()); 
                      }
                  });
      
      private  Int32 NewId<T>( Realm realm) where T : Realms.RealmObject, IDocument
              {
                  try
                  {
                       //this query gets an exception
                      T obj = realm.All<T>().OrderByDescending(x => x.Id).FirstOrDefault();
                      if (obj == null)
                      {
                          return 1;
                      }
                      return obj.Id + 1;
                  }
                  catch (InvalidOperationException)
                  {
                      return 1;
                  }
              }
      
      
      

      Version of Realm and Tooling

      Realm 1.2 Xamarin
      Visual Studio 2017
      Xamarin IOS

      • Realm Object Server Version: none
      • Flavor: Developer
      • Server OS & Version: Windows 10
      • Client SDK Version: Xamarin IOS SDK 10.8.0.174
      • Client OS & Version: IOS 10.3

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: