使用Firebase ChildEventListener的Realm事务

时间:2016-09-12 18:19:22

标签: android firebase firebase-realtime-database realm

我将realmfirebase一起使用,但我在交易中遇到了一些问题。在firebase ChildEventListener中执行事务的更好方法是什么?

public class MyChildEventListener implements ChildEventListener {

    private Realm realm;

    @Override
    public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
        getRealmInstance().executeTransactionAsync(new Realm.Transaction() {
           @Override
           public void execute(Realm realm) {
               Hour hour = dataSnapshot.getValue(Hour.class);
               String id = dataSnapshot.getKey;

               RealmQuery<HourRealm> query = realm.where(HourRealm.class).equalTo(HourRealm.ID, id);
               if(query.count() == 0) {
                  HourRealm hourRealm = realm.createObject(HourRealm.class);
                  hourRealm.setId(id);
                  ...
               }
           }
        });
    }

    ...

    private Realm getRealmInstance() {
        if(realm == null || realm.isClose()) {
            realm = Realm.getDefaultInstance();
        }
        return realm;
    }

}

它被称为片段,我有超过一百条记录。

public class HourFragment extends Fragment {

    private DatabaseReference hourRef;
    private MyChildEventListener myChildEventListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myChildEventListener = new MyChildEventListener();
        hourRef = FirebaseDatabase.getInstance().getReference().child("hours");
        hourRef.addChildEventListener(myChildEventListener);             
    } 

    @Override
    public void onDestroy() {
        super.onDestroy();
        hourRef.removeEventListener(myChildEventListener); 
    }          

}

导致此错误:

09-12 15:00:16.920 11359-11359/com.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project, PID: 11359
         java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@355e3a0e rejected from io.realm.internal.async.RealmThreadPoolExecutor@23a32c2f[Running, pool size = 9, active threads = 9, queued tasks = 100, completed tasks = 160]
            at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
            at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
            at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1339)
            at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:81)
            at io.realm.internal.async.RealmThreadPoolExecutor.submitTransaction(RealmThreadPoolExecutor.java:71)
            at io.realm.Realm.executeTransactionAsync(Realm.java:1181)
            at io.realm.Realm.executeTransactionAsync(Realm.java:1150)
            at com.project.listener.MyChildEventListener.onChildAdded(MyChildEventListener.java:29)
            at com.google.android.gms.internal.zzahe.zza(Unknown Source)
            at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
            at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5349)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

修改

Realm执行程序轮询大小限制为100.如果在非UI线程上运行ChildEventListener,请使用同步版本而不是异步。 @beeender

有关详细信息,请参阅Realm文档中的Asynchronous Transactions

0 个答案:

没有答案