2011-01-11 9 views
35

withValueBackReference의 정확한 의미를 이해할 수 없습니다.withValueBackReference의 의미는 무엇입니까?

이 메서드를 사용하여 예제 코드 (예 : 새 연락처를 추가하는 코드)를 읽었을 때 backReference 값을 0으로 지정했습니다. 이것이 의미하는 바는 무엇입니까?

문서는 말한다 : 뒷면 참조에서

열 값이 withValues ​​(ContentValues)에 지정된 값

+0

이 토론은 SO에서 발견되었습니다 .- 목표가있는 경우에 withValueBackReference 메소드 사용에 대해 이야기합니다. * 하나의 작업으로 마스터 레코드와 세부 레코드를 둘 다 저장하는 것입니다. 그러나, 나는 아직도 0의 뒷쪽 참조 값이 여기에 어떻게 나타나는지 이해하지 못한다! http://stackoverflow.com/questions/3224857/master-detail-using-contentresolver-applybatch – curioustechizen

답변

155

이 질문은 콘텐츠 공급자에 일괄 작업에 관한보다 우선합니다. 이 예는 this related question에서 수정되었습니다.

제하여 수행하는 동작들의리스트를 작성 작업의 배치를 만들기
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); 

applyBatch은 다음 방법을 사용하여 콘텐츠 제공자에 적용.

ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations); 

이것이 기본 개념이므로 적용 해 보겠습니다. Foo 레코드의 uris와 Bar라는 하위 레코드를 처리하는 콘텐츠 제공자가 있다고 가정 해 보겠습니다.

내용 : //com.stackoverflow.foobar/foo

내용 : 이제 우리는 단지 2를 삽입 할 수 있습니다 들어

//com.stackoverflow.foobar/foo/#/bar new Foo는 "Foo A"와 "Foo B"를 기록했으며 여기에 예제가 있습니다. 여기

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

//add a new ContentProviderOperation - inserting a FOO record with a name and a decscription 
operations.add(ContentProviderOperation.newInsert(intent.getData()) 
    .withValue(FOO.NAME, "Foo A") 
    .withValue(FOO.DESCRIPTION, "A foo of impeccable nature") 
    .build()); 

//let's add another 
operations.add(ContentProviderOperation.newInsert(intent.getData()) 
    .withValue(FOO.NAME, "Foo B") 
    .withValue(FOO.DESCRIPTION, "A foo of despicable nature") 
    .build()); 

ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations); 

아무것도 특별한, 우리는 우리의 목록이 개 ContentProviderOperation 항목을 추가하고 콘텐츠 제공자 목록을 적용하고 있습니다. 결과 배열은 방금 삽입 한 새 레코드의 ID로 채워집니다.

우리는 비슷한 것을하고 싶지만 한 번의 일괄 처리로 콘텐츠 공급자에 하위 레코드를 추가하려고합니다. 우리는 방금 만든 Foo 레코드에 하위 레코드를 첨부하려고합니다. 문제는 배치가 실행되지 않았기 때문에 부모 Foo 레코드의 ID를 알 수 없다는 것입니다. 이것이 바로 withValueBackReference가 우리에게 도움이되는 곳입니다. 다음의 예제를 보자 :

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

//add a new ContentProviderOperation - inserting a Foo record with a name and a decscription 
operations.add(ContentProviderOperation.newInsert(intent.getData()) 
    .withValue(FOO.NAME, "Foo A") 
    .withValue(FOO.DESCRIPTION, "Foo of impeccable nature") 
    .build()); 

//let's add another 
operations.add(ContentProviderOperation.newInsert(intent.getData()) 
    .withValue(FOO.NAME, "Foo B") 
    .withValue(FOO.DESCRIPTION, "Foo of despicable nature") 
    .build()); 

//now add a Bar record called [Barbarella] and relate it to [Foo A] 
operations.add(ContentProviderOperation.newInsert(intent.getData() 
    .buildUpon() 
    .appendPath("#") /* We don't know this yet */ 
    .appendPath("bar") 
    .build()) 
.withValueBackReference (BAR.FOO_ID, 0) /* Index is 0 because Foo A is the first operation in the array*/ 
.withValue(BAR.NAME, "Barbarella") 
.withValue(BAR.GENDER, "female") 
.build()); 

//add a Bar record called [Barbarian] and relate it to [Foo B] 
operations.add(ContentProviderOperation.newInsert(intent.getData() 
    .buildUpon() 
    .appendPath("#") /* We don't know this yet */ 
    .appendPath("bar") 
    .build()) 
.withValueBackReference (BAR.FOO_ID, 1) /* Index of parent Foo B is 1*/ 
.withValue(BAR.NAME, "Barbarian") 
.withValue(BAR.GENDER, "male") 
.build()); 

ContentProviderResult[] results = this.getContentResolver().applyBatch(FooBar.AUTHORITY, operations); 

는 그래서 withValueBackReference() 메소드는 우리가 그들에게 연관하려는 부모의 ID를 알기도 전에 우리가 관련 레코드를 삽입 할 수 있습니다. 역 참조 인덱스는 단순히 우리가 찾고자하는 id를 반환 할 연산의 인덱스입니다. id를 포함 할 것으로 예상되는 결과에 대해 생각하는 것이 더 쉽습니다. 예를 들어 results[1]은 "Foo B"에 대한 ID를 포함하므로 "Foo B"에 대한 참조를 뒷받침하는 색인은 1입니다.

+4

그건 완벽한 설명이었습니다. 엄청 고마워! 나는 어떻게하면 한 번 이상 upvote 수 있습니다. – curioustechizen

+3

이것은 내가 본 backReferences에 대한 유일한 설명 중 하나 인 금이다. 그것은 내 애플 리케이션에 구현있어 - 덕분에 –

+0

최고의 답변도, 정말 고마워, 당신은 단지 5 응답에 넣을 수있는 스타 플래그를 가지고 좋은 것입니다 :) 당신은 확실히 자신의 하나! – Goofyahead