4

I created an app for Android and Windows Phone. For data access i'm using sqllite.net async. I created the simple example solution with a PCL liblary, Xamarin Android project and Windows Phone 8 silverligth project. This is my DataService in PCL:

public  class DataService
{
     private SQLiteAsyncConnection _dbConnection;
     public DataService(ISQLitePlatform platform, string path)
    {
        var connectionFactory = new Func<SQLiteConnectionWithLock>
                (() => new SQLiteConnectionWithLock(platform, new     SQLiteConnectionString(path, true)));
        _dbConnection = new SQLiteAsyncConnection(connectionFactory);
    }
    public async Task Initialize()
    {
        await _dbConnection.CreateTableAsync<ToDo>().ContinueWith(t =>
        {
           Debug.WriteLine("Create");
        });
    }
    public async Task<int> AddNewToDo(ToDo item)
    {
        var result = await _dbConnection.InsertAsync(item);
        return result;
    } 

    public async Task<List<ToDo>> GetAllToDos()
    {
        var result = await _dbConnection.Table<ToDo>().OrderByDescending(t => t.TimeStamp).ToListAsync();
        return result;
    }
    ....
  }

This is using in Windows Phone:

    private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var db = new DataService(new SQLitePlatformWP8(), "my.db");
        await db.Initialize();
        await db.AddNewToDo(new ToDo {Text = "Hello world"});
        var items = await db.GetAllToDos();
        Debug.WriteLine("Count - {0}",items.Count);
    } 

output in Windows Phone:

Create
Count - 1

It is ok. Debugging is works.

This is using in Xamarin Android:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += delegate
        {
            TestDb();
        };
    }

    private async void TestDb()
    {
        string documentsPath =        System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); 
        var path = Path.Combine(documentsPath, "my.db");
        var db = new DataService(new SQLitePlatformAndroid(), path);
        await db.Initialize();
        await db.AddNewToDo(new ToDo { Text = "Hello world" });
        var items = await db.GetAllToDos();
        Console.WriteLine("count - {0}",items.Count);
    }

output:

 [0:] 
 Create
 [0:] Create
 02-18 00:46:01.167 I/mono-stdout(19234): Create
 count - 1
 02-18 00:46:01.675 I/mono-stdout(19234): count - 1

Why are invoked more than once? Debugging not working. When I stop at code with await, next step just drops out of the method without touching my return calls or anything.

This is a simple example, and I do not understand why this is happening. Maybe I'm doing something wrong.

5
  • I don't think your stuff is getting invoked more than once. If I remember correctly the logcat shows multiple rows for each debug line of output with Xamarin. Put a debug statement in non-async code and see if it is duplicated as well. Commented Feb 18, 2015 at 13:40
  • Why does not the debugging (with await) in the visual studio? Commented Feb 18, 2015 at 13:43
  • The awaited code isn't run immediately. The current method needs to exit before the debugger will turn control over to it. If you put a breakpoint only on the line that outputs 'Create' and then let it run, you should hit the breakpoint. Commented Feb 18, 2015 at 13:46
  • Begin to understand )). My code is works. I just do not understand why debugging in Visual studio works differently in Windows Phone and Xamarin. Commented Feb 18, 2015 at 14:08
  • Debuggers are complicated things, especially when you add things like asynchronous execution and multi-threading into the mix. Commented Feb 18, 2015 at 14:09

1 Answer 1

3

Your code have a problem:

button.Click += delegate
{
    TestDb();
};

TestDb is an async method and you're calling it asynchronously without the await.

This will make the call to TestDb to happens after you leave the Click event code.

I suggest you to await the call:

button.Click += async delegate
{
    await TestDb();
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.