【问题标题】:Object has been deleted or invalidated对象已被删除或失效
【发布时间】:2016-07-01 15:40:19
【问题描述】:

这个问题已经列出了很多,但我看不到适合我的情况的问题。我有一个功能可以在收藏夹中添加和删除音乐。我可以先添加它,也可以删除它。但是在我再次添加它之后。繁荣!!!请任何人帮助我。谢谢!

下面抛出错误:

*** 由于未捕获的异常“RLMException”而终止应用程序,原因:“不允许将已删除或无效的对象添加到领域”

请帮我检查下面的代码:

func addMusicToFavorite(music: Music) {
    let realm = try! Realm()

    try! realm.write {
        realm.add(music)
    }

}

func removeMusicFromFavorite(music: Music) {
    //Tips: '\(musics.mId)' : put quote ' ' like this means String, if not it will treat it as Int
    let realm = try! Realm()
    let tmpMusic = realm.objects(Music.self).filter("_mId == '\(music.mId)'")
    try! realm.write {
        print("remove \(music.mId)")
        realm.delete(tmpMusic)
    }
}

我的音乐模特:

class Music: Object {

private dynamic var _mId: String!
private dynamic var _mTitle: String!
private dynamic var _mArtist: String!
private dynamic var _mThumbnail: String!
private dynamic var _mAlbumImg: String!
private dynamic var _mPath: String!
private dynamic var _mNumOfView: String!
private dynamic var _mDuration: String!

var mId: String {
    return _mId
}

var mTitle: String {
    return _mTitle
}

var mArtist: String {
    return _mArtist
}

var mAlbumImg: String {
    return _mAlbumImg
}

var mThumbnail: String {
    return _mThumbnail
}

var mPath: String {
    return _mPath
}

var mNumOfView: String {
    return _mNumOfView
}

var mDuration: String {
    return _mDuration
}

required init() {
    super.init()
}

required init(realm: RLMRealm, schema: RLMObjectSchema) {
    super.init(realm: realm, schema: schema)

// fatalError("init(realm:schema:) 尚未实现") }

required init(value: AnyObject, schema: RLMSchema) {
    super.init(value: value, schema: schema)

// fatalError("init(value:schema:) 尚未实现") }

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


convenience init(dict: Dictionary<String, AnyObject>) {
    self.init()

    if let id = dict["id"] as? String {
        self._mId = id
    }

    if let titleAndArtist = dict["music_title"] as? String {
        let tmp = Utils.instance.splitStringsWithCharacter(titleAndArtist)
        self._mTitle = tmp.title
        self._mArtist = tmp.artist            
    }

    if let thumbnail = dict["image_thumb"] as? String {
        self._mThumbnail = thumbnail
    }

    if let albumImg = dict["image_album"] as? String {
        self._mAlbumImg = albumImg
    }

    if let path = dict["music_path"] as? String {
        self._mPath = path
    }

    if let numOfView = dict["music_view"] as? String {
        self._mNumOfView = numOfView
    }

    if let duration = dict["music_duration"] as? String {
        self._mDuration = duration
    }

}

}

【问题讨论】:

  • 你能展示Music对象的模型吗?
  • 嗨 xoudini,感谢您的评论,请检查我编辑的添加了音乐模型的问题...谢谢!
  • 你能评价一下“Boom!!!”吗? ;) Realm 是否抛出错误,应用程序是否终止,...?
  • 嗨,哈哈...听起来很有趣...应用程序被终止并抛出领域异常,原因是:对象已失效或删除...就像这样...抱歉不清楚问题,我很快就会编辑我的问题...谢谢...:)
  • 请检查我编辑的问题是否抛出错误。 @HAS

标签: swift realm


【解决方案1】:

只是为了记录,因为答案隐藏在所有 cmets 中:一旦从领域中删除它,您就不能重新add 相同的对象实例

realm.add 持久化给定的对象。此类对象的属性在此调用后直接在磁盘上的数据库中进行管理,而不仅仅是在普通内存中。

但是,如果您保留非托管对象并使用realm.create(object) 在 Realm 中创建一个副本,则即使在删除以这种方式创建的对象之后,您也可以重复该操作。

【讨论】:

    猜你喜欢
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多