Skip to content

Instantly share code, notes, and snippets.

@wolkenschieber
Last active October 28, 2016 18:09
Show Gist options
  • Save wolkenschieber/a8b5e8e26ef29218b4297d509fbe88b3 to your computer and use it in GitHub Desktop.
Save wolkenschieber/a8b5e8e26ef29218b4297d509fbe88b3 to your computer and use it in GitHub Desktop.
Requery transaction blocking?
public class CreateLotteries
{
private static final String LOG_TAG = "CreateLotteries";
private final SingleEntityStore<Persistable> data;
public CreateLotteries(SingleEntityStore<Persistable> data)
{
this.data = data;
}
public List<Observable> getPopulation()
{
InitialLotteryProvider provider = new InitialLotteryProvider();
List<LotteryEntityHolder> initialLotteries = provider.getEntities();
List<Observable> observables = new LinkedList<>();
for (LotteryEntityHolder holder : initialLotteries)
observables.add(getObservable(holder));
Log.v(LOG_TAG, String.format("Populated list with %d items", initialLotteries.size()));
return observables;
}
private Observable<Object> getObservable(LotteryEntityHolder holder)
{
List<Single<? extends Persistable>> lotterySingles = new LinkedList<>();
Single<LotteryEntity> lotterySingle = data.insert(holder.getLotteryEntity());
lotterySingles.add(lotterySingle);
for (LotteryNumbersEntity numberEntity : holder.getLotteryNumberEntities())
{
Single<LotteryNumbersEntity> lotteryNumberSingle = data.insert(numberEntity);
lotterySingles.add(lotteryNumberSingle);
}
Single<?>[] elements = lotterySingles.toArray(new Single<?>[lotterySingles.size()]);
return data.runInTransaction(elements);
}
}
V/MainActivity: Populating database ...
V/CreateLotteries: Populated list with 12 items
V/MainActivity: Applying rx.Observable@eb7e78f
W/SQLiteConnectionPool: The connection pool for database '/data/user/0/app/databases/default' has been unable to grant a connection to thread 202 (pool-1-thread-1) with flags 0x2 for 30.000002 seconds.
Connections: 0 active, 1 idle, 0 available.
W/SQLiteConnectionPool: The connection pool for database '/data/user/0/app/databases/default' has been unable to grant a connection to thread 205 (pool-1-thread-1) with flags 0x2 for 60.001003 seconds.
Connections: 0 active, 1 idle, 0 available.
W/SQLiteConnectionPool: The connection pool for database '/data/user/0/app/databases/default' has been unable to grant a connection to thread 205 (pool-1-thread-1) with flags 0x2 for 90.00101 seconds.
Connections: 0 active, 1 idle, 0 available.
W/SQLiteConnectionPool: The connection pool for database '/data/user/0/app/databases/default' has been unable to grant a connection to thread 205 (pool-1-thread-1) with flags 0x2 for 120.00101 seconds.
Connections: 0 active, 1 idle, 0 available.
W/SQLiteConnectionPool: The connection pool for database '/data/user/0/app/databases/default' has been unable to grant a connection to thread 205 (pool-1-thread-1) with flags 0x2 for 150.001 seconds.
Connections: 0 active, 1 idle, 0 available.
@Table(name = "Lottery")
@Entity(name = "LotteryEntity")
public interface Lottery extends Observable, Parcelable, Persistable
{
@Key
@Generated
int getID();
@OneToMany(mappedBy = "lottery", cascade = {CascadeAction.DELETE, CascadeAction.SAVE})
Result<LotteryNumbers> getNumbers();
}
@Table(name = "LotteryNumbers")
@Entity(name = "LotteryNumbersEntity")
public interface LotteryNumbers extends Observable, Parcelable, Persistable
{
@Key
@Generated
int getID();
@ForeignKey
@ManyToOne
Lottery getLottery();
int getName();
}
private void populateDatabase()
{
int lotteryCount = data.count(Lottery.class).get().value();
if (lotteryCount == 0)
{
Log.v(LOG_TAG, "Populating database ...");
CreateLotteries createLotteries = new CreateLotteries(data);
List<Observable> population = createLotteries.getPopulation();
for (Observable observable : population)
{
Log.v(LOG_TAG, "Applying " + observable);
observable.
observeOn(Schedulers.io()).
subscribe();
Log.v(LOG_TAG, "\tDone " + observable);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment