마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

Content provider에서 insert()를 구현하는데 notifyChange()하기

0 추천

udacity에서 Developing Android Apps를 공부하고 있습니다. Content Provider에서 insert()를 구현하는데 궁금한 점이 있네요. 예제를 보시면 실제 DB에 insert한 다음 생성한 returnUri와 insert()로 넘어온 이전 uri 이렇게 두 URI가 존재하게 되는데요. 강좌에서는 마지막에 getContext().getContentResolver().notifyChange()할 때 returnUri가 아닌 이전 uri를 사용해야 한다고 합니다. 'Note that we must use the past in URI, and not the return URI, as that will not correctly notify our cursors of the change' 이 부분이 이해가 되지 않는데요. 그냥 returnUri를 넣고 Test를 돌려도 통과되거든요? 꼭 이전 uri를 사용해야 하는 이유가 있을까요?

@Override

public Uri insert(Uri uri, ContentValues values) {

  final SQLiteDatabase db = mOpenHelper.getWritableDatabase();

  final int match = sUriMatcher.match(uri);

  Uri returnUri;

  switch (match) {
    case WEATHER: {
      normalizeDate(values);
      long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, values);
      if ( _id > 0 )
        returnUri = WeatherContract.WeatherEntry.buildWeatherUri(_id);
      else
        throw new android.database.SQLException("Failed to insert row into " + uri);
      break;
    }
    default:
      throw new UnsupportedOperationException("Unknown uri: " + uri);
  }
  getContext().getContentResolver().notifyChange(uri, null);
  return returnUri;
}

 

최강로떼 (320 포인트) 님이 2015년 8월 25일 질문

1개의 답변

0 추천

'Note that we must use the past in URI, and not the return URI, as that will not correctly notify our cursors of the change'

 

이 문장 자체에 질문에 대한 답이 이미 있는 것 아닌가요?

as that will not correctly notify our cursors of the change

보다 복잡한 여러 상황에 대해서 테스트 해보시는 게 어떨까요?

 

cc1232 (35,280 포인트) 님이 2015년 8월 26일 답변
...