gpt4 book ai didi

c# - 这是使用 Xamarin Forms 和 SQLite 建立数据库连接的完美方式吗?

转载 作者:行者123 更新时间:2023-12-03 14:38:35 29 4
gpt4 key购买 nike

我有一个应用程序可以在大部分时间工作,但有时会在 5-10 分钟后停止工作。我正在努力找出问题所在,为此我想得到一些反馈,以了解我与应用程序建立数据库连接的方式是否理想。这是我现在所拥有的:

public partial class App : Application
{
public static DataManager DB { get; } = new DataManager();

public partial class DataManager
{
protected static object l = new object();
protected SQLiteConnection db1;

public DataManager()
{
db1 = DependencyService.Get<ISQLiteDB1>().GetConnection();
db2.TimeExecution = true;
}

public interface ISQLiteDB1
{
SQLiteConnection GetConnection();
}

[assembly: Dependency(typeof(SQLiteDB1_iOS))]
namespace Ja.iOS
{
public class SQLiteDB1_iOS : ISQLiteDB1
{
public SQLite.SQLiteConnection GetConnection()
{
var sqliteFilename = "db1.db3";
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string libraryPath = Path.Combine(documentsPath, "..", "Library");
Debug.WriteLine((libraryPath));
var path = Path.Combine(libraryPath, sqliteFilename);
if (!File.Exists(path))
{
File.Create(path);
}
return new SQLite.SQLiteConnection(path);
}
}
}

[assembly: Dependency(typeof(SQLiteDB1_Android))]
namespace Jap.Droid
{
public class SQLiteDB1_Android :ISQLiteDB1
{

public SQLite.SQLiteConnection GetConnection()
{
var sqliteFilename = "db1.db3";
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, sqliteFilename);
if (!File.Exists(path))
{
File.Create(path);
}
return new SQLite.SQLiteConnection(path);
}
}
}

我使用此代码执行查询
public partial class DataManager
{

public void UpdatePhraseSetViewedFalse()
{
sql = string.Format("UPDATE Phrase SET Viewed = 0 WHERE Selected = 1");
App.DB.RunExecute(sql);
}

public partial class DataManager
{

private int RunExecute(String s)
{
lock (l)
{
try
{
rc1 = db2.Execute(s);
}
catch (Exception ex)
{
Crashes.TrackError(ex,
new Dictionary<string, string> {
{"RunExecute", "Exception"},
{"sql", s },
{"Device Model", DeviceInfo.Model },
});
throw;
}
}

以及用于插入和更新的代码样式:
            db2.Insert(new QuizHistory()
{
QuizId = quiz,
Cards = (App.viewablePhrases ?? GetViewablePhrases(MO.Quiz)).Count,
Score = score,
UtcNow = (int)Math.Truncate(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds),
Viewed = 1,
Deck = Helpers.Deck.GetTitle()
});

为了减少出现问题的机会,我应该在访问数据库的所有代码周围添加 lock(l) 吗?我应该改变我的连接方式吗?

最佳答案

目前尚不清楚应用程序如何停止工作(崩溃、挂起、其他?),但代码似乎有一些可能导致该问题的危险信号:

  • 您正在使用 lock从数据库访问数据时。虽然语法正确,但可能会导致死锁,如果调用数据访问方法如 UpdatePhraseSetViewedFalse从 UI 线程(并且很可能是您这样做),这将是应用程序挂起/崩溃的主要原因。
  • 您正在手动创建 SQL 查询并执行它们。 SQLite 因 performance issues when it is accessed on multiple threads 而闻名所以你可以尝试阅读 best practices ,最好使用一些 ORM 和 Wrapper SDK 来正确访问数据库。如SQLite.NET for iOSSQLite.NET for Android
  • 关于c# - 这是使用 Xamarin Forms 和 SQLite 建立数据库连接的完美方式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096072/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com