Last active
April 21, 2016 08:20
-
-
Save yatatsu/499a168015fe218866a7d6a506caeb73 to your computer and use it in GitHub Desktop.
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
package com.github.yatatsu.repository; | |
import io.realm.RealmObject; | |
import io.realm.RealmQuery; | |
public interface RealmObjectQuery<T extends RealmObject> { | |
T apply(RealmQuery<T> query); | |
} |
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
package com.github.yatatsu.repository; | |
import io.realm.Realm; | |
import io.realm.RealmConfiguration; | |
import io.realm.RealmObject; | |
import io.realm.RealmResults; | |
import java.util.List; | |
import rx.Observable; | |
public class RealmRepository<T extends RealmObject> { | |
private final RealmConfiguration configuration; | |
private final Realm realm; | |
private final Class<T> clazz; | |
public RealmRepository(RealmConfiguration configuration, Class<T> clazz) { | |
this.configuration = configuration; | |
this.clazz = clazz; | |
this.realm = Realm.getInstance(configuration); | |
} | |
public void close() { | |
realm.close(); | |
} | |
public void command(Realm.Transaction transaction) { | |
Realm realm = Realm.getInstance(configuration); | |
try { | |
realm.executeTransaction(transaction); | |
} finally { | |
realm.close(); | |
} | |
} | |
public T queryOne(RealmObjectQuery<T> query) { | |
return query.apply(realm.where(clazz)); | |
} | |
public Observable<T> observeQueryOne(RealmObjectQuery<T> query) { | |
return query.apply(realm.where(clazz)).<T>asObservable().filter(RealmObject::isLoaded); | |
} | |
public List<T> query(RealmResultsQuery<T> query) { | |
return query.apply(realm.where(clazz)); | |
} | |
public Observable<RealmResults<T>> observeQuery(RealmResultsQuery<T> query) { | |
return query.apply(realm.where(clazz)).<T>asObservable().filter(RealmResults::isLoaded); | |
} | |
public Observable<Void> observeCommand(Realm.Transaction transaction) { | |
return Observable.<Void>create(subscriber -> { | |
Realm realm = Realm.getInstance(configuration); | |
try { | |
realm.executeTransaction(transaction); | |
subscriber.onNext(null); | |
subscriber.onCompleted(); | |
} catch (Exception e) { | |
subscriber.onError(e); | |
} finally { | |
realm.close(); | |
} | |
}); | |
} | |
} |
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
package com.github.yatatsu.repository; | |
import io.realm.RealmObject; | |
import io.realm.RealmQuery; | |
import io.realm.RealmResults; | |
public interface RealmResultsQuery<T extends RealmObject> { | |
RealmResults<T> apply(RealmQuery<T> query); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment