Last active
December 4, 2015 02:12
-
-
Save wasnot/9efa4edefdb8ffdd54ce to your computer and use it in GitHub Desktop.
Realm Java 検索クエリの速度を活かすための注意点 ref: http://qiita.com/wasnot/items/63b1873e3a842d2342c0
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
// Realm | |
RealmResults<User> results = realm.where(User.class).findAll(); | |
// SQLite | |
sqliteDatabase.query(TABLE, null, null, null, null, null, null); |
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
// Realm | |
RealmResults<User> results = realm.where(User.class).findAll(); | |
for (User user : results) { | |
// クエリだけでなく、わざわざ全ての列要素にもアクセスする | |
user.getAge(); | |
user.getName(); | |
user.getEmail(); | |
} | |
// SQLite | |
Cursor cursor = sqliteDatabase.query(TABLE, null, null, null, null, null, null); | |
result = cursor.getCount(); | |
cursor.moveToFirst(); | |
int indexAge = cursor.getColumnIndex(UserColumns.AGE); | |
int indexName = cursor.getColumnIndex(UserColumns.NAME); | |
int indexEmail = cursor.getColumnIndex(UserColumns.EMAIL); | |
for (int i = 0; i < result; i++) { | |
// クエリだけでなく、わざわざ全ての列要素にもアクセスする | |
cursor.getInt(indexAge); | |
cursor.getString(indexName); | |
cursor.getString(indexEmail); | |
cursor.moveToNext(); | |
} |
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
// クエリを何も処理せずそのまま渡します。 | |
mRecyclerView.setAdapter(new MyItemRecyclerViewAdapter(mRealm.where(User.class).findAll())); | |
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { | |
// RealmObjectをそのまま参照として持ちます | |
private final List<User> mValues; | |
public MyRecyclerViewAdapter(List<User> items) { | |
mValues = items; | |
} | |
} |
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
// このクエリが発行できない。 | |
// SELECT * FROM Account WHERE (date = MONTH(date)) | |
// 月ごとにまとめる場合 | |
accounts = realm.where(Account.class).between("date", monthStart, monthEnd).findAll(); | |
accounts = realm.where(Account.class).findAllSorted("date") | |
Iterator<Account> it = accounts.iterator(); | |
int previousMonth = it.next().getDate().getMonth(); | |
while (it.hasNext) { | |
int month = it.next().getDate().getMonth(); | |
if (month != previousMonth) { | |
// month changed | |
} | |
previousMonth = month; | |
} |
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
// 年齢ごとにまとめる場合 | |
// このクエリも発行できない。 | |
// SELECT * FROM User GROUP BY age | |
// Realmの場合 | |
RealmResults<User> results1 = realm.where(User.class).findAll(); | |
List<Integer> list1 = new ArrayList<>(); | |
for (User o : results1) { | |
int age = o.getAge(); | |
if (!list1.contains(age)) { | |
list1.add(age); | |
} | |
} | |
// SQLiteの場合 | |
cursor = sqliteDatabase.rawQuery(cursor("select * from users group by age", null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment