我之所以选择greenDAO,是因为它的网站宣称它是最快的安卓对象关系管理系统之一。令我失望的是,在三星i9001上插入600条记录大约需要40秒。我不确定我是否做错了什么。
您有什么建议可以减少执行这些操作的时间吗?
生成器代码:
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);
}发布于 2012-10-07 22:08:15
正如我怀疑的那样,事情并不是在一条sql语句中执行的。为了实现它,只需在DAO对象上使用insertInTx即可。
下面是上面的代码,做了一些细微的修改,使它在半秒内就能运行
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);发布于 2012-11-07 17:53:20
您还可以创建一个新的Runnable,它在DaoSession.runInTx函数中执行所有插入操作
daoSession.runInTx(new Runnable {
    public void run(){
        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);
                     tesArticle.setShortDescription("short-text" + i);
                     testArticle.setDate(now);
                     newsArticleDao.insert(testArticle);
        }
   }
});发布于 2013-04-06 16:50:09
这里提供了一篇关于加速Android的SQLITE插入操作的优秀文章。尝试这一点,我可以优化同步模块的速度从1.2小时到14分钟,在同步39MB数据库。
http://tech.vg.no/2011/04/04/speeding-up-sqlite-insert-operations/
如果您需要代码spinnets,请告诉我。
https://stackoverflow.com/questions/12748899
复制相似问题