Android AsyncTask和realm.executeTransactionAsync之间的区别

时间:2016-07-12 20:20:25

标签: android multithreading android-asynctask realm

我是Realm世界的新手,我遇到了麻烦。我的Android应用程序中有一个动画,并且在运行数据库进程并且我的动画冻结时显示。 我想可能是因为我的数据库进程遇到了AsyncTask:

*** WARNING: renaming "_sqlite3" since importing it failed: dlopen(build/lib.macosx-10.12-x86_64-2.7/_sqlite3.so, 2): Symbol not found: _sqlite3_enable_load_extension
  Referenced from: build/lib.macosx-10.12-x86_64-2.7/_sqlite3.so
  Expected in: flat namespace
 in build/lib.macosx-10.12-x86_64-2.7/_sqlite3.so

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             dl                 imageop
linuxaudiodev      ossaudiodev        spwd
sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


Failed to build these modules:
_sqlite3

我想知道Android的AsyncTask和realm.executeTransactionAsync(...)之间是否有一些区别。方法executeTransactionAsync阻止UI冻结?

3 个答案:

答案 0 :(得分:2)

动画在主线程上运行,AsyncTask不在(在单独的线程上运行)。

  

我的动画很冷。我想可能是因为我的数据库进程遇到了AsyncTask

情况不太可能。

在Realm提供的异步事务中执行数据库事务当然比将事务包装在您自己创建的asynctask中更好(至少在概念上更明智)。

为异步交易切换asynctask不会解决您的问题。

答案 1 :(得分:1)

我认为您的问题与AsyncTaskrealm.executeTransactionAsync之间的差异无关,他们做同样的工作(如果您以正确的方式延长AsyncTask),如果您确定所有事务都是在后台完成的,你应该在其他地方查找问题(在主线程上运行的一些长任务),对于你发布的代码没什么可说的,但作为提示:

AsyncTaskRealm(异步交易)使用ThreadPoolExecutor时存在一些差异:

<强> Relam:

private static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2 + 1;
private static final int QUEUE_SIZE = 100;

ThreadPoolExecutor THREAD_POOL_EXECUTOR = ThreadPoolExecutor(CORE_POOL_SIZE, CORE_POOL_SIZE,
     0L, TimeUnit.MILLISECONDS, //terminated idle thread
     new ArrayBlockingQueue<Runnable>(QUEUE_SIZE));

<强>的AsyncTask:

private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
private static final int KEEP_ALIVE = 1;
private static final ThreadFactory sThreadFactory = new ThreadFactory() {
    private final AtomicInteger mCount = new AtomicInteger(1);

    public Thread newThread(Runnable r) {
        return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
    }
};

private static final BlockingQueue<Runnable> sPoolWorkQueue =
        new LinkedBlockingQueue<Runnable>(128);

public static final Executor THREAD_POOL_EXECUTOR
        = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

答案 2 :(得分:0)

我从未使用过域,但根据下面的文档,“executeTransactionAsync”用于“异步更新后台线程上的对象”,所以我认为这可以回答你的问题。

来源:

https://realm.io/docs/java/latest/