Phill Wiggins
Phill Wiggins

Reputation: 2927

SQLite-net-pcl not working properly on Android 7.0 (Xamarin)

SQLite-net-pcl seem's to work on all platforms except Android 7.0.

So far on Android 7.0 from a fresh install the application will crash due to an SQLException with the issue that it can't create a PK. If I uninstall the applications storage I.e. wipe the app from application settings, the app then works perfectly.

All other platforms seem to install instantly. See for youself https://play.google.com/store/apps/details?id=com.purewowstudio.fivestartips

I cannot seem to fix this. Worst of all, my iOS which shares a DB PCL doesn't load at all.

Does anyone know how to fix this?

Code example:-

Object

using System;

namespace App.LocalObjects
{
[Preserve(AllMembers = true)]
public class LocalArticle
{

    [SQLite.PrimaryKey, SQLite.Column("articleObjectId")]
    public string objectId { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime updatedAt { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Url { get; set; }
    public int Status { get; set; }

    public LocalArticle()
    {
        this.objectId = " ";
        this.Title = " ";
        this.Description = " ";
        this.Url = " ";
    }
}
}

Database

using System;
using System.Linq;
using System.Collections.Generic;
using SQLite;
using App.LocalObjects;

namespace App.DataLayer
{

public class Database
{
    static object locker = new object();
    public SQLiteConnection database;
    public string path;

    public Database(SQLiteConnection conn)
    {
        database = conn;
        database.CreateTable<LocalArticle>();
    }

public int SaveLocalArticleItem(LocalArticle item)
    {
        lock (locker)
        {

            LocalArticle article = database.Table<LocalArticle>().FirstOrDefault(x => x.objectId == item.objectId);

            if (article != null && article.objectId.Equals(item.objectId) && !article.updatedAt.Equals(item.updatedAt))
            {
                database.Update(item);

                return item.ID;
            } else {
                return database.Insert(item);
            }
        }
    }

Code to initialize DB:

using System;
using System.IO;
using Android.App;
using App.BusinessLayer.Managers;
using App.BusinessLayer.ParseObjects;
using App.Utils;
using Android.Content;
using Com.Nostra13.Universalimageloader.Core;
using Com.Nostra13.Universalimageloader.Core.Assist;
using Android.Graphics;
using Com.Nostra13.Universalimageloader.Cache.Memory.Impl;
using Com.OneSignal;
using Parse;
using SQLite;

namespace App.Droid
{
[Application(LargeHeap = true)]
public class App : Application
{

    public static App Current { get; private set; }
    public ModelManager modelManager { get; set; }
    private SQLiteConnection conn;
    private ImageLoader imageLoader;

public App(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
        : base(handle, transfer)
    {
        Current = this;
    }

public override void OnCreate()
    {
        base.OnCreate();

        SetupParse();
        SetupDB();
}

private void SetupParse()
    {
        ParseObject.RegisterSubclass<ParseArticle>();

        ParseClient.Initialize(new ParseClient.Configuration
        {
            ApplicationId = Constants.ParseApplicationID,
            Server = Constants.ParseServer
        });


    }

    private void SetupDB()
    {

        var sqliteFilename = Constants.DBName;
        string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        var path = System.IO.Path.Combine(libraryPath, sqliteFilename);
        conn = new SQLiteConnection(path);

        modelManager = new ModelManager(conn);
}

Here's what my project is currently referencing:-

enter image description here

Upvotes: 0

Views: 4325

Answers (2)

vebs
vebs

Reputation: 345

i have same issue. i install this package for sqlite and set targeted version to android 8(oreo) my application works again

Upvotes: 0

hvaughan3
hvaughan3

Reputation: 11105

Android N began actually enforcing rules around accessing the SQLite libraries that come with Android. Therefore any app (or app library) that was previously directly accessing SQLite without going through the special Java wrapper was already breaking the rules technically, the reason it was not a problem was because Android never really enforced those rules. Read more about that here

Now that Android N does enforce those rules, it causes the crash to occur. I am guessing that you are using the SQLite.Net-PCL library for accessing your SQLite DB. If you look here there is an open issue for that exact problem that the library creators never fixed.

We ran into the same issue and have since switched to the SQLite-Net-PCL library (notice the dash instead of the period) which works the exact same way and has an almost identical API. A link for that library can be found here.

Upvotes: 1

Related Questions