Search code examples
firebase-realtime-databasexamarin.android

Xamarin Android app can't parse Android.Graphics.Color in release mode, but in debug mode everything works as expected


For some reason, FirebaseClient (FirebaseDatabase.net) in release mode can't parse Android.Graphics.Color objects which are part of my data class, but in debug mode it works without problem.

Firebase observer for Table:

private readonly FirebaseClient firebaseClient = new FirebaseClient(...);
...
firebaseClient.Child("Tables")
                          .AsObservable<Table>()
                          .Subscribe(d =>
                          {
                              if (d.Object != null)
                              {
                                  Table table = d.Object;
 
                                  // ...
                              }
                          });

Table class

public class Table
{
        public int Id { get; set; }
        public byte SeatsNum { get; set; }
        public List<Guest> Guests { get; set; }
 
        [JsonConstructor]
        public Table(int id, byte seatsNum)
        {
            Id = id;
            SeatsNum = seatsNum;
            Guests = new List<Guest>();
        }
 
        public Table(int id, byte seatsNum, List<Guest> guests)
        {
            Id = id;
            SeatsNum = seatsNum;
            Guests = guests ?? new List<Guest>();
        }
}

Guest class

public class Guest
{
        public string Name { get; }
        public Color  BackgroundColor { get; }
        public Color TextColor { get; }
        public List<string> Tags { get; }
        public int TableId { get; }
        public bool Checked { get; set; }
 
        public Guest(string name, int tableId)
        {
            Name = name;
            ColorService colorService = new ColorService();
            (Color backgroundColor, Color textColor) = colorService.GetRandomColor();
            BackgroundColor = backgroundColor;
            TextColor = textColor;
            Tags = new List<string>();
            TableId = tableId;
            Checked = false;
        }
 
        public Guest(string name, int tableId, params string[] tags)
            : this(name, tableId)
        {
            Tags = tags.ToList();
        }
 
        [JsonConstructor]
        public Guest(Color backgroundColor, bool Checked, string name, int tableId, List<string> tags, Color textColor)
        {
            Name = name;
            BackgroundColor = backgroundColor;
            TextColor = textColor;
            Tags = tags ?? new List<string>();
            TableId = tableId;
            this.Checked = Checked;
        }
}

In debug mode everything works fine, but when i switch app to release mode and rebuild project, for some reason Color objects are (R,G,B,A)=(0,0,0,0), all other properties of data classes are parsed successfully.

I have solved my problem by saving BackgroundColor and TextColor as strings and manually convert them in Color object when needed. But, im interested, what could be a problem that can lead from normal behavior in debug mode to failure in release mode?


Solution

  • You can check this doc(Configure the Linker).

    The linker then discards all the unused assemblies, types, and members that are not used (or referenced).

    It could cause some helpful assemblies to be ignored when in release mode.