领域

时间:2016-05-30 13:17:46

标签: android realm

仅在重新启动应用程序后才会显示数据库中后台线程的更改。为什么?怎么解决?

public class UILApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();

    RealmConfiguration config = new RealmConfiguration.Builder(this).build();
    Realm.setDefaultConfiguration(config);
}

在后台插入DB的下一个代码:

public void setFollows(List<JSONObject> follows) {
    Realm realm = Realm.getDefaultInstance();
    List<Follow> finalFollows = new RealmList<>();

    try {
        for(JSONObject follow:follows){
            finalFollows.add(new Follow(follow.getLong("id"),follow.getString("username"), follow.getString("profile_picture"), follow.getString("full_name")));
        }
        List<Follow> goneFollows = getGoneFollows(finalFollows);
        List<Follow> newFollows = getNewFollows(finalFollows);

        realm.beginTransaction();

        if(goneFollows != null && !goneFollows.isEmpty()){
            goneFollows = realm.copyToRealmOrUpdate(goneFollows);
            L.d("getHistoryForUpdate goneFollows");
            History history = getHistoryForUpdate();
            history.getRemoveFollows().clear();
            history.getRemoveFollows().addAll(goneFollows);
        }

        if(newFollows != null && !newFollows.isEmpty()){
            newFollows = realm.copyToRealmOrUpdate(newFollows);
            L.d("getHistoryForUpdate newFollows");
            History history = getHistoryForUpdate();
            history.getNewFollows().clear();
            history.getNewFollows().addAll(newFollows);
        }

        finalFollows = realm.copyToRealmOrUpdate(finalFollows);
        getFollows().clear();
        getFollows().addAll(finalFollows);
        realm.commitTransaction();
        realm.close();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public History getHistoryForUpdate(){
    Realm realm = Realm.getDefaultInstance();
    String today = DateHandler.getOnlyDate();

    History history = realm.where(History.class).equalTo("createDate", today).findFirst();

    if(history == null){
        L.d("getHistoryForUpdate new");
        realm.beginTransaction();
        history = new History();
        history = realm.copyToRealm(history);
        history.setCreateDate(today);
        getHistoryList().add(history);
        realm.commitTransaction();
    }
    L.d("getHistoryForUpdate");
    realm.close();
    return history;
}

在Fragment中尝试输入新数据,但只有在重新启动应用程序后才能获得

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    realm = Realm.getDefaultInstance();
    favorite = realm.where(Favorite.class).equalTo("id", Long.parseLong(userId))
            .findFirst();
    RealmList<History> historyList = favorite.getHistoryList();
    ...
}

内部后台线程写得很好并且可以同时访问数据,但是要从应用程序获取数据,必须重新启动它

1 个答案:

答案 0 :(得分:1)

<receiver
        android:name=".core.receivers.Alarm"
        android:process=":remote" />

删除第二个属性

后取得了成功