Man*_*hot 11 sqlite android cursor
我应该使用以下哪两个来确保所有游标都关闭?
Cursor c = getCursor();
if(c!=null && c.getCount()>0){
try{
// read values from cursor
}catch(..){}
finally{
c.close();
}
}//end if
OR
Cursor c = getCursor();
try{
if(c!=null && c.getCount()>0){
// read values from cursor
}//end if
}catch(..){
}finally{
c.close();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
几个问题:
1.我们需要在计数为0的游标上调用close()吗?
因为在第一个习语的情况下,将永远不会调用close().它假定对于没有元素的游标,永远不会打开游标.这是一个有效的假设吗?
请指教.
Sky*_*ton 13
两者都没有,但第二个是最接近的.
我会用:
Cursor c = getCursor();
try {
if(c!=null && c.getCount()>0){
// do stuff with the cursor
}
}
catch(..) {
//Handle ex
}
finally {
if(c != null) {
c.close();
}
}
Run Code Online (Sandbox Code Playgroud)
...或者如果您希望光标经常为空,您可以稍微转过头:
Cursor c = getCursor();
if(c != null) {
try {
if(c.getCount()>0) {
// do stuff with the cursor
}
}
catch(..) {
//Handle ex
}
finally {
c.close();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2530 次 |
| 最近记录: |