Share
Embed
Android Architecture Components
Google I/O 2017 extended Android Codelabs
Android Architecture Components 발표자료
pluulove (노현석)
August 01, 2017
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
82 slides
-
Data L i f e c y c l e
ۄࢎ ߷ -
୭ઙ ইఃఫ ഋక Clean Architecture + MVVM +
Lifecycle -
class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public
void onCreate(...) { myLocationListener = new MyLocationListener(this, (location) -> { // update UI }); } public void onStart() { super.onStart(); myLocationListener.start(); } public void onStop() { super.onStop(); myLocationListener.stop(); } } ࢤݺӝ ҙ۲ ੌ߈ੋ Android ࢸ҅ • onStart()ীࢲ द • onStop()ীࢲ ઙܐ -
Lifecycle ࢤݺ ӝ ߮ ৌѢഋҗ ೣԋ അ ࢤݺ ӝ ࢚క
ৌѢഋਸ ࢎਊೞৈ ҙ۲ ҳࢿ ਃࣗী ೠ ࢤݺ ӝ ࢚కܳ ୶ -
LifecycleOwner Lifecycle ёܳ ߈ജೞח ױੌ ੋఠಕझ ݫࢲ٘ী ࢳਸ ୶оೣਵ۽ॄ ҳࢿ
ਃࣗ ࢤݺ ӝ ߮ܳ ݽפఠ݂ೡ ࣻ ח ੋఠಕझ LifecycleObserver -
public class LifecycleActivity extends FragmentActivity implements LifecycleRegistryOwner { private final
LifecycleRegistry mRegistry = new LifecycleRegistry(this); @Override public LifecycleRegistry getLifecycle() { return mRegistry; } } public class LifecycleFragment extends Fragment implements LifecycleRegistryOwner { LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); @Override public LifecycleRegistry getLifecycle() { return mLifecycleRegistry; } } -
will be deprecated LifecycleActivity LifecycleFragment Architecture Components ח അ ঌ
ߡ Fragmentҗ AppCompatActivity۽ח ҳഅ ࠛоמ Lifecycles ۽ંо दغݶ Support Library Fragment৬ Activityо LifecycleOwner ੋఠಕ झܳ ҳഅೠ. -
public class MyObserver implements LifecycleObserver { public MyObserver(Lifecycle lifecycle) {
// Starts lifecycle observation lifecycle.addObserver(this); ... } // Annotated methods called when the associated lifecycle goes through these events @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) public void onResume() { } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) public void onPause() { } } MyObserver observer = new MyObserver(aLifecycleOwner.getLifecycle()); 1 2 4 3 -
implements LifecycleObserver @OnLifecycleEvent(Lifecycle.Event.ON_START) APT ח GenericLifecycleObserver / ReflectiveGenericLifecycleObserver (ղࠗ ېझ
ਊ) ী ೯زਸ ׳ Lifecycle#State ߸ച ١ਸ LifecycleObserverী Dispatch Lifecycle Flow APTী ೧ ${Activity_name} _${LifecycleObserver_name} _LifecycleAdapter ېझо ࢤࢿؽ -
ViewModel UI ҙ۲ ؘఠܳ ೞҊ ҙܻೞৈ ؘఠо ച ݶ ഥҗ
э ҳࢿ ߸҃ਸ Ѽ٧ ࣻ ب۾ ࢸ҅غ णפ. -
ViewModel ଼ UI ؘఠ ҙܻ Activity / Fragment ߂ աݠ
জ ࢎ ాन ܻ Configuration ߸҃ী ೞח ؘఠ Activity / Fragment ۽ࠗఠ ة݀ -
The lifecycle of a ViewModel : https://developer.android.com/topic/libraries/architecture/viewmodel.html ViewModel Scope ViewModelਸ
оח Activityо ઙ ܐغח ҃, ۨਕо ViewModel onClearedܳ ഐೣ -
https://github.com/googlecodelabs/android-lifecycles/blob/master/app/src/main/java/com/example/android/lifecycles/step2/ChronometerViewModel.java public class ChronometerViewModel extends ViewModel { @Nullable private
Long startDate; @Nullable public Long getStartDate() { return startDate; } public void setStartDate(final long startDate) { this.startDate = startDate; } } -
https://github.com/googlecodelabs/android-lifecycles/blob/master/app/src/main/java/com/example/android/lifecycles/step2/ChronoActivity2.java public class ChronoActivity2 extends LifecycleActivity { @Override protected
void onCreate(Bundle savedInstanceState) { ... ChronometerViewModel chronometerViewModel = ViewModelProviders.of(this).get(ChronometerViewModel.class); ... } } ViewModelProviders.of(this) ܳ ખ ؊ Ө ইࠁݶ, ViewModel UI (Activity / Fragment)৬ োѾغয ਸ ঌ ࣻ णפ. -
ViewModelProviders ViewModelStore ਬܻ౭ ېझ ղࠗਵ۽ ViewModelStoreܳ ଵઑೞৈ ViewModel ӝઓ ੋ
झఢझܳ ߈ജ, ઓೞ ঋח ҃ ViewModel ੋझఢझܳ ࢜۽ ࢿפ. -
android.arch.lifecycle.ViewModelStores.java public class ViewModelStore { private final HashMap<String, ViewModel>
mMap = new HashMap<>(); final void put(String key, ViewModel viewModel) { ViewModel oldViewModel = mMap.get(key); if (oldViewModel != null) { oldViewModel.onCleared(); } mMap.put(key, viewModel); } final ViewModel get(String key) { return mMap.get(key); } public final void clear() { for (ViewModel vm : mMap.values()) { vm.onCleared(); } mMap.clear(); } } -
android.arch.lifecycle.AndroidViewModel.java public class AndroidViewModel extends ViewModel { private Application mApplication;
public AndroidViewModel(Application application) { mApplication = application; } /** * Return the application. */ public <T extends Application> T getApplication() { return (T) mApplication; } } -
ViewModel ઁড Viewী Ӕ X Drawable ١ Resource о
X Activity / Fragment о X ઙܐद ܻח onCleared()ী ӝࣿ -
LiveData ч (Value Holder)ਸ ࠁਬೞҊ ч ߸҃ ࢎ೦ਸ ҙ ೡ
ࣻח ҳࢿ ਃࣗੑפ. ViewHolder ؘఠܳ ਬೞب۾ ࢸ҅ -
LiveData ч (Value Holder)ਸ ࠁਬೞҊ ч ߸҃ ࢎ೦ਸ ҙ ೡ
ࣻח ҳࢿ ਃࣗੑפ. ViewHolder ؘఠܳ ਬೞب۾ ࢸ҅ Observableೠ DataHolder -
LiveData ೦࢚ ୭न ؘఠ ೠ ҳࢿ ߸҃ ݫݽܻ ־ࣻ
হ (Lifecycle) Lifecycle উೡदী݅ ׳ ч ߸҃ ܳ ਤೠ ߹ب ҳഅ X -
LiveData ৬ ӝઓҗ ର EventBus RxJava BehaviorSubject Lifecycle উೠ ࢚కੌٸ݅
ాо מ ೠߣ ҳةೞݶ زਵ۽ ೧ઁ೧ષ -
https://codelabs.developers.google.com/codelabs/android-persistence/index.html?index=..%2F..%2Fio2017#5 mViewModel.books.observe(this, new Observer<List<Book>>() { @Override public void onChanged(@NonNull final
List<Book> books) { showBooksInUi(books, mBooksTextView); } }); Model ߸҃द ഐ Model ҙ update UI -
LiveData ా दӝ LiveData#setValue(...) LiveData#postvalue(...) Active ࢚కীࢲ LiveData ч ߸҃غ
ਸ ٸ Inactiveীࢲ Active о غਸ ٸ -
Room Object Mapping Library ਗद SQL ௪ܻ ஹੌ ఋ
Ѩૐ SQL ௪ܻ৬ Java ؘఠ ё ࢎܳ ߸ജೞח boilerplate codeܳ ઁѢ ழࢲ ژח ۽؊ ࢿ ਃ হ ߈ജ ਗೣ ؘఠ ࣘച ઁҕ -
https://github.com/googlecodelabs/android-persistence/blob/master/app/src/main/java/com/example/android/persistence/codelab/db/User.java @Entity public class User { @PrimaryKey public String id;
public String name; public String lastName; public int age; @Ignore Bitmap picture; } Entity • @PrimaryKey ӝࠄః ◦ autoGenerate ◦ primaryKeys ◦ tableName • @Ignore ೠ ٘ח Column ࢤࢿܻܳ ೞ ঋ • setter/getterܳ ਊ೧ ٘ Ӕ • @ColumnInfo ߹ب оמ -
https://github.com/googlecodelabs/android-persistence/blob/master/app/src/main/java/com/example/android/persistence/codelab/db/Loan.java @Entity(foreignKeys = { @ForeignKey(entity = Book.class, parentColumns = "id",
childColumns = "book_id"), @ForeignKey(entity = User.class, parentColumns = "id", childColumns = "user_id")}) @TypeConverters(DateConverter.class) public class Loan { public @PrimaryKey String id; public Date startTime; public Date endTime; @ColumnInfo(name="book_id") public String bookId; @ColumnInfo(name="user_id") public String userId; } Foreign Key -
https://github.com/googlesamples/android-architecture-components/blob/178fe541643adb122d2a8925cf61a21950a4611c/BasicSample/app/src/main/java/com/example/ android/persistence/db/converter/DateConverter.java public class DateConverter { @TypeConverter public static Date
toDate(Long timestamp) { return timestamp == null ? null : new Date(timestamp); } @TypeConverter public static Long toTimestamp(Date date) { return date == null ? null : date.getTime(); } } TypeConverter ੑ۱ ఋੑ DB ীࢲ ఋੑ ۱ ఋੑ Date Long Date -
https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/vo/ Repo.java @Entity(primaryKeys = {"name", "owner_login"}) public class Repo {
public final int id; @SerializedName("name") public final String name; @SerializedName("full_name") public final String fullName; ... @SerializedName("owner") @Embedded(prefix = "owner_") public final Owner owner; ... } Nested Objects public class Owner { @SerializedName("login") public final String login; @SerializedName("url") public final String url; ... } -
Nested Objects ѐী ೞਤ ٘۽ ࠙೧ೞৈ ёܳ അ Room Entityр
ҙ҅ܳ য়࠳ં۽ അ ࠛ оמ RDBܳ ਤೠ ۨযݶࢲ Object-relational mapping ইצ ਬ class A { @OneToOne // ਗ ࠛо public B b; } -
https://github.com/googlecodelabs/android-persistence/blob/master/app/src/main/java/com/example/android/persistence/codelab/db/UserDao.java @Dao public interface UserDao { @Query("select * from user")
List<User> loadAllUsers(); @Query("select * from user where name = :firstName and lastName = :lastName") List<User> findByNameAndLastName(String firstName, String lastName); @Delete void deleteUser(User user); @Insert(onConflict = IGNORE) void insertOrReplaceUsers(User... users); ... } Data Access Object (Dao) -
https://github.com/googlecodelabs/android-persistence/blob/master/app/src/main/java/com/example/android/persistence/codelab/db/UserDao.java @Dao public interface UserDao { @Query("select * from user")
List<User> loadAllUsers(); @Query("select * from user where name = :firstName and lastName = :lastName") List<User> findByNameAndLastName(String firstName, String lastName); @Delete void deleteUser(User user); @Insert(onConflict = IGNORE) void insertOrReplaceUsers(User... users); ... } Data Access Object (Dao) -
public List<User> findByNameAndLastName(String firstName, String lastName) { final String _sql
= "select * from user where name = ? and lastName = ?"; final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 2); int _argIndex = 1; if (firstName == null) { _statement.bindNull(_argIndex); } else { _statement.bindString(_argIndex, firstName); } _argIndex = 2; if (lastName == null) { _statement.bindNull(_argIndex); } else { _statement.bindString(_argIndex, lastName); } UserDao_Impl implements UserDao (1/3) -
final Cursor _cursor = __db.query(_statement); try { final int _cursorIndexOfId
= _cursor.getColumnIndexOrThrow("id"); final int _cursorIndexOfName = _cursor.getColumnIndexOrThrow("name"); final int _cursorIndexOfLastName = _cursor.getColumnIndexOrThrow("lastName"); final int _cursorIndexOfAge = _cursor.getColumnIndexOrThrow("age"); final List<User> _result = new ArrayList<User>(_cursor.getCount()); while(_cursor.moveToNext()) { final User _item; _item = new User(); _item.id = _cursor.getString(_cursorIndexOfId); _item.name = _cursor.getString(_cursorIndexOfName); _item.lastName = _cursor.getString(_cursorIndexOfLastName); _item.age = _cursor.getInt(_cursorIndexOfAge); _result.add(_item); } return _result; UserDao_Impl implements UserDao (2/3) -
https://github.com/googlecodelabs/android-persistence/blob/master/app/src/main/java/com/example/android/persistence/codelab/db/AppDatabase.java // Create Database @Database(entities = {User.class, Book.class, Loan.class}, version
= 1) public abstract class AppDatabase extends RoomDatabase { public abstract UserDao userModel(); public abstract BookDao bookModel(); public abstract LoanDao loanModel(); ... } Database -
public class AppDatabase_Impl extends AppDatabase { private volatile UserDao _userDao;
private volatile BookDao _bookDao; private volatile LoanDao _loanDao; protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration configuration) { // Create, Drop, Migration … etc } @Override protected InvalidationTracker createInvalidationTracker() { ... } @Override public UserDao userModel() { … } @Override public BookDao bookModel() { … } @Override public LoanDao loanModel() { … } } AppDatabase_Impl extends AppDatabase (1/3) // ప࠶ ߸҃ਸ х -
protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration configuration) { final SupportSQLiteOpenHelper.Callback _openCallback = new
RoomOpenHelper(configuration, new RoomOpenHelper.Delegate() { public void createAllTables(SupportSQLiteDatabase _db) { … } public void dropAllTables(SupportSQLiteDatabase _db) { … } protected void onCreate(SupportSQLiteDatabase _db) { … } public void onOpen(SupportSQLiteDatabase _db) { … } protected void validateMigration(SupportSQLiteDatabase _db) { … } }, "..."); AppDatabase_Impl extends AppDatabase (2/3) -
final SupportSQLiteOpenHelper.Configuration _sqliteConfig = SupportSQLiteOpenHelper.Configuration.builder(configuration.context) .name(configuration.name) .version(1) .callback(_openCallback) .build(); final
SupportSQLiteOpenHelper _helper = configuration.sqliteOpenHelperFactory.create(_sqliteConfig); return _helper; } AppDatabase_Impl extends AppDatabase (3/3) -
https://github.com/googlecodelabs/android-persistence/blob/master/app/src/main/java/com/example/android/persistence/codelab/db/AppDatabase.java // Use Room Database AppDatabase db = Room.inMemoryDatabaseBuilder( context.getApplicationContext(),
AppDatabase.class) .allowMainThreadQueries() .build(); Use Database -
https://developer.android.com/reference/android/arch/persistence/room/Room.html // File Builder<T> databaseBuilder (Context context, Class<T> klass, String
name) // Memory Builder<T> inMemoryDatabaseBuilder (Context context, Class<T> klass) Database Generate Options -
Invalidation Tracker ۨ٘ ߸҃ী ೠ ୶ InvalidationTracker ېझо ప࠶ ߸҃ਸ
Ѩࢎೣ Room ীࢲ ࢎਊߑߨ LiveData RxJava2 Flowable/Publisher DAOীࢲ ਤ ೦ݾਸ ࢎਊ -
Invalidation Tracker We create an in memory table with (version,
table_id) where version is an auto-increment primary key and a table_id (hardcoded int from initialization). ObservedTableTracker tracks list of tables we should be watching (e.g. adding triggers for). Before each beginTransaction, RoomDatabase invokes InvalidationTracker to sync trigger states. After each endTransaction, RoomDatabase invokes InvalidationTracker to refresh invalidated tables. Each update on one of the observed tables triggers an insertion into this table, hence a new version. Unfortunately, we cannot override the previous row because sqlite uses the conflict resolution of the outer query (the thing that triggered us) so we do a cleanup as we sync instead of letting SQLite override the rows. https://sqlite.org/lang_createtrigger.html: An ON CONFLICT clause may be specified as part of an UPDATE or INSERT action within the body of the trigger. However if an ON CONFLICT clause is specified as part of the statement causing the trigger to fire, then conflict handling policy of the outer statement is used instead. -
Database migration Migration ېझ ਊ startVersion, endVersion ୭न ߡਵ۽
ೞӝী ࠙ೠ ݃Ӓۨ࣌ ઁҕೞ ঋח ҃ ؘఠ߬झܳ ࢿೞ۽ ݽٚ ؘఠ ࣚप -
https://developer.android.com/topic/libraries/architecture/room.html Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name") .addMigrations(MIGRATION_1_2, MIGRATION_2_3).build(); static final Migration
MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(SupportSQLiteDatabase database) { database.execSQL("CREATE TABLE `Fruit` (`id` INTEGER, " + "`name` TEXT, PRIMARY KEY(`id`))"); } }; static final Migration MIGRATION_2_3 = new Migration(2, 3) { @Override public void migrate(SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE Book " + " ADD COLUMN pub_year INTEGER"); } }; -
https://developer.android.com/topic/libraries/architecture/room.html#db-migration // build.gradle android { ... defaultConfig { ... javaCompileOptions
{ annotationProcessorOptions { arguments = ["room.schemaLocation": "$projectDir/schemas".toString()] } } } ... } Testing migrations (1) ݢ ؘఠ߬झ झః݃ܳ ղࠁղঠפ. Room ஹੌद ؘఠ߬झ झః݃ ࠁ ܳ JSON ੌਸ ࢤࢿೞ۽, build.gradle ੌীࢲ room.schemaLocation Annotation ۽ࣁࢲ ࣘࢿਸ ࢸೠ. -
https://developer.android.com/topic/libraries/architecture/room.html#db-migration // build.gradle android { ... sourceSets { androidTest.assets.srcDirs +=
files("$projectDir/schemas".toString()) } ... dependencies { androidTestCompile "android.arch.persistence.room:testing" } } Testing migrations (2) ‘android.arch.persistence.ro om:testing’ ਸ ઙࣘࢿী ୶оೞ Ҋ, ղࠁմ झః݃ ੌ (JSON) ਸ asset ಫ؊ী ߓೠ. -
https://developer.android.com/topic/libraries/architecture/room.html#db-migration @RunWith(AndroidJUnit4.class) public class MigrationTest { private static final String
TEST_DB = "migration-test"; @Rule public MigrationTestHelper helper; public MigrationTest() { helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(), MigrationDb.class.getCanonicalName(), new FrameworkSQLiteOpenHelperFactory()); } … } Testing migrations (3) MigrationTestHelper ېझܳ ਊ೧ࢲ झః݃ ੌਸ ੍যٜੋ. -
https://developer.android.com/topic/libraries/architecture/room.html#db-migration @RunWith(AndroidJUnit4.class) public class MigrationTest { @Test public void migrate1To2()
throws IOException { // DB۽ ೞח Schema Versionਵ۽ ࢿ SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1); // Daoח ୭न Schemaী ઓೞ۽ ࢎਊ ࠛоמ // Ӓېࢲ ࣻসਵ۽ ؘఠܳ ֍যঠೠ. db.execSQL(...); // Migrationী ࠺ೞৈ dbܳ ײח db.close(); ... Testing migrations (4) -
https://developer.android.com/topic/libraries/architecture/room.html#db-migration ... // ਗೞח ߡী ೠ ݃Ӓۨ࣌ ۽ࣁझܳ ઁҕ db
= helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2); // MigrationTestHelperо झః݃ ߸҃ ࢎ೦ਸ زਵ۽ ഛੋ // ؘఠо ৢ߄ܰѱ غח ഛੋ೧ળ // SELECT ޙ ١ਸ ਊ೧ࢲ ೠߣ ؊ Ѩૐೞח Ѫਸ ୶ୌ } } Testing migrations (5) -
https://developer.android.com/topic/libraries/architecture/room.html Entites DAO Primary key @Insert, @Update, @Delete Passing a
collection of arguments Querying multiple tables Indices and uniqueness @Query Observable queries type converters Relationships Passing parameters into the query RxJava Nested objects Returning subsets of columns Direct cursor access -
Database ܳ ਊೞ۰ח ӝמ ݽ पઁ DBী غח ؘఠ Entity৬
Dao ূܻ ನੋ @Entity @Dao @Database -
ޥо ࠗೠоਃ? (2) android-architecture-components/GithubBrowserSample Lifecycle / LiveData / ViewModel Room
Dagger2 Fragment р ؘఠ ҕਬ -
ଵҊ উ٘۽٘ ইఃఫ о٘৬ ইఃఫ ஹಌքী ೧ ഛੋ೧ ࠁࣁਃ. Android
Architecture Components Introduction to Android Architecture Components Android Architecture Components android-architecture-componentsκϸϜϰΧϜϯᅻॲሑᇥΞΤΑΝ [Android Architecture Components] Lifecycle, LiveData and ViewModel ᓮḑ [Android Architecture Components] - Room ᓮḑ -
Thank you, reviewer - ӣకഐ (ҳӖ ܻই) - थ (Android
GDE) - ӂకജ (ঌೖܻই) - थ (٘ۄ݃ঙஹಌפ)