一对多关系删除 [英] Deleting with one-to-many relationship

查看:114
本文介绍了一对多关系删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对多的关系:

class GameSystem: Object {
dynamic var gameSystemName = ""
}

class games: Object {
dynamic var gameSystemName = gameSystemName().name
dynamic var gameTitle = ""
dynamic var gameGenre = ""
}

gameSystemName当前显示在TableView上.如果用户删除了gameSystemName,我希望将该gameSystemName连同该系统的所有游戏一起删除.

The gameSystemNames are currently displayed on a TableView. If the user deletes a gameSystemName, I want that gameSystemName along with all of that system's games deleted.

我当前使用的代码只会删除GameSystem,但会保留所有游戏.

The code I'm currently using will only delete the GameSystem, but leaves all the games.

    func deleteRowAtIndexPath(indexPath: NSIndexPath) {
    let realm = Realm()
    let objectToDelete = gameSystems[indexPath.row]
    realm.write {
        realm.delete(objectToDelete)
    }
    gameSystemTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    gameSystems = Realm(path: Realm.defaultPath).objects(GameSystem)
}

我认为有一种简单的方法可以做到这一点.

I'm assuming there's a simple way to do this.

推荐答案

如果保持模型不变,解决方案将是首先查询关系Game的相关对象:

If you keep your model as it is, the solution would be to query first for the relevant objects of the relation Game:

// …
let objectToDelete = gameSystems[indexPath.row]
let gameSystemName = objectToDelete.gameSystemName
realm.write {
    let games = realm.objects(Game).filter("gameSystemName = ?", gameSystemName)
    realm.delete(games)
    realm.delete(objectToDelete)
}
// …

模型推荐

我建议您将显式链接添加到模型中,而不要通过松散链接的外键来表达关系.但是对象映射非常个人化,可能取决于超出您的问题和答案范围之外的其他约束.进一步参考,如下所示:

Model Recommendation

I'd propose instead that you add an explicit link to your model, instead of expressing the relationship through a loosely linked foreign key. But the object-mapping is very individual and may be dependent on further constraints going beyond the scope of your question and this answer. For further reference, that would look like below:

 class GameSystem : Object {
      dynamic var name = ""
      let games = List<Game>()
 }

 class Game : Object {
      dynamic var title = ""
      dynamic var genre = ""

      // Use a backlink
      // (https://realm.io/docs/swift/latest/#inverse-relationships)
      dynamic var gameSystem: GameSystem? {
           return linkingObjects(GameSystem.self, forProperty: "games").first
      }
 }

如果您这样设置模型,则可以非常轻松地删除游戏:

If you setup your model like this, you can delete your games very easy:

// …
let objectToDelete = gameSystems[indexPath.row]
realm.write {
    realm.delete(objectToDelete.games)
    realm.delete(objectToDelete)
}
// …

注意:将来,Realm将把级联删除"作为功能.发行后,您甚至无需手动删除关联的游戏,而是可以声明模型中的牢固关系,以便自动删除游戏.

Note: In the future, Realm will bring Cascading Deletes as feature. Once that is released, you won't even need to take care of the manual deletion of associated games, but you will rather be able to declare the strong relationship in your model, so that the Games are automatically deleted.

您也可以声明您的链接,反之亦然,但是将来可能会更难使用级联删除之类的功能.但是,现在将其删除的代码与上面的代码相同.

You can also declare your link vice-versa, but that would make it likely harder to use a feature like Cascading Deletes in the future. However the code to delete them for now would look the same as above.

 class GameSystem : Object {
      dynamic var name = ""

      // Use a backlink
      // (https://realm.io/docs/swift/latest/#inverse-relationships)
      dynamic var games: [Game] {
           return linkingObjects(Game.self, forProperty: "gameSystem")
      }
 }

 class Game : Object {
      dynamic var title = ""
      dynamic var genre = ""
      let gameSystem: GameSystem? = nil
 }

这篇关于一对多关系删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆