{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/2123","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2123/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2123/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2123/events","html_url":"https://github.com/realm/realm-cocoa/issues/2123","id":90931468,"number":2123,"title":"Uniqueness validation","user":{"login":"stefanvermaas","id":1470863,"avatar_url":"https://avatars.githubusercontent.com/u/1470863?v=3","gravatar_id":"","url":"https://api.github.com/users/stefanvermaas","html_url":"https://github.com/stefanvermaas","followers_url":"https://api.github.com/users/stefanvermaas/followers","following_url":"https://api.github.com/users/stefanvermaas/following{/other_user}","gists_url":"https://api.github.com/users/stefanvermaas/gists{/gist_id}","starred_url":"https://api.github.com/users/stefanvermaas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stefanvermaas/subscriptions","organizations_url":"https://api.github.com/users/stefanvermaas/orgs","repos_url":"https://api.github.com/users/stefanvermaas/repos","events_url":"https://api.github.com/users/stefanvermaas/events{/privacy}","received_events_url":"https://api.github.com/users/stefanvermaas/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-cocoa/labels/pending","name":"pending","color":"ededed"}],"state":"open","locked":false,"assignee":{"login":"segiddins","id":1946610,"avatar_url":"https://avatars.githubusercontent.com/u/1946610?v=3","gravatar_id":"","url":"https://api.github.com/users/segiddins","html_url":"https://github.com/segiddins","followers_url":"https://api.github.com/users/segiddins/followers","following_url":"https://api.github.com/users/segiddins/following{/other_user}","gists_url":"https://api.github.com/users/segiddins/gists{/gist_id}","starred_url":"https://api.github.com/users/segiddins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/segiddins/subscriptions","organizations_url":"https://api.github.com/users/segiddins/orgs","repos_url":"https://api.github.com/users/segiddins/repos","events_url":"https://api.github.com/users/segiddins/events{/privacy}","received_events_url":"https://api.github.com/users/segiddins/received_events","type":"User","site_admin":false},"milestone":null,"comments":9,"created_at":"2015-06-25T11:10:16Z","updated_at":"2015-06-29T11:36:15Z","closed_at":null,"body":"Hi guys,\r\n\r\nI pretty new to iOS development, so sorry if this is some sort of rookie mistake, but I've got some problems with uniqueness validations.\r\n\r\nI've got this tableView that lists all the current conversations. I'm now using Realm to store the conversations on the device, though I'm running into an issue with the primary_key.\r\n\r\nEvery time the view is loaded/opened I'm looking up the latest conversations from my web API. This web API then returns all the conversations for an user. As this respons returns to the app, I want to add them to the Realm store. \r\n\r\n```swift\r\n  private func getAllConversations() {\r\n    Alamofire.request(Router.All(\"conversations\"))\r\n      .validate(statusCode: 200..<300)\r\n      .responseJSON { (_, _, rawConversations, error) in\r\n        if let theError = error { return }\r\n        \r\n        // Add all conversations to the Realm store\r\n        let theConversations = JSON(rawConversations!)\r\n        for (id, c) in theConversations {\r\n          let conversation = self.newConversation(c)\r\n          RealmManager.sharedInstance.add(conversation)\r\n        }\r\n    }\r\n  }\r\n  \r\n  private func newConversation(c: JSON!) -> Conversation {\r\n    let conversation = Conversation()\r\n    \r\n    // Add the values to the conversation\r\n    conversation.id         = c[\"id\"].intValue\r\n    conversation.title      = c[\"title\"].stringValue\r\n    conversation.timestamp  = c[\"timestamp\"].intValue\r\n    \r\n    // Return the conversation\r\n    return conversation\r\n  }\r\n```\r\n\r\nThough using this an exception will be thrown because I've already added the conversation to the store. Is it possible to catch this exception (I believe it's not in swift right now). Or is there an workaround?\r\n\r\nI was thinking of checking myself or an conversation already existed or not, but this is a bit ugly, because Realm is already doing this. Shouldn't Realm fail silently or make it possible to handle the exception?!\r\n\r\nCheers!"},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/116622875","html_url":"https://github.com/realm/realm-cocoa/issues/2123#issuecomment-116622875","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2123","id":116622875,"user":{"login":"stefanvermaas","id":1470863,"avatar_url":"https://avatars.githubusercontent.com/u/1470863?v=3","gravatar_id":"","url":"https://api.github.com/users/stefanvermaas","html_url":"https://github.com/stefanvermaas","followers_url":"https://api.github.com/users/stefanvermaas/followers","following_url":"https://api.github.com/users/stefanvermaas/following{/other_user}","gists_url":"https://api.github.com/users/stefanvermaas/gists{/gist_id}","starred_url":"https://api.github.com/users/stefanvermaas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stefanvermaas/subscriptions","organizations_url":"https://api.github.com/users/stefanvermaas/orgs","repos_url":"https://api.github.com/users/stefanvermaas/repos","events_url":"https://api.github.com/users/stefanvermaas/events{/privacy}","received_events_url":"https://api.github.com/users/stefanvermaas/received_events","type":"User","site_admin":false},"created_at":"2015-06-29T11:36:15Z","updated_at":"2015-06-29T11:36:15Z","body":"Both thanks for pointing this out! I'll try it in a second. \r\n\r\n@NicholasTD07: I'm only using this in the (UI) default thread and not in a background thread, so this should be fine, right?! Or am I interpreting your suggestion wrongly?"}}