{"action":"created","issue":{"url":"https://api.github.com/repos/ffried/FFCoreData/issues/18","repository_url":"https://api.github.com/repos/ffried/FFCoreData","labels_url":"https://api.github.com/repos/ffried/FFCoreData/issues/18/labels{/name}","comments_url":"https://api.github.com/repos/ffried/FFCoreData/issues/18/comments","events_url":"https://api.github.com/repos/ffried/FFCoreData/issues/18/events","html_url":"https://github.com/ffried/FFCoreData/issues/18","id":315375476,"number":18,"title":"Decodable Object prevent duplicates","user":{"login":"darnat","id":9109697,"avatar_url":"https://avatars1.githubusercontent.com/u/9109697?v=4","gravatar_id":"","url":"https://api.github.com/users/darnat","html_url":"https://github.com/darnat","followers_url":"https://api.github.com/users/darnat/followers","following_url":"https://api.github.com/users/darnat/following{/other_user}","gists_url":"https://api.github.com/users/darnat/gists{/gist_id}","starred_url":"https://api.github.com/users/darnat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/darnat/subscriptions","organizations_url":"https://api.github.com/users/darnat/orgs","repos_url":"https://api.github.com/users/darnat/repos","events_url":"https://api.github.com/users/darnat/events{/privacy}","received_events_url":"https://api.github.com/users/darnat/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-04-18T08:13:32Z","updated_at":"2018-04-18T09:07:40Z","closed_at":null,"author_association":"NONE","body":"The code works perfectly fine, but something is missing.\r\nWhat if when I'm tryin to **decode** a JSON with my **NSManagedObject Codable Model**, but this object already exist. I mean to say for exemple that I already have in my Database an **Object** with the same **primaryKey**. And I don't want to have a _duplicate_, just **update** the existing one.\r\n\r\nSince I don't really have the control when init(from decoder: Decoder) is called. How can I perform an NSRequest and return the object if it exist or create a new One. Exactly like the Person and its dogs but at the root level ?"},"comment":{"url":"https://api.github.com/repos/ffried/FFCoreData/issues/comments/382318696","html_url":"https://github.com/ffried/FFCoreData/issues/18#issuecomment-382318696","issue_url":"https://api.github.com/repos/ffried/FFCoreData/issues/18","id":382318696,"user":{"login":"ffried","id":1689782,"avatar_url":"https://avatars0.githubusercontent.com/u/1689782?v=4","gravatar_id":"","url":"https://api.github.com/users/ffried","html_url":"https://github.com/ffried","followers_url":"https://api.github.com/users/ffried/followers","following_url":"https://api.github.com/users/ffried/following{/other_user}","gists_url":"https://api.github.com/users/ffried/gists{/gist_id}","starred_url":"https://api.github.com/users/ffried/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ffried/subscriptions","organizations_url":"https://api.github.com/users/ffried/orgs","repos_url":"https://api.github.com/users/ffried/repos","events_url":"https://api.github.com/users/ffried/events{/privacy}","received_events_url":"https://api.github.com/users/ffried/received_events","type":"User","site_admin":false},"created_at":"2018-04-18T09:07:40Z","updated_at":"2018-04-18T09:07:40Z","author_association":"OWNER","body":"That's exactly what [`findOrCreate(for:in:)`](https://github.com/ffried/FFCoreData/blob/master/FFCoreData/FFCoreData/NSManagedObject/CoreDataDecodable.swift#L27) is for. While the default implementation fetches a \"singleton-like\" object and updates it, you can always implement it in your entities and fetch an object with the id of the DTO.\r\n\r\nOf course if this happens a the root level, you'd need to manually decode the DTO and then pass it to `findOrCreate(for:in:)`. I might add some convenience extensions / functions for that. Meanwhile you can use something like this:\r\n\r\n```swift\r\nextension JSONDecoder {\r\n    final func decode<Entity: CoreDataDecodable>(_: Entity.Type, from data: Data) throws -> Entity {\r\n        return try .findOrCreate(for: decode(Entity.DTO.self, from: data), in: .decodingContext())\r\n    }\r\n}\r\n```"}}
{"actor":{"display_login":"ffried"}}