greenhoorn
greenhoorn

Reputation: 1561

Xamarin.Forms method executing twice on certain devices

I have a Xamarin.Forms application for Android and iOS which reads a qr code and saves it in a local sqlite database.

The app is installed on 3 devices (iPhone 4s with iOS 9.3.5, iPhone 5s with iOS 9.4 and Samsung Galaxy S7 Edge with Android 6.0). Strange thing, on the iPhone 4s the barcode method to save it in the database is called twice!

Device.BeginInvokeOnMainThread(() =>{
                        Navigation.PopAsync();
                        DisplayAlert("Scanned Barcode", result.Text, "OK");
                        dbHelper.SaveItem(new DbItem() { Name = result.Text });
    });

Upvotes: 0

Views: 608

Answers (1)

Mario Galván
Mario Galván

Reputation: 4032

You can use a flag to lock the event something like:

Device.BeginInvokeOnMainThread(() =>{

    if(flag){
        return;
    }

    flag = true;

    Navigation.PopAsync();
    DisplayAlert("Scanned Barcode", result.Text, "OK");
    dbHelper.SaveItem(new DbItem() { Name = result.Text });
});

override OnAppearing(){
    flag = false;
}

Upvotes: 1

Related Questions