This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SomeActivity extends AppCompatActivity { | |
Realm realm; | |
@Override | |
public void onCreate(Bundle bundle) { | |
super.onCreate(bundle); | |
realm = Realm.getDefaultInstance(); | |
} | |
@Override |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.Log; | |
import io.realm.Realm; | |
import io.realm.RealmConfiguration; | |
/** | |
* Created by Zhuinden on 2016.08.16.. | |
*/ | |
public class RealmManager { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BooksScopeListener extends Fragment { // could go to base class | |
public BooksScopeListener() { | |
setRetainInstance(true); | |
RealmManager.incrementCount(); | |
} | |
@Override | |
public void onDestroy() { | |
RealmManager.decrementCount(); | |
super.onDestroy(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetNewsPostAsyncTask | |
extends AsyncTask<Void, Void, Void> { | |
private Retrofit retrofit; | |
private ApiService apiService; | |
private long postId; | |
public GetNewsPostAsyncTask(Retrofit retrofit, ApiService apiService, long postId) { | |
this.retrofit = retrofit; | |
this.apiService = apiService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
adapter = new RealmRecyclerViewAdapter<NewsPost, NewsPostViewHolder>(getContext(), | |
realm.where(NewsPost.class).findAllSortedAsync(NewsPostFields.MODIFIED, Sort.DESCENDING), true) { // query definition + true == automatic updates | |
@Override | |
public NewsPostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
return new NewsPostViewHolder(LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.view_news_post_item, parent, false)); | |
} | |
@Override | |
public void onBindViewHolder(NewsPostViewHolder holder, int position) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void editPost(final DialogContract dialogContract, final long id) { | |
Realm realm = RealmManager.getRealm(); // UI thread Realm, called via `onClick()` listener | |
realm.executeTransactionAsync(new Realm.Transaction() { | |
@Override | |
public void execute(Realm realm) { | |
NewsPost post = realm.where(NewsPost.class).equalTo(NewsPostFields.ID, id).findFirst(); | |
// synchronous query by ID on background thread, inside transaction | |
if(post != null) { | |
post.setTitle(dialogContract.getTitle()); | |
//... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NewsPost extends RealmObject { | |
@PrimaryKey | |
@Required | |
private long id; | |
// left nullable as default as example | |
private String author; | |
private String title; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Post extends RealmObject implements Observable, RealmDataBinding { | |
@PrimaryKey | |
private long id; | |
private String text; | |
@Bindable | |
public long getId() { | |
return id; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface RealmDataBinding { | |
interface Factory { | |
RealmChangeListener create(); | |
} | |
RealmDataBinding.Factory FACTORY = () -> element -> { | |
if(element instanceof RealmDataBinding) { | |
((RealmDataBinding)element).notifyChange(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity | |
extends RealmActivity { | |
Post post; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); | |
post = realm.where(Post.class).equalTo("id", 1L).findFirst(); |