質問

目標:XML データからデータベースを更新する

プロセス:

  • トランザクションの開始
  • 消去 テーブルの既存のすべての行
  • 解析された XML の各主要要素ごと 入れる メインテーブルに行を追加し、PK を取得します
  • メイン要素の各子ごとに 入れる 前のステップからの FK を提供する 2 番目のテーブルに記録します
  • トランザクションをコミットする

データベース操作に関してはかなり標準的なものです。問題は、CRUD 操作が内部で実行されないことです。 ContentProvider むしろ使用しています ContentResolver たとえば、挿入は次のようになります resolver.insert(CONTENT_URI, contentValues). 。ContentResolver API にはトランザクションに関連するものがないようで、使用できません bulkInsert 2つのテーブルに断続的に挿入しているので(さらに、 delete トランザクション内でも同様です)。

カスタマイズしたものを登録しようと思ったのですが、 ContentProvider を使用してリスナーとして registerContentObserver しかしそれ以来 ContentResolver#acquireProvider メソッドが隠されています。正しい参照を取得するにはどうすればよいですか?

私は運が悪いのでしょうか?

役に立ちましたか?

解決

私はGoogleのI / Oアプリケーションのソースコードに、彼らはContentProviderapplyBatch()メソッドをオーバーライドし、その中のトランザクションを使用することを見てきました。それで、あなたはContentProviderOperation秒のバッチを作成し、getContentResolver().applyBatch(uri_authority, batch)を呼び出します。

私はそれがどのように動作するかを確認するには、このアプローチを使用することを計画しています。誰がそれをしようとした場合、私は興味があります。

他のヒント

kaciulaにより述べたように、ContentProviderOperationを使用して、アンドロイド2.1以降ではなくきれいにトランザクションベースのマルチテーブルの挿入を行うことが可能である。

あなたはContentProviderOperationオブジェクトを構築する場合、

、あなたは.withValueBackReference(フィールド名、refNr)を呼び出すことができます。動作はapplyBatchを用いて適用された場合、結果はContentValuesはそれが注入整数を持つことになり、インサート()呼び出しが供給されているオブジェクトということです。整数フィールド名文字列にキー止めされ、その値は、以前refNrによって索引付け、ContentProviderOperation適用のContentProviderResultから取得されます。

以下のサンプルコードを参照してください。試料中に、行が表1に挿入され、簡潔にするため、表2の行を挿入するとき(この場合は「1」に)、得られたIDをその値として使用され、のContentProviderは、データベースに接続されていません。 ContentProviderでは、トランザクション処理を追加するために適しているであろうプリントアウトがあります。

public class BatchTestActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<ContentProviderOperation> list = new
            ArrayList<ContentProviderOperation>();

        list.add(ContentProviderOperation.
            newInsert(BatchContentProvider.FIRST_URI).build());
        ContentValues cv = new ContentValues();
        cv.put("name", "second_name");
        cv.put("refId", 23);

        // In this example, "refId" in the contentValues will be overwritten by
        // the result from the first insert operation, indexed by 0
        list.add(ContentProviderOperation.
            newInsert(BatchContentProvider.SECOND_URI).
            withValues(cv).withValueBackReference("refId", 0).build());

        try {
            getContentResolver().applyBatch(
                BatchContentProvider.AUTHORITY, list);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }
    }
}

public class BatchContentProvider extends ContentProvider {

    private static final String SCHEME = "content://";
    public static final String AUTHORITY = "com.test.batch";

    public static final Uri FIRST_URI =
        Uri.parse(SCHEME + AUTHORITY + "/" + "table1");
    public static final Uri SECOND_URI =
        Uri.parse(SCHEME + AUTHORITY + "/" + "table2");


    public ContentProviderResult[] applyBatch(
        ArrayList<ContentProviderOperation> operations)
            throws OperationApplicationException {
        System.out.println("starting transaction");
        ContentProviderResult[] result;
        try {
            result = super.applyBatch(operations);
        } catch (OperationApplicationException e) {
            System.out.println("aborting transaction");
            throw e;
        }
        System.out.println("ending transaction");
        return result;
    }

    public Uri insert(Uri uri, ContentValues values) {
        // this printout will have a proper value when
        // the second operation is applied
        System.out.println("" + values);

        return ContentUris.withAppendedId(uri, 1);
    }

    // other overrides omitted for brevity
}

すべての権利 - これはあてもなくディングルしません:私は考えることができる唯一の方法は、URLベースのクエリ要求としてStartTransactionメソッドとENDTRANSACTIONをコーディングすることです。 ContentResolver.query(START_TRANSACTION, null, null, null, null)ような何か。そして、登録URLの呼び出しに基づいてContentProvider#queryで開始または終了トランザクション

あなたはマルチプロセス=「true」またはプロセスとプロバイダのプロセスを制御することができます=「」<のhref = "のhttp://開発

あなたは、コンテンツプロバイダオブジェクト自体(同じプロセスであれば、ヒントの実装を取得することができます.android.com /ガイド/トピック/マニフェスト/プロバイダelement.html」REL = "" nofollowを> http://developer.android.com/guide/topics/manifest/provider-element.html の)使用閉じて、データベースを削除し、また保存()と密接にカスタムトランザクションクラスのインスタンスを返すことができますリセット(のような余分な機能を提供することができ、あなたのプロバイダ実装にキャストすることができますContentProviderClient.getLocalContentProvider())()メソッド。

public class Transaction {
    protected Transaction (SQLiteDatabase database) {
        this.database = database;
        database.beginTransaction ();
    }

    public void save () {
        this.database.setTransactionSuccessful ();
    }

    public void close () {
        this.database.endTransaction ();
    }

    private SQLiteDatabase database;
}

public Transaction createTransaction () {
    return new Transaction (this.dbHelper.getWritableDatabase ());
}

するとます:

ContentProviderClient client = getContentResolver ().acquireContentProviderClient (Contract.authorityLocal);
Transaction tx = ((LocalContentProvider) client.getLocalContentProvider ()).createTransaction ();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top