Created
January 21, 2016 06:21
-
-
Save njovy/c96df348da5458f15029 to your computer and use it in GitHub Desktop.
realm findFirstAsync
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.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