简体   繁体   中英

Using Realm with swift

I'm quite familiar with Sqlite, but decided to try using realm for my next project. I'm having trouble with reading data from the db and deleting objects as well.

I'm using the default realm path:

let realm = RLMRealm.defaultRealm()

When a button is pressed an RLMObject should either be added or deleted (if already there). This is the IBAction for the button:

@IBAction func addToFavor(sender: UIBarButtonItem) {

    // Create RealmTV (RLMObject)
    let tvShow = RealmTV(id: id, title: TitleLabel.text!, posterPath: posterUrl)

    if favoriteButton.image!.isEqual(UIImage(named: "Favor unfilled")) {

        realm.beginWriteTransaction()
        // Create or update tv-show in database 
        RealmTV.createOrUpdateInDefaultRealmWithValue(tvShow)

        try! realm.commitWriteTransaction()

        // Change button state
        favoriteButton.image = UIImage(named: "Favor filled")
    }
    else
    {

        realm.beginWriteTransaction()

        // Delete tv-show object from database
        realm.deleteObject(tvShow)          /*   RLMException here   */

        try! realm.commitWriteTransaction()

        // Change button state
        favoriteButton.image = UIImage(named: "Favor unfilled")
    }

}

When I try to delete the object after it has been added to db. I get an RLMExecption saying:

'Can only delete an object from the Realm it belongs to.'

I understand what the above reason mean, but not how to solve it?

And also how do I retrieve only this object from db after it has been added?

EDIT

This is my RealmTv class:

   import UIKit
   import Realm

   class RealmTV: RLMObject {
    dynamic var id = ""
    dynamic var title = ""
    dynamic var posterPath = ""

    override class func primaryKey() -> String? {
        return "id"
    }

    override init() {
        super.init()
    }

    init(id: String, title: String, posterPath: String) {
        super.init()

        self.id = id
        self.title = title
        self.posterPath = posterPath

       }
     }

What the error message is trying to convey is that the object you pass to -[RLMRealm deleteObject:] must belong to the Realm that you're trying to delete the object from. In your case you're passing a new object that does not belong to any Realm (such an object is referred to as a standalone or unpersisted object in Realm's documentation). Instead you must pass either an object that you have retrieved from the Realm (using -[RLMRealm objectForPrimaryKey:] , +[RLMObject allObjectsInRealm:] , etc.), or added to the Realm (using -[RLMRealm addObject:] ).

Reworking your code to meet these requirements would look something like:

if favoriteButton.image!.isEqual(UIImage(named: "Favor unfilled")) {
    realm.beginWriteTransaction()
    // Create or update tv-show in database 
    let tvShow = RealmTV(id: id, title: TitleLabel.text!, posterPath: posterUrl)
    RealmTV.createOrUpdateInDefaultRealmWithValue(tvShow)

    try! realm.commitWriteTransaction()

    // Change button state
    favoriteButton.image = UIImage(named: "Favor filled")
}
else {
    realm.beginWriteTransaction()

    // Delete tv-show object from database
    let tvShow = RealmTV.objectForPrimaryKey(id)
    realm.deleteObject(tvShow)

    try! realm.commitWriteTransaction()

    // Change button state
    favoriteButton.image = UIImage(named: "Favor unfilled")
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM