gpt4 book ai didi

android - 游标变量重用

转载 作者:行者123 更新时间:2023-11-29 00:03:53 25 4
gpt4 key购买 nike

我在 while 循环中使用游标对我的数据库执行多个查询,因此每次 Content Resolver 都返回新的游标对象实例。我不确定在循环的每次迭代中我应该重用光标的正确方法:

  1. 执行完所有操作后关闭一次

    Cursor c;
    try {
    while(condition) {
    c = Context.getContentResolver().query(...);
    // fetching values
    }
    } finally {
    if (c != null) {
    c.close()
    }
    }
  2. 在每次迭代结束时关闭它

    Cursor c;
    try {
    while(condition) {
    c = Context.getContentResolver().query(...);
    // fetching values
    if (c != null) {
    c.close()
    }
    }
    } finally {
    if (c != null) {
    c.close()
    }
    }
  3. 在 while 循环中创建新的游标变量

    while(condition) {
    Cursor c = Context.getContentResolver().query(...);
    try {
    // fetching values
    } finally {
    if (c != null) {
    c.close()
    }
    }
    }

?

最佳答案

  • 情况 1 不好,因为您在循环中分配了一个新游标,而您只能关闭最后分配的游标。

  • 情况 2 和情况 3 非常相似,但我更喜欢情况 3,因为在情况 2 中,您可以意外地跳出循环,而在情况 3 中,您将光标保持在循环范围内,循环可以继续运行。

关于android - 游标变量重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40952466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com