Skip to content

Instantly share code, notes, and snippets.

@njovy
Created January 21, 2016 06:21
Show Gist options
  • Save njovy/c96df348da5458f15029 to your computer and use it in GitHub Desktop.
Save njovy/c96df348da5458f15029 to your computer and use it in GitHub Desktop.
realm findFirstAsync
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import com.oldroid.examples.realm.models.Conversation;
import io.realm.Realm;
import rx.subscriptions.CompositeSubscription;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private CompositeSubscription subscription = new CompositeSubscription();
private Realm realm;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
realm = Realm.getInstance(this);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
subscription.add(realm.where(Conversation.class)
.findFirstAsync()
.<Conversation>asObservable()
.filter(Conversation::isLoaded)
.subscribe(conversation -> {
if (conversation.isValid()) {
d("Conversation=" + conversation.toString());
} else {
d("Conversation is null");
}
}));
}
@Override protected void onDestroy() {
super.onDestroy();
subscription.unsubscribe();
realm.close();
}
private void d(String message) {
Log.d(TAG, String.format("[%s] %s", Thread.currentThread().getName(), message));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment