Saving Data on Android


NO_ACTION : If the parent record is deleted or modified, no action is taken. 3. RESTRICT



Download 19,28 Mb.
Pdf ko'rish
bet139/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   135   136   137   138   139   140   141   142   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

NO_ACTION
: If the parent record is deleted or modified, no action is taken.
3.
RESTRICT
: This constraint means that, if a record in the parent entity has one or 
more records mapped to it in the child entity, the app is prohibited from deleting 
or updating the parent record.
Saving Data on Android
Chapter 7: Mastering Relations
raywenderlich.com
126


4.
SET_DEFAULT
: If the parent record is deleted, the foreign key in the child record 
gets a default value.
5.
SET_NULL
: If the parent record is deleted or updated, the foreign key in the child 
record gets a NULL value.
One important thing to remember is that, if you define a foreign key constraint, 
SQLite requires that you create a unique index in the parent entity for the mapped 
columns. It is also recommended in the documentation that you create an index on 
the child table to avoid full table scans when the parent table is updated. If you don't, 
Room will throw a compile time warning.
Therefore, modify your annotation like this:
@Entity(tableName = "answer", 
    foreignKeys = [ 
        ForeignKey(entity = Question::class, 
            parentColumns = ["question_id"], 
            childColumns = ["question_id"], 
            onDelete = CASCADE)
 
    ], 
    indices = [Index(
"question_id"
)])
//only this line changes
You will need to add the import for 
androidx.room.Index
, or you can just import 
andoidx.room.*
.
The 
indices
 property allows you to define an index for one or more columns in your 
entity by passing an array with the column names. This takes care of the index for 
the child entity, now you need to define it for the parent entity.
Open 
Question.kt
  and modify the 
@Entity
 annotation like below:
@Entity(tableName = "question", indices = 
[Index("question_id")
])
Again, you will need to add the import for 
androidx.room.Index
.
Just like the 
Answer
 entity, this code is telling Room that you want to create an index 
for the 
question_id
 primary key field.
Build and run your app to verify everything is working properly.
Saving Data on Android
Chapter 7: Mastering Relations
raywenderlich.com
127


Now, say you want to retrieve a list of all the questions with their respective answers. 
To do this, you would need to write two different queries: One to retrieve the list of 
all the questions and another to retrieve the answers based on the 
question_id

Your Daos would look like this:
@Query("SELECT * FROM question ORDER BY question_id")
 
fun
 
getAllQuestions
()
: LiveData
 
@Query("SELECT * FROM answer WHERE question_id = :questionId")
 
fun
 
getAnswersForQuestion
(questionId: Int)
: List
While the above approach is not bad, Room offers a better way to work with 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   135   136   137   138   139   140   141   142   ...   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