bums3230
bums3230

Reputation: 71

Realm bad behavior in background thread

Changes in the database from a background thread is displayed only after restart the application. Why? How to fix?

public class UILApplication extends Application {

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

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

In the background is next code inserting to 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;
}

In Fragment trying to enter new data but I get only after you restart the application

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();
    ...
}

Inside background thread are all well written and have access to the data at once, but to get the data from the application, it must be restarted

Upvotes: 1

Views: 184

Answers (1)

bums3230
bums3230

Reputation: 71

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

Success came after removal of the second attribute

Upvotes: 1

Related Questions