从azure DB获取数据

时间:2017-12-19 23:15:52

标签: c# azure xamarin

我是Xamarin的Azure新手,我正在尝试制作应用。

如何从数据库中检索项目?

目前,此代码只是将一个项目添加到数据库中。

protected override async void OnAppearing()
{
    base.OnAppearing();

    TodoItem item = new TodoItem { Text = "Awesome item" };
    await MobileService.GetTable<TodoItem>().InsertAsync(item);
}

修改

@Brandon Minnick,这是我在你的回答评论中提到的错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

我建议使用SyncTable。这可确保本地数据库中的项目与远程数据库中的项目同步。

您可以使用await MobileService.GetSyncTable<TodoItem>()从数据库中检索所有项目。

protected override async void OnAppearing()
{
    base.OnAppearing();

    TodoItem item = new TodoItem { Text = "Awesome item" };
    await MobileService.GetSyncTable<TodoItem>().InsertAsync(item);

    List<ToDoItem> allItems = await MobileService.GetSyncTable<TodoItem>().ToEnumerableAsync();
}

我喜欢使用这个泛型类来处理我的所有Azure查询

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;

using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.Sync;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;

using UITestSampleApp.Shared;

namespace UITestSampleApp
{
    public class AzureService : IDataService
    {
        #region Constant Fields
        readonly Dictionary<Type, bool> _isInitializedDictionary = new Dictionary<Type, bool>();
        readonly Lazy<MobileServiceClient> _mobileServiceClientHolder = new Lazy<MobileServiceClient>(() => new MobileServiceClient(AzureConstants.AzureDataServiceUrl));
        #endregion

        #region Fields
        int _networkIndicatorCount;
        #endregion

        #region Properties
        MobileServiceClient MobileServiceClient => _mobileServiceClientHolder.Value;
        #endregion

        #region Methods
        public async Task<IEnumerable<T>> GetItemsAsync<T>() where T : EntityData
        {
            await Initialize<T>();

            return await MobileServiceClient.GetSyncTable<T>().ToEnumerableAsync();
        }

        public async Task<T> GetItem<T>(string id) where T : EntityData
        {
            await Initialize<T>();

            return await MobileServiceClient.GetSyncTable<T>().LookupAsync(id);
        }

        public async Task AddItemAsync<T>(T item) where T : EntityData
        {
            await Initialize<T>();

            await MobileServiceClient.GetSyncTable<T>().InsertAsync(item);
        }

        public async Task UpdateItemAsync<T>(T item) where T : EntityData
        {
            await Initialize<T>();

            await MobileServiceClient.GetSyncTable<T>().UpdateAsync(item);
        }

        public async Task RemoveItemAsync<T>(T item) where T : EntityData
        {
            await Initialize<T>();

            await MobileServiceClient.GetSyncTable<T>().DeleteAsync(item);
        }

        public async Task SyncItemsAsync<T>() where T : EntityData
        {
            await Initialize<T>();

            UpdateNetworkActivityIndicatorStatus(true);

            try
            {
                await MobileServiceClient.GetSyncTable<T>().PullAsync($"all{typeof(T).Name}", MobileServiceClient.GetSyncTable<T>().CreateQuery());
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Error during Sync occurred: {ex.Message}");
            }
            finally
            {
                UpdateNetworkActivityIndicatorStatus(false);
            }
        }

        async Task Initialize<T>() where T : EntityData
        {
            if (IsDataTypeInitialized<T>())
                return;

            _isInitializedDictionary?.Add(typeof(T), false);

            await ConfigureOnlineOfflineSync<T>();

            _isInitializedDictionary[typeof(T)] = true;
        }

        async Task ConfigureOnlineOfflineSync<T>() where T : EntityData
        {
            var path = Path.Combine(MobileServiceClient.DefaultDatabasePath, "app.db");
            var store = new MobileServiceSQLiteStore(path);
            store.DefineTable<T>();

            await MobileServiceClient.SyncContext.InitializeAsync(store, new SyncHandler());
        }

        bool IsDataTypeInitialized<T>() where T : EntityData
        {
            var isDataTypeInitalized = _isInitializedDictionary?.FirstOrDefault(x => x.Key.Equals(typeof(T))).Value;
            return isDataTypeInitalized == true;
        }

        void UpdateNetworkActivityIndicatorStatus(bool isActivityIndicatorDisplayed)
        {
            if (isActivityIndicatorDisplayed)
            {
                Xamarin.Forms.Device.BeginInvokeOnMainThread(() => Xamarin.Forms.Application.Current.MainPage.IsBusy = true);
                _networkIndicatorCount++;
            }
            else if (--_networkIndicatorCount <= 0)
            {
                Xamarin.Forms.Device.BeginInvokeOnMainThread(() => Xamarin.Forms.Application.Current.MainPage.IsBusy = false);
                _networkIndicatorCount = 0;
            }
        }
        #endregion
    }
}

此代码取自使用Azure移动服务的Xamarin.Forms示例应用程序: https://github.com/brminnick/UITestSampleApp/blob/master/Src/UITestSampleApp/Services/AzureService.cs

相关问题