猿问

SQLiteOpenHelper - 无法访问已处置的对象

我有一个我的SQLiteOpenHelper班级,它被写成单身人士。我应该注意,我不是在 Java 中执行此操作,而是使用 Xamarin.Android C# 编写此程序。


这是该课程的一个片段:


public class DatabaseHelper : SQLiteOpenHelper

{

    private static readonly string TAG = typeof(DatabaseHelper).FullName;


    private static readonly string _databaseName = "istockdb";


    private static readonly int _databaseVersion = 32;


    private static DatabaseHelper _instance;


    private Context _context;


    private DatabaseHelper(Context context) : base(context, _databaseName, null, _databaseVersion)

    {

        _context = context;

    }


    [MethodImpl(MethodImplOptions.Synchronized)]

    public static DatabaseHelper Instance(Context context)

    {

        // *** Use the application context, which will ensure that ***

        // *** the Activity's context is not accidentally leaked ***

        return _instance ?? (_instance = new DatabaseHelper(context.ApplicationContext));

    }


}

所以我有DatabaseHelper一个单身人士,在活动和服务中这样使用:


服务:


[Service(Name=Text.MobileBackgroundHbService, Enabled = true, Exported = true), IntentFilter(new []{Intents.SyncHeartbeats})]

public class BGHeartbeatService : BaseIntentService

{

    public BGHeartbeatService()

    {

        this._database = DatabaseHelper.Instance(Application.Context);

    }


    protected override void OnHandleIntent(Intent intent)

    {

        if (this._database == null)

            this._database = DatabaseHelper.Instance(Application.Context);


        if (intent.Action.Equals(Intents.SyncHeartbeats)) SyncHeartbeatRecords();


        var broadcastIntent = new Intent(Intents.MobileRefresh);

        SendBroadcast(broadcastIntent);

    }


}


梵蒂冈之花
浏览 121回答 1
1回答

隔江千里

事实证明,我的单例实例DatbaseHelper没有被处理掉。实际上发生的事情是我正在处理由辅助方法中SQLiteDatabase的 from 使用的对象。DatbaseHelper要真正解决这个问题,我所要做的就是改变:/// <summary>/// Inserts a Heartbeat record into local DB./// </summary>/// <param name="heartbeat"></param>/// <returns></returns>public long InsertHeartbeat(Heartbeat heartbeat){&nbsp; &nbsp; if (heartbeat == null) return -2L;&nbsp; &nbsp; // This using() statement is causing the disposal&nbsp; &nbsp; using (var db = this.WritableDatabase)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var id = -3L;&nbsp; &nbsp; &nbsp; &nbsp; db.BeginTransactionNonExclusive();&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var cv = GetContentValues(heartbeat);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = db.Insert(DatabaseSchema.Heartbeat.TableName, null, cv);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.SetTransactionSuccessful();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: Document Exception&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Util.Tools.Bark(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; finally&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.EndTransaction();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }}到:/// <summary>/// Inserts a Heartbeat record into local DB./// </summary>/// <param name="heartbeat"></param>/// <returns></returns>public long InsertHeartbeat(Heartbeat heartbeat){&nbsp; &nbsp; if (heartbeat == null) return -2L;&nbsp; &nbsp; // This is no longer initialized in a using() statement&nbsp; &nbsp; var db = this.WritableDatabase;&nbsp; &nbsp; &nbsp; &nbsp; var id = -3L;&nbsp; &nbsp; &nbsp; &nbsp; db.BeginTransactionNonExclusive();&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var cv = GetContentValues(heartbeat);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = db.Insert(DatabaseSchema.Heartbeat.TableName, null, cv);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.SetTransactionSuccessful();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: Document Exception&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Util.Tools.Bark(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; finally&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.EndTransaction();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return id;}概括:通过在辅助方法内的语句内初始化我的SQLiteDatabase db对象,我处理了我需要的对象。using()SQLiteDatabaseDatabaseHelper
随时随地看视频慕课网APP
我要回答