2017-08-16 76 views
0

當我啓動Android應用程序時,登錄到Realm Object Server並從Realm Server Object下載(同步)數據。好的。 現在我到了顯示人員列表的片段。遠程領域(在領域對象服務器上)發生變化時不通知片段

下面的代碼:

public class PersonsFragment extends Fragment { 
    private ListView notEmptytsListView; 
    private Realm realm; 
    private RealmResults<Person> personsRealmResults; 
    private PersonsAdapter personsAdapter; // RealmBaseAdapter<Organization> 
    private RealmChangeListener realmChangeListener; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.persons_fragment, container, false); 
     init(rootView); 
     return rootView; 
    } 

    private void init(View view) { 
     realm = Realm.getDefaultInstance(); 
     notEmptytsListView = (ListView) view.findViewById(R.id.notEmptytsListView); 
     personsRealmResults = realm.where(Person.class).findAllSorted(Person.NAME); 
     personsAdapter = new PersonsAdapter(personsRealmResults); 
     notEmptytsListView.setAdapter(personsAdapter); 

     // NOT CALL WHEN DATA ON REALM OBJECT SERVER WAS CHANGED 
     realmChangeListener = new RealmChangeListener() { 
      @Override 
      public void onChange(Object object) { 
       personsAdapter.notifyDataSetChanged(); 
      } 
     }; 
     personsRealmResults.addChangeListener(realmChangeListener); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     realm.removeChangeListener(realmChangeListener); 
     realm.close(); 
    } 
} 

如果我留在片段和數據變化領域對象服務器(遠程域)的本地數據不同步的。不調用方法onChanged()。

我該如何解決這個問題?

+0

您的部分代碼未格式化,請進行校對。 – SteveFest

+0

您需要將RealmResults存儲爲字段變量,而不僅僅是局部變量。 – EpicPandaForce

回答

0
public class MyFragment extends Fragment {  
    RealmResults<Person> personsRealmResults; // <-- 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.persons_fragment, container, false); 
     notEmptytsListView = (ListView) view.findViewById(R.id.notEmptytsListView); 

     realm = Realm.getDefaultInstance(); 
     personsRealmResults = realm.where(Person.class).findAllSorted(Person.NAME); 
     personsAdapter = new PersonsAdapter(personsRealmResults); 
     notEmptytsListView.setAdapter(personsAdapter); 

     realmChangeListener = new RealmChangeListener() { 
      @Override 
      public void onChange(Object object) { 
       // call when data on Realm Object Server was changed 
     } 
     }; 
     personsRealmResults.addChangeListener(realmChangeListener); 
     return rootView; 
    } 

    @Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     realm.removeChangeListener(realmChangeListener); 
     realm.close(); 
    } 
} 

雖然它很可能更容易,如果PersonsAdapter是一個RealmBaseAdapter。 (請參閱docs

+0

是的。在這裏我的適配器:'公共類PersonssAdapter擴展RealmBaseAdapter implements ListsAdapter' – lmdic

+0

沒有幫助。同樣的結果。不打* onChange()*。在發佈後查看我的更新代碼。只有在重新啓動應用程序後,數據纔會更改。 – lmdic

+0

上面的代碼應該可以工作,所以其他的東西一定是不對的。您是否100%下載了更改? –