2016-05-30 7 views
1

バックグラウンドスレッドからのデータベースの変更は、アプリケーションの再起動後にのみ表示されます。どうして?直し方?新しいデータを入力しようとしているフラグメントでレルムバックグラウンドスレッドでの不正な動作

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; 
} 

私はあなたがバックグラウンドスレッドの内部でアプリケーション

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

を再起動した後にのみ取得:バックグラウンドで

public class UILApplication extends Application { 

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

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

は、DBに挿入し、次のコードであります一度にデータにアクセスできますが、アプリケーションからデータを取得するには、再起動する必要があります。

+0

それはあなたはそれが自動的にデータを再ロードすることを期待なぜTIM-castelijnsコード) –

+0

表示されない場合はお答えするのは難しいですか?あなたは一度だけhistoryListをフェッチし、何もしません。 – user3424119

+0

を追加しました@あなたが私たちのコード –

答えて

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

成功が第二の属性を除去した後に来た

+0

この場合、マルチプロセスの不一致が発生しました。 – EpicPandaForce

+0

はい、レルムは現在マルチプロセスアクセスをサポートしていません。ここで追跡されています:https://github.com/realm/realm-java/issues/1091 –

関連する問題