"You must call xamarin.forms.init () prior to using it" - Problem trying to access sqlite data

1

Hello,

I'm a beginner in mobile programming and I'm committed to a PCL Xamarin project with Xamarin.Forms . The application uses alarms and for this I make use of dependency injection to access the native classes that take care of the notifications. In Android I use the class AlarmManeger which naturally has the records deleted when the device is turned off. To restore the alarms I created a class in the Android project that inherits from BroadcastReceiver and triggers under ActionBootCompleted action and would like to access the SQLite database from the PCL project. But when referencing the connection I get the following exception:

"BootReceiver" class (.Droid project):

namespace NutriTime.Droid.Notification
{
  [BroadcastReceiver]
  [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted },
      Categories = new[] { Android.Content.Intent.CategoryDefault })]
  public class BootReceiver : BroadcastReceiver
  {
      public override void OnReceive(Context context, Intent intent)
      {           
          try
          {
              //A linha abaixo gera a Exception
              NutriTime.Data.RefeicaoRepository dados = new NutriTime.Data.RefeicaoRepository(NutriTime.App.DataBase); 

              //TODO:Manipulação dos dados

              Toast.MakeText(context, "Ok", ToastLength.Long).Show();
          }
          catch (Exception e)
          {
              Toast.MakeText(context, e.ToString(), ToastLength.Long).Show();
          }
      }        
  }
}

SQLite Configuration (.Droid project):

[assembly: Dependency(typeof(NutriTime.Droid.Data.SQLite_Android))]

namespace NutriTime.Droid.Data
{
  public class SQLite_Android : ISQLite
  {
     private string _diretorioDB;
     public string DiretorioDB
     {
         get
         {
             if (string.IsNullOrEmpty(_diretorioDB))
             {
                 _diretorioDB = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
             }
             return _diretorioDB;
         }
     }
     private SQLite.Net.Interop.ISQLitePlatform _plataforma;
     public ISQLitePlatform Plataforma
     {
        get
        {
            if (_plataforma == null)
            {
                _plataforma = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
            }
            return _plataforma;
      }
    }
  }
}

SQLite Abstraction Interface (Portable project):

namespace NutriTime.Interfaces
{
  public interface ISQLite
  {
      string DiretorioDB { get; }
      ISQLitePlatform Plataforma { get; }
  }
}

Class responsible for creating the database (Portable project):

namespace NutriTime.Data
{
  public class DBConnection
  {
      private SQLiteConnection _con;
      public DBConnection()
      {
         var config = DependencyService.Get<ISQLite>();
         _con = new SQLite.Net.SQLiteConnection(config.Plataforma, System.IO.Path.Combine(config.DiretorioDB, "nutritimedb.db3"));

         _con.CreateTable<PacienteModel>();
         _con.CreateTable<RefeicaoModel>();
         _con.CreateTable<AlimentoModel>();
         _con.CreateTable<ItemModel>();

       }
       public SQLiteConnection GetConnection()
       {
          return _con;
       }
   }
}

What is the best way to access SQLite data within the specific Android project?

    
asked by anonymous 28.09.2016 / 22:37

1 answer

1

After more research I realized where I was wrong.

In the BootReceiver class, where you have

//A linha abaixo gera a Exception
          NutriTime.Data.RefeicaoRepository dados = new NutriTime.Data.RefeicaoRepository(NutriTime.App.DataBase);

Replaces with

            SQLite.Net.Interop.ISQLitePlatform plataforma = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
            string diretorioDB = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            using (SQLiteConnection _con = new SQLite.Net.SQLiteConnection(plataforma, System.IO.Path.Combine(diretorioDB, "nutritimedb.db3")))
            {
               //Manipulação de dados                      
            }
Basically, from what I understand, I was trying to access the database through the data access classes present in the PCL project (including the App class) that require Xamarin.Forms to boot.

If you feel free to explain better or supplement / correct my answer, feel free to do so. Thankful.

Here's where I noticed my error: link

    
19.10.2016 / 21:16