【问题标题】:Is it possible to save custom sized array of int arrays as field of RealmObject?是否可以将自定义大小的 int 数组数组保存为 RealmObject 的字段?
【发布时间】:2017-01-29 12:25:19
【问题描述】:

基本上我需要这样的数据类型:

int[] list1 = new int[4] { 1, 2, 3, 4 };
int[] list2 = new int[4] { 5, 6, 7, 8 };
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };

int[][] lists = new int[][] {  list1 ,  list2 ,  list3 ,  list4  };

具有 4 个整数的自定义大小的数组。

那么我可以在 Realm 数据库中进行操作吗?

感觉最好用List<int[4]> 替换“自定义大小的数组”,但我怀疑这是可能的。

【问题讨论】:

    标签: arrays xamarin realm


    【解决方案1】:

    有几种方法可以解决这个问题,这里是一种。 ;-)

    我会创建两个RealmObjects。一个定义数组的单个元素(在我的示例中由四个整数定义的Color)和一个包含这些数组元素的IListRealmObject

    示例领域对象:

    public class Color : RealmObject
    {
        public int R { get; set; }
        public int G { get; set; }
        public int B { get; set; }
        public int A { get; set; }
    
        public int[] RGBA
        {
            get { return new int[] { R, G, B, A }; }
            set { R = value[0]; G = value[1]; B = value[2]; A = value[3]; }
        }
    }
    
    public class MaterialColors : RealmObject
    {
        public string Material { get; set; }
        public Color PrimaryColor { get; set; }
        public IList<Color> AlternativeColors { get; }
        public void AddAlts(Color[] ca)
        {
            for (int i = 0; i < ca.Length; i++)
            {
                AlternativeColors.Add(ca[i]);
            }
        }
    }
    

    使用示例:

    using (var realm = Realm.GetInstance(new RealmConfiguration { SchemaVersion = 1 }))
    {
        var primary = new Color { RGBA = new int[] { 1, 2, 3, 4 } };
        var alt1 = new Color { RGBA = new int[] { 5, 6, 7, 8 } };
        var alt2 = new Color { RGBA = new int[] { 1, 3, 2, 1 } };
        var alt3 = new Color { RGBA = new int[] { 5, 4, 3, 2 } };
    
        var material = new MaterialColors
        {
            Material = "StackOverflow",
            PrimaryColor = primary,
        };
        // Add array element one at a time... 
        material.AlternativeColors.Add(alt3);
        // Add multiple elements (array[]) via custom method...
        material.AddAlts(new Color[] { alt1, alt2 });
    
        realm.Write(() =>
        {
            realm.Add(material);    
        });
    
        var materials = realm.All<MaterialColors>();
        foreach (var aMaterial in materials)
        {
            Console.WriteLine($"Pri: [{aMaterial.PrimaryColor.RGBA[0]}:{aMaterial.PrimaryColor.RGBA[1]}:{aMaterial.PrimaryColor.RGBA[2]}:{aMaterial.PrimaryColor.RGBA[3]}]");
            foreach (var color in aMaterial.AlternativeColors)
            {
                Console.WriteLine($"Alt: [{color.RGBA[0]}:{color.RGBA[1]}:{color.RGBA[2]}:{color.RGBA[3]}]");
            }
        }
    }
    

    输出:

    Pri: [1:2:3:4]
    Alt: [5:4:3:2]
    Alt: [5:6:7:8]
    Alt: [1:3:2:1]
    

    【讨论】:

    • 完美答案!谢谢!
    【解决方案2】:

    来自@SushiHangover 的精彩回答。

    我们将在某个时候对此提供更直接的支持。我们不承诺发布日期,但您可以track the issue 了解它何时正在开发以及源代码可用。

    如果您对场景或期望的行为有进一步的设计反馈,请将 cmets 添加到that issue 1194

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 2019-03-11
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 2016-05-22
      相关资源
      最近更新 更多