티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Closing the database in a ContentProvider

ContentProvider에서 데이터베이스 닫기

 문제 내용 

This week I've been learning all about ContentProvider and using the SQLiteOpenHelper class to manage the creation and upgrading of the database inside of a provider. Specifically, I've been reading through the NotePad example from the sdk's samples directory.

이번 주에는 ContentProvider 및 SQLiteOpenHelper 클래스를 사용하여 공급자 내에서 데이터베이스의 생성 및 업그레이드를 관리하는 방법에 대해 배웠습니다. 구체적으로는 SDK 샘플 디렉토리의 NotePad 예제를 읽어보았습니다.

 

Now, I can see that SQLiteOpenHelper has a close() method. I'm aware that leaving idle databases open is bad practice and can cause memory leaks and whatnot (unless this discussion is headed in the right direction). If I were using the class in an Activity, then I would simply call close() in the onDestroy() method, but as far as I know, ContentProvider does not have the same life cycle that activities do. The code for NotePad never seems to call close(), so I would like to assume that it is handled by SQLiteOpenHelper or some other piece of the puzzle, but I'd really like to know for sure. I don't really trust the sample code that much, either...

지금은 SQLiteOpenHelper 클래스에 close() 메서드가 있다는 것을 알 수 있습니다. 유휴 상태의 데이터베이스를 열어 둔 채로 두면 메모리 누수 및 기타 문제가 발생할 수 있다는 것을 알고 있습니다(이 논의가 올바른 방향으로 나아가는 것이라면). 클래스를 Activity에서 사용하고 있다면 onDestroy() 메서드에서 단순히 close()를 호출하면 되지만, ContentProvider에는 Activity와 같은 라이프 사이클이 없다는 것을 알고 있습니다. NotePad의 코드에서는 close()를 호출하는 것 같지 않아서, SQLiteOpenHelper 또는 기타 퍼즐 조각으로 처리되는 것으로 가정할 수 있지만, 확실하게 알고 싶습니다. 샘플 코드를 그다지 신뢰하지 않습니다...

 

Question summary: When should we close the database in a provider, if at all?

질문 요약: ContentProvider에서 데이터베이스를 언제, 그리고 닫아야 하는가?

 

 

 

 높은 점수를 받은 Solution 

According to Dianne Hackborn (Android framework engineer) there is no need to close the database in a content provider.

안드로이드 프레임워크 엔지니어인 Dianne Hackborn에 따르면 ContentProvider에서 데이터베이스를 닫을 필요가 없다고 합니다.

 

A content provider is created when its hosting process is created, and remains around for as long as the process does, so there is no need to close the database -- it will get closed as part of the kernel cleaning up the process's resources when the process is killed.

콘텐트 프로바이더는 해당하는 프로세스가 생성될 때 생성되며, 해당 프로세스가 종료될 때까지 유지되므로 데이터베이스를 닫을 필요가 없습니다. 프로세스가 종료될 때 커널이 자원 정리 과정에서 데이터베이스가 닫히게 됩니다.

 

Thanks @bigstones for pointing this out.

@bigstones 님께서 이것을 지적해주셔서 감사합니다.

 

 

 

출처 : https://stackoverflow.com/questions/4547461/closing-the-database-in-a-contentprovider

반응형
댓글
공지사항
최근에 올라온 글