CursorWrapperInner:光标在没有事先close()的情况下完成

时间:2017-11-04 05:43:23

标签: android sqlite android-contentprovider

我已经构建了一个rss新闻应用程序。它有一些错误,在某些方面工作得很好。问题是内容提供商没有为某些RSS订阅源添加新的新闻,并且在日志中没有先前的close()错误的情况下完成Cursor。我在后台IntentService中刷新了feed。如果可能,需要提高性能。

private int refreshLocalFeed(Feeds feeds) throws RemoteException, OperationApplicationException {

    LogMessage.d("feed refresh:", feeds.getCategoryId() + " : " + feeds.getName());
    Call<RSSFeed2> call = api.loadOwnRssFeed(feeds.getUrl());
    ContentResolver cr = MyApp.getAppContext().getContentResolver();
    int success = 0;
    try {
        Response<RSSFeed2> response = call.execute();
        RSSFeed2 rssFeed = response.body();
        if (rssFeed != null) {
            ArrayList<ContentProviderOperation> operations = new ArrayList<>();
            Collections.reverse(rssFeed.getArticleList());
            for (Article article : rssFeed.getArticleList()) {

                Cursor c = null;
                try {
                    c = cr.query(DbContract.EntryColumns.CONTENT_URI, new String[]{DbContract.EntryColumns._ID}, DbContract.EntryColumns.CATEGORYID + "=? AND " + DbContract.EntryColumns.LINK + "=?",
                            new String[]{feeds.getCategoryId(), article.getLink()}, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ContentValues values = new ContentValues();
                values.put(DbContract.EntryColumns.TITLE, article.getTitle());
                values.put(DbContract.EntryColumns.DESCRIPTION, article.getDescription());
                values.put(DbContract.EntryColumns.DATE, article.getPubDate());
                values.put(DbContract.EntryColumns.AUTHOR, getString(R.string.app_name));
                values.put(DbContract.EntryColumns.FETCH_DATE, System.currentTimeMillis());
                values.put(DbContract.EntryColumns.CATEGORYID, feeds.getCategoryId());
                values.put(DbContract.EntryColumns.GUID, article.getGuid());

                String alternateImageUrl = getImageUrl(article.getDescription());
                if (article.getThumbnail() != null && !article.getThumbnail().isEmpty()) {
                    values.put(DbContract.EntryColumns.IMAGE_URL, article.getThumbnail());
                } else if (!alternateImageUrl.isEmpty()) {
                    values.put(DbContract.EntryColumns.IMAGE_URL, getImageUrl(article.getDescription()));
                }
                values.put(DbContract.EntryColumns.LINK, article.getLink());

                if (c != null && c.getCount() > 0) {
                    operations.add(ContentProviderOperation.newUpdate(DbContract.EntryColumns.CONTENT_URI)
                            .withSelection(DbContract.EntryColumns._ID, new String[]{String.valueOf(c.getInt(c.getColumnIndexOrThrow(DbContract.EntryColumns._ID)))})
                            .withValues(values)
                            .build());
                    c.close();
                } else {
                    operations.add(ContentProviderOperation.newInsert(DbContract.EntryColumns.CONTENT_URI)
                            .withValues(values)
                            .build());

                }
            }
            ContentProviderResult[] results = cr.applyBatch(DbContract.AUTHORITY, operations);
            for (ContentProviderResult result : results) {
                if (result.uri != null) {
                    success++;
                }
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

修改

if (c != null) {
    if (c.getCount() > 0) {
        operations.add(ContentProviderOperation.newUpdate(DbContract.EntryColumns.CONTENT_URI)
                                .withSelection(DbContract.EntryColumns._ID, new String[]{String.valueOf(c.getInt(c.getColumnIndexOrThrow(DbContract.EntryColumns._ID)))})
                                .withValues(values)
                                .build());
    }
    c.close();
} else {
        operations.add(ContentProviderOperation.newInsert(DbContract.EntryColumns.CONTENT_URI)
                            .withValues(values)
                            .build());
    }

0 个答案:

没有答案