Saving Data on Android


Understanding Room migra/ons



Download 19,28 Mb.
Pdf ko'rish
bet182/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   178   179   180   181   182   183   184   185   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Understanding Room migra/ons
SQLite handles database migrations by specifying a version number for each 
database schema you create. In other words, each time you modify your database 
schema by creating, deleting or updating a table, you have to increase the database 
version number and modify 
SQLiteOpenHelper.onUpgrade()

onUpGrade()
 will tell 
SQLite what to do when one database version changes to another.
For example, say one of your users still has database version 1 but a new update of 
your app now uses database version 2. SQLite would realize that the current database 
version is obsolete and needs an upgrade. Then, SQLite would look for  
SQLiteOpenHelper.onUpgrade(db, 1, 2)
 and trigger its body to migrate to the 
new schema. If 
SQLiteOpenHelper.onUpgrade(db, 1, 2)
 doesn't exist, it will 
trigger an error.
Saving Data on Android
Chapter 10: Migrations with Room
raywenderlich.com
173


Room migrations work in a very similar way. The difference is that Room provides an 
abstraction layer on top of the traditional SQLite methods with a 
Migration
 class.
Migration(startVersion, endVersion)
 is the base class of a database migration; 
it can move between 
any
 two migrations defined by the 
startVersion
 and 
endVersion
 parameters. The reason I've emphasized 
any
 is because you don't 
necessarily need to specify a sequential migration. For example, say Room opens 
database version 2 and the latest version is 5. Normally, Room would execute 
migrations in this order:
Migration(2, 3)
Migration(3, 4)
Migration(4, 5)
The beauty of Room is that you can also specify a 
Migration
 that goes directly from 
version 2 to version 5 like this: 
Migration(2, 5)
, which makes the migration 
process much faster. Of course, there won't always be a direct path from migration X 
to migration Y, so executing all you migrations one by one might be necessary, but 
it's usually a good practice to specify a direct migration if possible.
If you don't specify an appropriate migration for the current database version, Room 
will throw a runtime error and the app will crash.
Note
: You can also call 
fallbackToDestructiveMigration()
 when building 
your database. This will tell Room to destructively recreate tables if you 
haven't specified a migration. The advantage is that you won't need to create 
any migrations and your app won't crash. The disadvantage is that you will 
delete your data every time you specify a new database version.
Now that you know the theory, you can move on to creating your first Room 
migrations!

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   178   179   180   181   182   183   184   185   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish