Search code examples
xamarinrealmrealm-mobile-platform

fetch data from server using xamarin with realm


I am trying to create mobile app using Xamarin and Realm. I am trying to pull data from server and load it into objects extended by RealmObjects.

code to fetch data from server

public async void getCompanyMaster() {
    var uri = new Uri(string.Format(URL, string.Empty));

    try
    {
        getConnection();
        var response = await client.GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            Company company = JsonConvert.DeserializeObject<Company>(content);

            Debug.WriteLine("Company List " );
        }
    }
    catch (Exception e)
    {
        Debug.WriteLine("Error {0}", e.Message);

    }       
}

realm model company

namespace DesignModel.Model.Master
{
    public class Company : RealmObject    
    {
        public string errorMessage { get; set; }
        public string status { get; set; }

        [JsonProperty(PropertyName = "companyMaster")]
        IList<CompanyMaster> companyMaster { get; }
    }
}

realm model for company master

namespace DesignModel.Model.Master
{        
    public class CompanyMaster : RealmObject
    {       
        public string companyAddress { get; set; }
        public string companyId { get; set; }
        public string companyName { get; set; }

        public string companyPhoneNumber { get; set; }
    }
}

now the issue is at following line in getCompanyMaster function

Company company = JsonConvert.DeserializeObject<Company>(content);

it raises an exception

Error Error setting value to 'errorMessage' on 'DesignModel.Model.Master.Company'.

if i do not extend my class from RealmObject then it works fine but when i add RealObject it doesn't work.

the above approach does work in android native but do not know why it doesn't work in xamarin.

sample json

{
  "errorMessage": "",
  "status": "Ok",
  "companyMaster": [
    {
      "companyAddress": "123 Coffee Street\nSuite 300\nRedmond, WA 98052\nUSA",
      "companyId": "cec",
      "companyName": "Contoso Entertainment Systems",
      "companyPhoneNumber": "425-555-0156"
    }]    
}

complete stack trace

[0:] Error Newtonsoft.Json.JsonSerializationException: Error setting value to 'errorMessage' on 'DesignModel.Model.Master.Company'. ---> System.PlatformNotSupportedException: The PCL build of Realm is being linked which probably means you need to use NuGet or otherwise link a platform-specific Realm.dll to your main application.


Solution

  • Adding a note from documentation found here

    https://realm.io/docs/xamarin/latest/

    under Getting Started - Prerequisites

    Important note for PCL users - this works via the NuGet Bait and Switch Trick. You have to install the Realm NuGet package into every PCL that uses Realm as well as each of your platform-specific projects, e.g.: your final app for iOS and Android. If you are using shared projects, you only have to install the NuGet into each platform-specific project.