在android中通过Loaders插入记录 [英] Inserting records via Loaders in android

查看:15
本文介绍了在android中通过Loaders插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题将是一个后续问题 内容解析器与游标加载器答案清楚地说明了如何使用 Content Resolver 将记录插入到 sqlite Db 中.我的问题如下:

This question would be a follow up from the one asked Content Resolver vs Cursor Loader Where the answer clearly states how to insert records into a sqlite Db using a Content Resolver . My question is the following :

  1. 我可以使用加载器(普通加载器)来实现这个功能吗?

例如:

    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
            switch (id) {

    case AppConstants.LOADER_ADD_FAV_PHONE_NUM:

        ContentValues values = new ContentValues();
        values.put(DBHelper.TM_DB_COLUMN_PHONE_NUMBER,
                directionOrder);
        values.put(TransitMeDBHelper.TM_DB_COLUMN_NAME_ID, selectionArgs[0]);
        Uri insertFavStop =getContentResolver().insert(TransitMeContentProvider.CONTENT_URI_INSERT_FAV_PHONE,
                        values);
        break;
    }

return null;
}

这将在主 UI 线程或工作线程中运行功能.我知道我不会收到回调,因为内容解析器将返回一个 URI 而不是包含数据的游标.

Would this run the functionality in the Main UI thread or a worker Thread . I understand that i would not receive callbacks for this as the content resolver will return a URI instead of the cursor containing data .

我尝试运行此代码,但它的行为有点奇怪.. 记录了重复的插入并且它不一致,有人可以告诉我一些正确的资源,它使用加载器来执行插入操作(我已经看到了许多带有查询示例的示例它就像一个魅力:) )

I tried running this code but it behaves kind of wierd .. Duplicate inserts are recorded and its not consistient , Could someone show me to some right resource which uses a Loader to do Insert operations ( I have seen many examples with query examples and it works like a charm :) )

感谢您的宝贵时间.

推荐答案

我可以使用加载器(普通加载器)来实现这个功能吗?

Can i use a loader(Normal Loader) to implement this functionality ?

您不能使用默认的 CursorLoader 插入数据.如果您实现自己的 Loader 您可以可能使用 loadInBackground 方法进行插入,但这最多会使您的代码尴尬(我不知道它是否真的有效).

You can't use a default CursorLoader to insert data. If you implement your own Loader you could probably use the loadInBackground method to do the insert but that will make your code awkward at best(and I don't know if it will actually work).

这会在主 UI 线程或工作线程中运行功能吗线 .我知道我不会收到回调,因为内容解析器将返回一个 URI 而不是光标包含数据.

Would this run the functionality in the Main UI thread or a worker Thread . I understand that i would not receive callbacks for this as the content resolver will return a URI instead of the cursor containing data .

加载器回调将在实现它们的线程上运行(最有可能是 UI 主线程),因此您在主 UI 线程上执行 getContentResolver().insert()阻塞主 UI 线程的可能性.

The loader callbacks will run on the thread where they are implemented(the UI main thread most likely) so you're doing the getContentResolver().insert() on the main UI thread with the possibility of blocking the main UI thread.

我尝试运行此代码,但它的行为有点奇怪.. 重复插入被记录并且它不一致,

I tried running this code but it behaves kind of wierd .. Duplicate inserts are recorded and its not consistient ,

你测试过onCreateLoader方法被调用了多少次吗?

Have you tested how many times is the onCreateLoader method called?

谁能告诉我一些正确的资源,它使用加载器来做插入操作(我见过很多带有查询示例和它就像一个魅力:) )

Could someone show me to some right resource which uses a Loader to do Insert operations ( I have seen many examples with query examples and it works like a charm :) )

不要使用/尝试使用 LoaderContentProvider/Database 中插入数据.它就像查询的魅力一样,因为加载器旨在从数据源加载数据(而不是插入数据),这就是它们的目的.如果您想插入数据,请使用 AsyncTask(也可以查看 AsyncQueryhandler),一个常规线程.

Don't use/try to use a Loader to insert data in the ContentProvider/Database. It works like a charm for queries because the loaders were designed to load data from a data source(not insert data), that is their purpose. If you want to insert data use an AsyncTask(also have a look at AsyncQueryhandler), a regular thread.

这篇关于在android中通过Loaders插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆