Приложение .NET MAUI зависает на CreateTableAsyncC#

Место общения программистов C#
Ответить
Anonymous
 Приложение .NET MAUI зависает на CreateTableAsync

Сообщение Anonymous »

Я следовал этому руководству, чтобы внедрить SQLite-net в свое приложение MAUI. Всякий раз, когда программа выполняет «await db.CreateTableAsync();», все приложение зависает. Я попробовал несколько решений, в том числе попытался предотвратить взаимоблокировку с помощью ConfigurationAwait(false), как показано в этом блоге, и использовал MainThread.BeginInvokeOnMainThread, как показано в этой теме и этой теме. Оба этих потока не работали, потому что решения по-прежнему назывались CreateTableAsync, поскольку они находятся внутри задачи InitAsync.

Я также пробовал:

-Обновить все мои зависимости.
-Реализация raw.bundle.green.

-Создание совершенно нового проекта, чтобы посмотреть, будет ли проблема воспроизведена (так и было).

-Удаление свойства Location модели Restroom и соответствующая корректировка кода.

-Добавление true в .csproj, чтобы отключить быстрое развертывание согласно этому ответ.

В чем может быть проблема?
Restroom.cs

Код: Выделить всё

using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dook.Model
{
public class Restroom
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Username { get; set; }
public Location PinLocation { get; set; }
}
}
RestroomService.cs

Код: Выделить всё

using Dook.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dook.Services
{
public static class RestroomService
{
static SQLiteAsyncConnection db;

static async Task InitAsync()
{
if(db != null)
return;

//Get an absolute path to the database file
var databasePath = Path.Combine(FileSystem.AppDataDirectory, "MyData.db");

db = new SQLiteAsyncConnection(databasePath);

await db.CreateTableAsync();
}

public static async Task AddPinAsync(string name, string address, string username, Location location)
{
await InitAsync();
var test = "test";
var restroom = new Restroom
{
Name = name,
Address = address,
Username = username,
PinLocation = location
};

var id = await db.InsertAsync(restroom);
}

public static async Task RemovePinAsync(int id)
{
await InitAsync();

await db.DeleteAsync(id);
}

public static async Task GetPinAsync()
{
await InitAsync();

var restroom = await db.Table().ToListAsync();
return restroom;
}
}
}

MainViewModel.cs

Код: Выделить всё

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls.Maps;
using Microsoft.Maui.Maps;
using Map = Microsoft.Maui.Controls.Maps.Map;
using Dook.Model;
using Dook.Services;
using MvvmHelpers;
using MvvmHelpers.Commands;
using CommunityToolkit.Maui.Core.Extensions;

namespace Dook.ViewModel
{
public partial class MainViewModel : BaseViewModel
{
public ObservableRangeCollection Restroom { get; set; }
public AsyncCommand RefreshCommand { get; }
public AsyncCommand AddCommand { get; }
public AsyncCommand RemoveCommand { get;  }

public MainViewModel()
{
Title = "Map Controller";

//var restroom1 = RestroomService.GetPin().Result;
//Restroom = restroom1 as ObservableRangeCollection;

Restroom = new ObservableRangeCollection();

AddCommand = new AsyncCommand(Add);
RemoveCommand = new AsyncCommand(Remove);
RefreshCommand = new AsyncCommand(Refresh);
}

async Task Add(Location pinlocation)
{
var name = await App.Current.MainPage.DisplayPromptAsync("Location Name", "Name of Location");
// var address = "Latitude: {pinlocation.Latitude}, Longitude: {pinlocation.Longitude}, Altitude: {location.Altitude}";
var address = "test";
var username = await App.Current.MainPage.DisplayPromptAsync("Username", "Username of Toilet Adder");
Location location = pinlocation;
if(name == null || address == null || username == null) { return; }
await RestroomService.AddPinAsync(name, address, username, location);
await Refresh();
}

async Task Remove(Restroom restroom)
{
await RestroomService.RemovePin(restroom.Id);
await Refresh();
}

async Task Refresh()
{
IsBusy = true;
await Task.Delay(2000);
Restroom.Clear();
var restrooms = await RestroomService.GetPin();
Restroom.AddRange(restrooms);
IsBusy = false;
}

public static Location GetLocation()
{
try
{
Location location = new();
location = Geolocation.Default.GetLastKnownLocationAsync().Result;
if (location != null)
return location;
}
catch (Exception ex)
{
Debug.WriteLine($"Unable to get location: {ex.Message}");
Application.Current.MainPage.DisplayAlert("Error!", ex.Message, "OK");
}

return null;
}
}
}
MainPage.xaml.cs

Код: Выделить всё

namespace Dook;
using Microsoft.Maui.Controls.Maps;
using Microsoft.Maui.Maps;
using System.Diagnostics;
using Map = Microsoft.Maui.Controls.Maps.Map;
using Dook.ViewModel;
using Dook.Model;

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
MoveMapLocation();
}

private void GoToLocation_Button(object sender, EventArgs e)
{
MoveMapLocation();
}

private void OnMapClicked(object sender, MapClickedEventArgs e)
{
var viewModel = new MainViewModel();

if(viewModel.AddCommand.CanExecute(e.Location))
viewModel.AddCommand.ExecuteAsync(e.Location);
}

private void RefreshButton_Clicked(object sender, EventArgs e)
{
RefreshPins();
}

private void MoveMapLocation()
{
//Function to avoid boilerplate code
MapSpan mapSpan = new MapSpan(MainViewModel.GetLocation(), 0.01, 0.01);
mainmap.MoveToRegion(mapSpan);
}

private void RefreshPins()
{
var viewModel = new MainViewModel();

viewModel.RefreshCommand.ExecuteAsync();
}
}
Окно «Параллельные задачи»
Изображение


Подробнее здесь: https://stackoverflow.com/questions/773 ... tableasync
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»