如何实现 CrossProcessCursor 兼容的通用自定义 CursorWrapper

发布于 2024-11-14 07:36:20 字数 833 浏览 5 评论 0原文

我设法创建了一个可用的 CursorWrapper,但当我想跨进程使用 ContentProvider 时,我遇到了困难。

这些帖子甚至展示了如何实现 CrossProcessCursor 接口,特别是硬且未记录的 fillWindow() 方法:

问题是我可以只考虑 fillWindow() 的实现,该实现处理包含 Blob、字符串或...的所有列的游标,但不考虑这些的混合,正如现实世界的光标所必然具有的那样。这里真正的问题是缺少 getType() 函数(仅从 v11 开始存在),getRaw() /putRaw() 只会复制二进制文件而不会抱怨。您如何处理这个而不在返回的游标值中引起不需要的转换

I manage to create a working CursorWrapper, but get stuck when I want to use my ContentProvider across processes.

These posts even show how to implement the CrossProcessCursor interface, notably the hard and undocumented fillWindow() method:

The problem is that I can think only of an implementation of fillWindow() that deals with a cursor with either all columns containing Blobs, or Strings, or ..., but not a mix of those, as a real-world cursor is bound to have. The real issue here is a lack of a getType() function (exists only from v11 on), or a getRaw()/putRaw() that would just copy binary without complaining. How do you deal with this without incurring unwanted conversions in your returned cursor values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错々过的事 2024-11-21 07:36:20

我现在已经按如下方式实现了它,但感觉不是正确的做法或稳健的做法:

/**
 * Copy data from cursor to CursorWindow
 * @param position start position of data
 * @param window
 */
public void fillWindow(int position, CursorWindow window) {
    if (position < 0 || position > getCount()) {
        return;
    }
    window.acquireReference();
    try {
        int oldpos = this.getPosition();
        this.moveToPosition(position - 1);
        window.clear();
        window.setStartPosition(position);
        int columnNum = getColumnCount();
        window.setNumColumns(columnNum);
        while (moveToNext() && window.allocRow()) {            
            for (int i = 0; i < columnNum; i++) {

                //int type = getType(i);//only from v11 on

                try {
                    String field7 = getString(i);
                    if (field7 != null) {
                        try {
                                if (!window.putLong(new Long(field7), this.getPosition(), i)) {
                                    if (!window.putDouble(new Double(field7), this.getPosition(), i)) {
                                        if (!window.putString(field7, this.getPosition(), i)) {
                                            window.freeLastRow();
                                            break;
                                        }
                                    }
                                }
                            } catch (NumberFormatException e) {
                                try {
                                    if (!window.putDouble(new Double(field7), this.getPosition(), i)) {
                                        if (!window.putString(field7, this.getPosition(), i)) {
                                            window.freeLastRow();
                                            break;
                                        }
                                    }
                                } catch (NumberFormatException e1) {
                                    if (!window.putString(field7, this.getPosition(), i)) {
                                        window.freeLastRow();
                                        break;
                                    }
                                }
                            }
                    } else {
                        if (!window.putNull(this.getPosition(), i)) {
                            window.freeLastRow();
                            break;
                        }
                    }
                } catch (SQLiteException e7) {

                    try {
                        byte[] field1 = getBlob(i);
                        if (field1 != null) {
                            if (!window.putBlob(field1, this.getPosition(), i)) {
                                window.freeLastRow();
                                break;
                            }
                        } else {
                            if (!window.putNull(this.getPosition(), i)) {
                                window.freeLastRow();
                                break;
                            }
                        }
                    } catch (SQLiteException e1) {
                        throw e1;
                    }
                }
            }
        }

        this.moveToPosition(oldpos);
    } catch (IllegalStateException e){
        // simply ignore it
    } finally {
        window.releaseReference();
    }
}

I've implemented it as follows for now, but it does not feel like the right thing to do or robust:

/**
 * Copy data from cursor to CursorWindow
 * @param position start position of data
 * @param window
 */
public void fillWindow(int position, CursorWindow window) {
    if (position < 0 || position > getCount()) {
        return;
    }
    window.acquireReference();
    try {
        int oldpos = this.getPosition();
        this.moveToPosition(position - 1);
        window.clear();
        window.setStartPosition(position);
        int columnNum = getColumnCount();
        window.setNumColumns(columnNum);
        while (moveToNext() && window.allocRow()) {            
            for (int i = 0; i < columnNum; i++) {

                //int type = getType(i);//only from v11 on

                try {
                    String field7 = getString(i);
                    if (field7 != null) {
                        try {
                                if (!window.putLong(new Long(field7), this.getPosition(), i)) {
                                    if (!window.putDouble(new Double(field7), this.getPosition(), i)) {
                                        if (!window.putString(field7, this.getPosition(), i)) {
                                            window.freeLastRow();
                                            break;
                                        }
                                    }
                                }
                            } catch (NumberFormatException e) {
                                try {
                                    if (!window.putDouble(new Double(field7), this.getPosition(), i)) {
                                        if (!window.putString(field7, this.getPosition(), i)) {
                                            window.freeLastRow();
                                            break;
                                        }
                                    }
                                } catch (NumberFormatException e1) {
                                    if (!window.putString(field7, this.getPosition(), i)) {
                                        window.freeLastRow();
                                        break;
                                    }
                                }
                            }
                    } else {
                        if (!window.putNull(this.getPosition(), i)) {
                            window.freeLastRow();
                            break;
                        }
                    }
                } catch (SQLiteException e7) {

                    try {
                        byte[] field1 = getBlob(i);
                        if (field1 != null) {
                            if (!window.putBlob(field1, this.getPosition(), i)) {
                                window.freeLastRow();
                                break;
                            }
                        } else {
                            if (!window.putNull(this.getPosition(), i)) {
                                window.freeLastRow();
                                break;
                            }
                        }
                    } catch (SQLiteException e1) {
                        throw e1;
                    }
                }
            }
        }

        this.moveToPosition(oldpos);
    } catch (IllegalStateException e){
        // simply ignore it
    } finally {
        window.releaseReference();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文