OData Hangs Forever in Xamarin Form

别等时光非礼了梦想. 提交于 2020-01-15 12:24:25

问题


I am trying to fetch data from the Nortwind URL with using Simple.OData.Client in Xamarin form. Application hangs forever. The simple project source code is in the following link: Download

using Simple.OData.Client;

public async void InitializeDataService(){

    try {
        mODataClient = new ODataClient("http://services.odata.org/Northwind/Northwind.svc/");
    }

    catch {
        await DisplayAlert("Error", "Connection Error", "OK", "Cancel");
        System.Diagnostics.Debug.WriteLine("ERROR!");
    }
}

public async void GetDataFromOdataService (string myDataClicked){

    try {
        myCustomers= mODataClient.For(myDataClicked).Top(10).FindEntriesAsync().Result;
    }

    catch {
        await DisplayAlert("Error", "Connection Error", "OK", "Cancel");
        System.Diagnostics.Debug.WriteLine("ERROR!");
    }
}

Update:1 I have updated my code based on Pete suggestion. Still hangs forever.

private ODataClient mODataClient;
private IEnumerable <IDictionary<string,object>> myCustomers;
public ObservableCollection <Customer>Customers { get; set;}
public string myDataString;

public MyDataServices (string myDataClicked)
{
    Title="Customers";
    myDataString = myDataClicked;
    callServices();
}

public async Task callServices()
{
    await InitializeDataService ();
    await GetDataFromOdataService (myDataString);   
}

public async Task InitializeDataService(){

    try {
        mODataClient = 
            new ODataClient("http://services.odata.org/Northwind/Northwind.svc/");
    }
    catch {
        await DisplayAlert("Error", "Connection Error", "OK", "Cancel");
        System.Diagnostics.Debug.WriteLine("ERROR!");
    }
}

public async Task GetDataFromOdataService (string myDataClicked){

    try {
        myCustomers= mODataClient.For(myDataClicked).Top(10).FindEntriesAsync().Result;
    }
    catch {
        await DisplayAlert("Error", "Connection Error", "OK", "Cancel");
        System.Diagnostics.Debug.WriteLine("ERROR!");
    }
}

回答1:


I suspect this is hanging due how your trying to call async and making it synchronous.

Where you are doing this, I take you are just doing this as a test at present, and that later on you will expand upon as at present your variable is only local to that function.

So try doing public static async Page GetMainPage(), and changing to:-

var customers= await mODataClient.For("Customers").Top(10).FindEntriesAsync();

as it is most likely hanging on your previous implementation.

Note you will most likely have to do async declaration and put in an await to the page that calls the GetMainPage() function also to get all this working.

Really - though - I would take this as a Task<> function and await it from within the Page that you are displaying.

Update 1:-

Issue was related to async / await issues.

Data task was separated out into a separate function returning a Task<>, subsequently updating the ListView.ItemsSource control when the data is retrieved.




回答2:


As Pete suspected, the application hangs most likely due to your attempt to run it synchronously. Inspect the source code and revise all places where you call .Result or .Wait(). Then it should work. I know such refactoring may require substantial code rewrite, but that's the way to go.



来源:https://stackoverflow.com/questions/26407176/odata-hangs-forever-in-xamarin-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!