简体   繁体   English

如何在Android中的多个线程中更新Realm?

[英]How to update Realm in several threads in Android?

I am trying to update realm instances of several threads. 我试图更新几个线程的领域实例。 Instead of explaining everything I created a flowchart as I understand it. 而不是解释所有我创建流程图,因为我理解它。

Here it is: 这里是: 在此输入图像描述

So the problem is when I call notifyAppWidgetViewDataChanged() it doesnt update the realm. 所以问题是当我调用notifyAppWidgetViewDataChanged()它不会更新领域。 I tried adding Realm.OnChangeListener in the Service's onCreate() , but the method is not called when changing the realm from the main thread. 我尝试在Service的onCreate()添加Realm.OnChangeListener ,但是在从主线程更改域时不调用该方法。 I also tried calling getDefaultRealmInstance() from service's onGetViewFactory() instead. 我也试过从服务的onGetViewFactory()调用getDefaultRealmInstance() Same problem. 同样的问题。

Allright, here is my "not-so-memory-efficient" solution: 好吧,这是我的“不那么记忆效率”的解决方案:
From the Widget's service I override only one method to create factory instance: 从Widget的服务我只覆盖一个方法来创建工厂实例:

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new WidFactory(getApplicationContext());
}

And then WidFactory class: 然后是WidFactory类:

class WidFactory implements WidService.RemoteViewsFactory {
    private static int myTasksSize;
    private Context context;
    private Realm realm;
    private RealmResults<Task> myTasks;
    WidFactory(Context context){
        this.context = context;
    }
    @Override
    public void onCreate() {
        realm = Realm.getDefaultInstance();
        myTasks = realm.where(Task.class).findAll();
        myTasksSize = myTasks.size();
        realm.close();
    }
    @Override
    public void onDataSetChanged() {
        realm = Realm.getDefaultInstance();
        realm.refresh();
        myTasks = realm.where(Task.class).findAll();
        myTasksSize = myTasks.size();
        realm.close();
    }

    @Override
    public RemoteViews getViewAt(int position) {
        realm = Realm.getDefaultInstance();
        myTasks = realm.where(Task.class).findAll();
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widgetrow_layout);
        final Task myTask = myTasks.get(position);
        rv.setTextViewText(R.id.widgetrow_text_id, myTask.getTaskText());
        Intent fillInIntent = new Intent(context, WidShowTaskActivity.class);
        fillInIntent.putExtra("code", myTask.getCode());
        realm.close();
        rv.setOnClickFillInIntent(R.id.widgetrow_text_id, fillInIntent);
        return rv;
    }

    @Override
    public int getCount() {
        return myTasksSize;
    }
    @Override
    public RemoteViews getLoadingView() {
        return null;
    }
    @Override
    public int getViewTypeCount() {
        return 1;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public boolean hasStableIds() {
        return true;
    }
    @Override
    public void onDestroy() {
        if (!realm.isClosed()){      realm.close();     }
    }
}

The bad thing with this solution is that I open and close realm 3 times: in onCreate() , onDatasetChanged() and in getViewAt() . 这个解决方案的坏处是我打开和关闭了3次:在onCreate()onDatasetChanged()getViewAt() What is even worse is that generally speaking I only need one realm instance for all widgets, so I wanted to pass the realm in the factories constructor, so all factories use one realm. 更糟糕的是,一般来说我只需要一个面向所有小部件的领域实例,所以我想在工厂构造函数中传递领域,因此所有工厂都使用一个领域。 But 2 problems appeared: 但出现了2个问题:
1)i dont know how to "update" the realm instance when started from service; 1)我不知道如何从服务启动时“更新”领域实例; Android doesn't provide any methods. Android不提供任何方法。
2)I am not sure how content providers work, but the factory class somehow spreads the work over several threads and Realm doesn't like this. 2)我不确定内容提供程序是如何工作的,但是工厂类以某种方式将工作分散到多个线程上,而Realm不喜欢这样。 Not at all. 一点也不。
PS Please correct me if I am wrong somewhere I am relatively new to Android and Realm. PS请纠正我,如果我错了,我对Android和Realm相对较新。
And thank you again for your reply :) @G. 再次感谢您的回复:) @G。 Blake Meike Blake Meike

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM