I am trying to create a TV Series schema with a list of available Seasons (using RealmJS), I would like to not add duplicate season numbers to the Seasons[]
list.
This is what I have:
TvSeries.schema = {
name: 'TvSeries',
primaryKey: 'mediaId',
properties: {
mediaId: 'string',
seasons: 'Seasons[]'
}
}
Seasons.schema = {
name: 'Seasons',
primaryKey: 'seasonNumber',
properties: {
seasonNumber: 'int'
}
}
realm.write(() => {
let season = realm.create('Seasons', { seasonNumber: seasonNumber }, true)
let tvShow = realm.create('TvSeries', mediaObject, true)
let seasonsList = tvShow.seasons
seasonsList.push(season)
})
When I add a new episode (not shown here), it will add the TV Series data and Season again and as there are multiple episodes in a season, it will add to the Seasons[]
list multiple times.
My thoughts would be to go through the Seasons[]
list and check if it exists, if not then push a new season to it.
Is there a better way of doing this?
I would just do a quick check to see if the list already contains the season before inserting:
realm.write(() => {
let season = realm.create('Seasons', { seasonNumber: seasonNumber }, true)
let tvShow = realm.create('TvSeries', mediaObject, true)
let seasonsList = tvShow.seasons
if (seasonsList.filtered("seasonNumber == $0", season.seasonNumber).length == 0) {
seasonsList.push(season)
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.