greenDAO 40秒插入600条记录 [英] greenDAO 40 seconds to insert 600 records

查看:102
本文介绍了greenDAO 40秒插入600条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择greenDAO是因为它的网站指出它是Android最快的ORM系统之一.令我失望的是,在三星i9001上插入600条记录大约需要40秒.我不确定自己做错了什么.

I've chosen greenDAO because it's site states that it's one of the fastest ORM systems for android. To my dissapointment it takes it like 40 seconds to insert 600 records on Samsung i9001. I'm not sure if I'm doing anything wrong.

您能建议些什么来减少执行这些操作所需的时间吗?

Could you suggest anything to lessen the amount of time it takes to perform those operations ?

生成器代码:

private static void addNewsArticle(Schema schema) {
    Entity article = schema.addEntity("NewsArticle");
    article.addIdProperty().autoincrement();
    article.addStringProperty("title").notNull();
    article.addStringProperty("body").notNull();
    article.addStringProperty("shortDescription").notNull();
    article.addStringProperty("thumb");
    article.addDateProperty("date").notNull();
}

插入

Date now = Calendar.getInstance().getTime();
for (int i = 0; i < 600; i++) {
    NewsArticle testArticle = new NewsArticle();
    testArticle.setTitle("title-text" + i);
    testArticle.setBody("body-text" + i);
    testArticle.setShortDescription("short-text" + i);
    testArticle.setDate(now);
    newsArticleDao.insert(testArticle);
}

推荐答案

我怀疑没有在单个sql语句中执行任何操作.为了实现它,只需在DAO对象上使用insertInTx.

As I suspected things weren't executed within a single sql statement. In order to achieve it just use insertInTx on a DAO object.

以下是上面的代码,仅作了一些细微的更改,使其可以在半秒钟内运行

Here's the above code with slight changes that make it run in like half a second

NewsArticle[] newsArticles = new NewsArticle[600];
NewsArticle testArticle;
    for (int i = 0; i < 600; i++) {
         testArticle = new NewsArticle();
         testArticle.setTitle("title-text" + i);
         testArticle.setBody("body-text" + i);
         testArticle.setShortDescription("short-text" + i);
         testArticle.setDate(now);
         newsArticles[i] = testArticle;
    }
newsArticleDao.insertInTx(newsArticles);

这篇关于greenDAO 40秒插入600条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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