我可以在Xamarin Forms to Wear应用程序中发送SQlite数据库数据吗?

时间:2017-11-26 15:20:36

标签: c# sqlite xamarin.forms wear-os

我的Android手机和Wear设备上有一个基于SQlite的Xamarin Forms应用程序。根据{{​​3}}执行我的手机和我的Wear手表之间的同步。

我的数据库有下表Match和MatchStat:

public class Match
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public DateTime MatchDate { get; set; }
    public TimeSpan MatchTime { get; set; }
    public string Home { get; set; }
    public string Guest { get; set; }
    public int HomeScore { get; set; }
    public int GuestScore { get; set; }
    public bool Active { get; set; }
    public bool Done { get; set; }
    public string HomeColor { get; set; }
    public string GuestColor { get; set; }

    public Match()
    { }
}

public class MatchStat
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public int MatchId { get; set; }
    public int ActionMin { get; set; }
    public int HomeScore { get; set; }
    public int GuestScore { get; set; }
    public string ActionItem { get; set; }
    public string PlayerName { get; set; }

    public MatchStat()
    { }
}

如果我想将我的Match和所有MatchStat数据从一个设备同步到另一个设备我是通过将数据映射为MainActivity中的字符串来实现的:

    public async static void SendNewMatch(Match match, string path)
    {
        if (!client.IsConnected)
            client.Connect();
        await Task.Delay(200);
        try
        {
            var request = PutDataMapRequest.Create(path);
            var map = request.DataMap;

            if (match != null)
            {
                map.PutString("Device", device);
                map.PutString("Item", "AddMatch");
                map.PutString("Home", match.Home);
                map.PutString("Guest", match.Guest);
                map.PutString("Active", match.Active.ToString());
                map.PutString("Done", match.Done.ToString());
                map.PutString("GuestScore", match.GuestScore.ToString());
                map.PutString("HomeScore", match.HomeScore.ToString());
                map.PutString("Date", match.MatchDate.Date.ToString());
                map.PutString("Time", match.MatchTime.ToString());
                map.PutLong("UpdatedAt", DateTime.UtcNow.Ticks);
                await WearableClass.DataApi.PutDataItem(client, request.AsPutDataRequest());
            }
            request.UnregisterFromRuntime();
        }
        catch
        { }
        finally
        {
            client.Disconnect();
        }

    }

    public async static void SendMatchStat(MatchStat matchstat, Match match, int matchstatsize, string path)
    {
        if (!client.IsConnected)
            client.Connect();
        await Task.Delay(200);
        try
        {
            var request = PutDataMapRequest.Create(path);
            var map = request.DataMap;
            MatchHelper mh = new MatchHelper();

            if (matchstat != null)
            {
                map.PutString("Device", device);
                map.PutString("Item", "MatchStat");
                map.PutString("Home", match.Home);
                map.PutString("Date", match.MatchDate.Date.ToString());
                map.PutString("Time", match.MatchTime.ToString());
                map.PutString("ActionItem", matchstat.ActionItem);
                map.PutString("ActionMin", matchstat.ActionMin.ToString());
                map.PutString("GuestScore", matchstat.GuestScore.ToString());
                map.PutString("HomeScore", matchstat.HomeScore.ToString());
                map.PutString("MatchStatSize", matchstatsize.ToString());
                //map.PutString("PlayerName", matchstat.PlayerName.ToString());
                map.PutLong("UpdatedAt", DateTime.UtcNow.Ticks);
                await WearableClass.DataApi.PutDataItem(client, request.AsPutDataRequest());
            }
            request.UnregisterFromRuntime();
        }
        catch
        { }
        finally
        {
            client.Disconnect();
        }

    }

    public void ProcessMessage(Intent intent)
    {
        if (intent.GetStringExtra("Device") != device)
        {
            switch (intent.GetStringExtra("Item"))
            {
                case "AddMatch":
                    {
                        AddMatch(intent);
                        break;
                    }
                case "MatchStat":
                    {
                        InsertMatchStat(intent);
                        break;
                    }

            }
        }
    }

    private void AddMatch(Intent intent)
    {
        MatchHelper mh = new MatchHelper();
        if (bool.Parse(intent.GetStringExtra("Active")))
        {
            ObservableCollection<Match> activeMatches = mh.GetActiveMatches();
            foreach (Match activeMatch in activeMatches)
            {
                mh.InactivateMatch(activeMatch);
            }
        }
        Match newmatch = new Match();
        newmatch.Home = intent.GetStringExtra("Home");
        newmatch.Guest = intent.GetStringExtra("Guest");
        newmatch.HomeColor = intent.GetStringExtra("HomeColor");
        newmatch.GuestColor = intent.GetStringExtra("GuestColor");
        newmatch.Active = bool.Parse(intent.GetStringExtra("Active"));
        newmatch.Done = bool.Parse(intent.GetStringExtra("Done"));
        newmatch.HomeScore = int.Parse(intent.GetStringExtra("HomeScore"));
        newmatch.GuestScore = int.Parse(intent.GetStringExtra("GuestScore"));
        newmatch.Active = bool.Parse(intent.GetStringExtra("Active"));
        newmatch.Done = bool.Parse(intent.GetStringExtra("Done"));
        newmatch.MatchDate = DateTime.Parse(intent.GetStringExtra("Date"));
        newmatch.MatchTime = TimeSpan.Parse(intent.GetStringExtra("Time"));
        mh.InsertMatch(newmatch);
    }

    private void InsertMatchStat(Intent intent)
    {
        MatchHelper mh = new MatchHelper();
        Match match = mh.GetSpecificMatch(intent.GetStringExtra("Home"), DateTime.Parse(intent.GetStringExtra("Date")), TimeSpan.Parse(intent.GetStringExtra("Time")));
        if (match != null)
        {
            MatchStat machstat = new MatchStat();
            machstat.MatchId = match.Id;
            machstat.ActionItem = intent.GetStringExtra("ActionItem");
            machstat.ActionMin = int.Parse(intent.GetStringExtra("ActionMin"));
            machstat.GuestScore = int.Parse(intent.GetStringExtra("GuestScore"));
            machstat.HomeScore = int.Parse(intent.GetStringExtra("HomeScore"));
            machstat.PlayerName = intent.GetStringExtra("PlayerName");

            mh.InsertMatchStat(machstat);

        }
    }

在我的WearService中,我有OnDataChanged:

    public override void OnDataChanged(DataEventBuffer dataEvents)
    {
        var dataEvent = Enumerable.Range(0, dataEvents.Count)
                                  .Select(i => dataEvents.Get(i).JavaCast<IDataEvent>())
                                  .FirstOrDefault(x => x.Type == DataEvent.TypeChanged && x.DataItem.Uri.Path.Equals(_syncPath));
        if (dataEvent == null)
            return;

        //get data from wearable
        var dataMapItem = DataMapItem.FromDataItem(dataEvent.DataItem);
        var map = dataMapItem.DataMap;

        Intent intent = new Intent();
        intent.SetAction(Intent.ActionSend);

        intent.PutExtra("Device", map.GetString("Device"));
        intent.PutExtra("Item", map.GetString("Item"));

        switch (map.GetString("Item"))
        {
            case "AddMatch":
                {
                    intent.PutExtra("Home", map.GetString("Home"));
                    intent.PutExtra("Guest", map.GetString("Guest"));
                    intent.PutExtra("HomeColor", map.GetString("HomeColor"));
                    intent.PutExtra("GuestColor", map.GetString("GuestColor"));
                    intent.PutExtra("Active", map.GetString("Active"));
                    intent.PutExtra("Done", map.GetString("Done"));
                    intent.PutExtra("HomeScore", map.GetString("HomeScore"));
                    intent.PutExtra("GuestScore", map.GetString("GuestScore"));
                    intent.PutExtra("Date", map.GetString("Date"));
                    intent.PutExtra("Time", map.GetString("Time"));
                    LocalBroadcastManager.GetInstance(this).SendBroadcast(intent);
                    break;
                }
            case "MatchStat":
                {
                    intent.PutExtra("Home", map.GetString("Home"));
                    intent.PutExtra("Date", map.GetString("Date"));
                    intent.PutExtra("Time", map.GetString("Time"));
                    intent.PutExtra("ActionItem", map.GetString("ActionItem"));
                    intent.PutExtra("ActionMin", map.GetString("ActionMin"));
                    intent.PutExtra("GuestScore", map.GetString("GuestScore"));
                    intent.PutExtra("HomeScore", map.GetString("HomeScore"));
                    intent.PutExtra("PlayerName", map.GetString("PlayerName"));
                    LocalBroadcastManager.GetInstance(this).SendBroadcast(intent);
                    break;
                }
        }
    }

我不想通过单独的字符串发送数据,而是将我的匹配数据作为数据库文件发送(必要时作为.ToString)。是否有可能实现这一点,以后如何检索数据。

其次,我将MatchStats作为列表(IEnumerable或ObservableCollection)。是否可以将其作为列表发送,或者我必须单独发送每个MatchStat。通过单独发送Matchstats,我的其他设备将无法按所需顺序接收它们,并且不会收到所有MatchStats。

1 个答案:

答案 0 :(得分:0)

我没有将数据库数据作为数据库文件发送,而是通过将Matchstats发送到一个文件而不是发送所有单独的数据来解决这个问题。

    public async static void SendMatchStats(ObservableCollection<MatchStat> matchstats, Match match, int matchstatsize, string path)
    {
        if (!client.IsConnected)
            client.Connect();
        await Task.Delay(80);
        try
        {
            var request = PutDataMapRequest.Create(path);
            var map = request.DataMap;
            MatchHelper mh = new MatchHelper();
            int cnt = 1;
            if (matchstatsize != 0)
            {
                map.PutString("Device", device);
                map.PutString("Item", "MatchStats");
                map.PutString("Home", match.Home);
                map.PutString("Date", match.MatchDate.Date.ToString());
                map.PutString("Time", match.MatchTime.ToString());
                map.PutString("MatchStatSize", matchstatsize.ToString());
                map.PutString("Path", path);
                foreach (MatchStat matchstat in matchstats)
                {
                    map.PutString("ActionItem" + cnt.ToString(), matchstat.ActionItem);
                    map.PutString("ActionMin" + cnt.ToString(), matchstat.ActionMin.ToString());
                    map.PutString("Color" + cnt.ToString(), matchstat.Color);
                    //map.PutString("ColorSchemeId", matchstat.ColorSchemeId.ToString());
                    map.PutString("GuestScore" + cnt.ToString(), matchstat.GuestScore.ToString());
                    map.PutString("HomeScore" + cnt.ToString(), matchstat.HomeScore.ToString());
                    if (matchstat.PlayerName == null)
                        map.PutString("PlayerName" + cnt.ToString(), "");
                    else
                        map.PutString("PlayerName" + cnt.ToString(), matchstat.PlayerName.ToString());
                    cnt++;
                }
                map.PutLong("UpdatedAt", DateTime.UtcNow.Ticks);
                await WearableClass.DataApi.PutDataItem(client, request.AsPutDataRequest());
            }
            request.SetUrgent();
            request.UnregisterFromRuntime();
        }
        catch
        { }
        finally
        {
            client.Disconnect();
        }

    }

在我的WearService中,我有OnDataChanged:

                case "MatchStats":
                {
                    int size = int.Parse(map.GetString("MatchStatSize"));
                    intent.PutExtra("Home", map.GetString("Home"));
                    intent.PutExtra("Date", map.GetString("Date"));
                    intent.PutExtra("Time", map.GetString("Time"));
                    intent.PutExtra("MatchStatSize", map.GetString("MatchStatSize"));
                    intent.PutExtra("Path", map.GetString("Path"));
                    for (int cnt = 1; cnt <= size; cnt++)
                    {
                        intent.PutExtra("ActionItem" + cnt.ToString(), map.GetString("ActionItem" + cnt.ToString()));
                        intent.PutExtra("ActionMin" + cnt.ToString(), map.GetString("ActionMin" + cnt.ToString()));
                        intent.PutExtra("Color" + cnt.ToString(), map.GetString("Color" + cnt.ToString()));
                        intent.PutExtra("GuestScore" + cnt.ToString(), map.GetString("GuestScore" + cnt.ToString()));
                        intent.PutExtra("HomeScore" + cnt.ToString(), map.GetString("HomeScore" + cnt.ToString()));
                        intent.PutExtra("PlayerName" + cnt.ToString(), map.GetString("PlayerName" + cnt.ToString()));
                        Console.WriteLine("PutExtra " + map.GetString("ActionMin" + cnt.ToString()) + " min " + map.GetString("HomeScore" + cnt.ToString()) + "-" + map.GetString("GuestScore" + cnt.ToString()));
                        Console.WriteLine("GetStringExtra " + intent.GetStringExtra("ActionMin" + cnt.ToString()) + " min " + intent.GetStringExtra("HomeScore" + cnt.ToString()) + "-" + intent.GetStringExtra("GuestScore" + cnt.ToString()));
                    }
                    LocalBroadcastManager.GetInstance(this).SendBroadcast(intent);
                    break;
                }

在MainActivity中,我正在处理单独的void InsertMatchstats中的数据:

    private void InsertMatchStats(Intent intent)
    {
        MatchHelper mh = new MatchHelper();
        Match match = mh.GetSpecificMatch(intent.GetStringExtra("Home"), DateTime.Parse(intent.GetStringExtra("Date")), TimeSpan.Parse(intent.GetStringExtra("Time")));
        int size = int.Parse(intent.GetStringExtra("MatchStatSize"));
        if (match != null)
        {

            for (int cnt = 1; cnt <= size; cnt++)
            {
                MatchStat matchstat = new MatchStat
                {
                    MatchId = match.Id,
                    ActionItem = intent.GetStringExtra("ActionItem" + cnt.ToString()),
                    ActionMin = int.Parse(intent.GetStringExtra("ActionMin" + cnt.ToString())),
                    Color = intent.GetStringExtra("Color" + cnt.ToString()),
                    //ColorSchemeId = int.Parse(intent.GetStringExtra("ColorSchemeId"));
                    GuestScore = int.Parse(intent.GetStringExtra("GuestScore" + cnt.ToString())),
                    HomeScore = int.Parse(intent.GetStringExtra("HomeScore" + cnt.ToString())),
                    PlayerName = intent.GetStringExtra("PlayerName" + cnt.ToString()),
                    Sync = true
                };
                if (matchstat.Color == null)
                    matchstat.Color = "#FFFFFF";
                MatchStat checkMatchStat = mh.CheckMatchStat(matchstat.HomeScore, matchstat.GuestScore, matchstat.ActionItem, matchstat.MatchId);
                if (checkMatchStat == null)
                    mh.InsertMatchStat(matchstat);
                else
                {
                    checkMatchStat.ActionMin = matchstat.ActionMin;
                    checkMatchStat.Color = matchstat.Color;
                    //checkMatchStat.ColorSchemeId = matchstat.ColorSchemeId;
                    checkMatchStat.PlayerName = matchstat.PlayerName;
                    mh.UpdateMatchStat(checkMatchStat);
                }
                Console.WriteLine("PutExtra " + intent.GetStringExtra("ActionMin" + cnt.ToString()) + " min " + intent.GetStringExtra("HomeScore" + cnt.ToString()) + "-" + intent.GetStringExtra("GuestScore" + cnt.ToString()));
            }
            SendBack(intent.GetStringExtra("Path"));

        }
    }

使用此方法,同步更快,更精确。没有数据丢失,所有数据都按正确的顺序接收和处理。