How to manage sets of Realm Objects


#1

I want to have two different sets of Realm Objects I want to have one set for Realm Objects that represent a Video that are in the users PlayList and a second set of Objects that are for Recently Played Videos. Any Realm Video Object could be in neither set, one of them or both of them. It looks like using a List is the most appropriate way to accomplish this, but I wanted to confirm this is the best approach. I create a List and I can append to it but when I do a count of the list it returns zero objects. Do I need to create a single Realm Object for Playlist that contains a List of Videos and then save that single record and another Recent Videos Object with a single record? Let me know what is the best practice to solve this problem.

-Scott


#2

Are you appending objects to the list in a write transaction?


#3

Not sure of your codebase but here’s an object in Swift that would do it

class UserClass: Object {
    @objc dynamic var user_id = NSUUID().uuidString
   @objc dynamic var name = ""

    let playList = List<VideoClass>()
    let recentlyPlayedList = List<VideoClass>()

    override static func primaryKey() -> String? {
        return "user_id"
    }
}

however you could also do the same thing with just a single property (warning - generic untested code follows)

Set up your VideoClass

class VideoClass: Object {
    @objc dynamic var video_name = ""
    @objc dynamic var last_played = "" //this will be a timestamp
}

then the UserClass with a single list

class UserClass: Object {
    @objc dynamic var user_id = NSUUID().uuidString
    @objc dynamic var name = ""

    let playList = List<VideoClass>()

    override static func primaryKey() -> String? {
        return "user_id"
    }

    func getRecentlyPlayed() -> Results<VideoClass> {
        let videoResults = self.playList.filter("last_played > two weeks ago")
        return videoResults
    }
}

So now all of your vides are stored in a single playlist and you can query for the most recently played videos out of that list.


#4

Yes I am. Following is the Swift Code I am Using:

// Writes Videos to local data source using RealmSwift for RecentVideos
class RecentVideos {
    static let singleton = RecentVideos()
    let videos = List<ETVideo>()
    
    var count : Int {
        get {
            return videos.count
        }
    }
    
func addVideo(video: ETVideo) {
        let realm = try! Realm()
        
        try! realm.write {
            videos.append(video)
        }
    }

    func getItem(at index: Int) -> ETVideo? {
        if (index < count) {
            return videos[index]
        } else {
            return nil
        }
    }
}

#5

RecentVideos is not managed by realm so it doesn’t make sense to modify it in a write transaction. List is a realm object but that doesn’t mean it’s automatically managed by realm.

You need to think clearly about what your object schema will be, possibly taking jay’s snippets as a starting point. I’d also suggest thoroughly reading the documentation https://realm.io/docs/swift/latest/#getting-started or even buying the book https://store.raywenderlich.com/products/realm-building-modern-swift-apps-with-realm-database