{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/2826","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2826/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2826/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2826/events","html_url":"https://github.com/realm/realm-cocoa/issues/2826","id":116062431,"number":2826,"title":"Object deleted exception in Realm","user":{"login":"pavlem","id":14887690,"avatar_url":"https://avatars.githubusercontent.com/u/14887690?v=3","gravatar_id":"","url":"https://api.github.com/users/pavlem","html_url":"https://github.com/pavlem","followers_url":"https://api.github.com/users/pavlem/followers","following_url":"https://api.github.com/users/pavlem/following{/other_user}","gists_url":"https://api.github.com/users/pavlem/gists{/gist_id}","starred_url":"https://api.github.com/users/pavlem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pavlem/subscriptions","organizations_url":"https://api.github.com/users/pavlem/orgs","repos_url":"https://api.github.com/users/pavlem/repos","events_url":"https://api.github.com/users/pavlem/events{/privacy}","received_events_url":"https://api.github.com/users/pavlem/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-10T08:42:45Z","updated_at":"2015-11-10T09:42:23Z","closed_at":null,"body":"Hi, \r\n\r\nI implemented Realm DB where in my app I have made a realm model and in some VC I used that model. \r\n\r\nIn a VC I have a button where I can SAVE and DELETE DB data.\r\n\r\nModel is \"FundDB: Object\" which has several properties like name, number, etc. \r\n\r\nIn VC I created \"fundDB\", populated it's properties and when I press ADD button I added it to the DB successfully (it's there when I go to another VC aimed to show DB items).\r\n\r\nWhen I press DELETE it does it successfully again (it's removed from VC aimed to show DB items).\r\n\r\nThe problem emerges when I add it then remove it and then add it again. I get an exception with following info **Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated**\r\n\r\n\r\nHere is the code part. \r\n\r\n\r\n\r\n```javascript\r\nclass FundDetailVC: UBSViewController {...\r\n    let fundDB = FundDB()\r\n...\r\n}\r\n```\r\n\r\n```javascript\r\noverride func viewDidLoad() {\r\n        super.viewDidLoad()\r\n\r\n...\r\n        fundDB.fundName = nameForFund\r\n        fundDB.fundIsin = isinForFund\r\n        fundDB.fundValor = valorForFund\r\n        fundDB.fundUrlRequest = urlPath\r\n\r\n}\r\n```\r\n\r\n\r\n```javascript\r\n    @IBAction func addToMyFundsButtonAction(sender: UIButton) {\r\n        \r\n        let fundDBRealmInstance = DB_HANDLER.getFund(primaryKey: self.isinForFund)\r\n               \r\n        if fundDBRealmInstance != nil {\r\n            print(self.fundDB.fundIsin)\r\n            DB_HANDLER.deleteFundFromDB(fundDBRealmInstance!)            \r\n        } else {        \r\n            let fundDBRealmObject = self.fundDB\r\n            DB_HANDLER.saveFundToDB(fundDBRealmObject)\r\n        }\r\n      }\r\n```\r\nDB_Handler is a helper class I created and which uses \r\n```javascript \r\n\r\n private func writeToDB(dbObject: Object) {\r\n        do {\r\n            try uiRealm.write({ () -> Void in\r\n                uiRealm.add(dbObject)\r\n            })\r\n        } catch {\r\n            print(\"DB Realm insert failed\")\r\n        }\r\n    }\r\n   \r\n    private func deleteFromDB(dbObject: Object) {\r\n        \r\n        do {\r\n            try uiRealm.write({ () -> Void in\r\n                uiRealm.delete(dbObject)\r\n            })\r\n        } catch {\r\n            print(\"DB Realm insert failed\")\r\n        }\r\n    }\r\n```\r\n\r\nFor some reason fundDB property in my VC doesn't exist any more. \r\n\r\nCan you please give me some light on the matter :)\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/155371331","html_url":"https://github.com/realm/realm-cocoa/issues/2826#issuecomment-155371331","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2826","id":155371331,"user":{"login":"pavlem","id":14887690,"avatar_url":"https://avatars.githubusercontent.com/u/14887690?v=3","gravatar_id":"","url":"https://api.github.com/users/pavlem","html_url":"https://github.com/pavlem","followers_url":"https://api.github.com/users/pavlem/followers","following_url":"https://api.github.com/users/pavlem/following{/other_user}","gists_url":"https://api.github.com/users/pavlem/gists{/gist_id}","starred_url":"https://api.github.com/users/pavlem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pavlem/subscriptions","organizations_url":"https://api.github.com/users/pavlem/orgs","repos_url":"https://api.github.com/users/pavlem/repos","events_url":"https://api.github.com/users/pavlem/events{/privacy}","received_events_url":"https://api.github.com/users/pavlem/received_events","type":"User","site_admin":false},"created_at":"2015-11-10T09:42:23Z","updated_at":"2015-11-10T09:42:23Z","body":"I resolved this by replacing \r\n\r\n```javascript\r\n else {        \r\n            let fundDBRealmObject = self.fundDB\r\n            DB_HANDLER.saveFundToDB(fundDBRealmObject)\r\n        }\r\n```\r\nwith \r\n\r\n```javascript\r\nelse {\r\n          let fundDBRealmObject = FundDB(value: self.fundDB)\r\n          DB_HANDLER.saveFundToDB(fundDBRealmObject)\r\n        }\r\n```\r\n\r\nAs I see, when deleting fundDB Realm object from default.realm DB file on disk also deleted fundDB instance (property) on my VC. \r\n"}}