{"action":"created","issue":{"url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1022","labels_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1022/labels{/name}","comments_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1022/comments","events_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1022/events","html_url":"https://github.com/magicalpanda/MagicalRecord/issues/1022","id":84619307,"number":1022,"title":"saveWithBlock doesn't find existing entities with MR_FindFirstByAttribute - Swift","user":{"login":"whipsterCZ","id":567430,"avatar_url":"https://avatars.githubusercontent.com/u/567430?v=3","gravatar_id":"","url":"https://api.github.com/users/whipsterCZ","html_url":"https://github.com/whipsterCZ","followers_url":"https://api.github.com/users/whipsterCZ/followers","following_url":"https://api.github.com/users/whipsterCZ/following{/other_user}","gists_url":"https://api.github.com/users/whipsterCZ/gists{/gist_id}","starred_url":"https://api.github.com/users/whipsterCZ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/whipsterCZ/subscriptions","organizations_url":"https://api.github.com/users/whipsterCZ/orgs","repos_url":"https://api.github.com/users/whipsterCZ/repos","events_url":"https://api.github.com/users/whipsterCZ/events{/privacy}","received_events_url":"https://api.github.com/users/whipsterCZ/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-03T15:23:44Z","updated_at":"2015-06-03T15:24:07Z","closed_at":null,"body":"I'm trying to import entities with this block\r\n\r\n     MagicalRecord.saveWithBlock({   (moc: NSManagedObjectContext!) in                \r\n                               \r\n          //parsing inquiries\r\n          for (index: String, jsonInquiry: JSON) in json[\"data\"][\"inquiries\"] {\r\n                    \r\n               var idInquiry =  jsonInquiry[\"id_inquiry\"].int! \r\n               var inquiry: Inquiry\r\n                  \r\n               //trying to find existing entity\r\n               if let existingInquiry = Inquiry.MR_findFirstByAttribute(\"id\", withValue: idInquiry, inContext: moc) as? Inquiry {\r\n                    inquiry = existingInquiry\r\n                    println(\"fetching existing inquiry: \\(idInquiry)\")\r\n                } else {\r\n                    inquiry = Inquiry.MR_createInContext(moc) as! Inquiry\r\n                    println(\"creating new inquiry: \\(idInquiry)\")\r\n                }\r\n                ...\r\n           }\r\n      }    \r\n\r\n> Only first entity is found as **existing**, others are **created as new**\r\n\r\nBut when I preFetch all entities at once with:\r\n\r\n     MagicalRecord.saveWithBlock({  (moc: NSManagedObjectContext!) in\r\n            \r\n          //preFetch all entities        \r\n          var existingInquiries = [Int:Inquiry]()\r\n          for item in Inquiry.MR_findAllInContext(moc) {\r\n               if let existingItem = item as? Inquiry {\r\n                    existingInquiries[ existingItem.id ] = existingItem\r\n               }\r\n          }\r\n                \r\n          //parsing inquiries\r\n          for (index: String, jsonInquiry: JSON) in json[\"data\"][\"inquiries\"] {\r\n               ....     \r\n               var idInquiry =  jsonInquiry[\"id_inquiry\"].int! \r\n               var inquiry: Inquiry\r\n                    \r\n               if let existingInquiry = existingInquiries[idInquiry] {\r\n                    inquiry = existingInquiry\r\n                        println(\"fetching existing inquiry: \\(idInquiry)\")\r\n                    } else {\r\n                        inquiry = Inquiry.MR_createInContext(moc) as! Inquiry\r\n                        println(\"fetching new inquiry: \\(idInquiry)\")\r\n                    }\r\n              }\r\n              ....\r\n       }\r\n\r\n> all entities are found as **existing**\r\n\r\n\r\nCan someone find my mistake or bug? maybe Im missing something basic. \r\nI'm struggling with this for hours \r\n\r\nThanks\r\n\r\nI use XCODE Version 6.3 (6D570) and Swift 1.2"},"comment":{"url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/comments/108481683","html_url":"https://github.com/magicalpanda/MagicalRecord/issues/1022#issuecomment-108481683","issue_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1022","id":108481683,"user":{"login":"whipsterCZ","id":567430,"avatar_url":"https://avatars.githubusercontent.com/u/567430?v=3","gravatar_id":"","url":"https://api.github.com/users/whipsterCZ","html_url":"https://github.com/whipsterCZ","followers_url":"https://api.github.com/users/whipsterCZ/followers","following_url":"https://api.github.com/users/whipsterCZ/following{/other_user}","gists_url":"https://api.github.com/users/whipsterCZ/gists{/gist_id}","starred_url":"https://api.github.com/users/whipsterCZ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/whipsterCZ/subscriptions","organizations_url":"https://api.github.com/users/whipsterCZ/orgs","repos_url":"https://api.github.com/users/whipsterCZ/repos","events_url":"https://api.github.com/users/whipsterCZ/events{/privacy}","received_events_url":"https://api.github.com/users/whipsterCZ/received_events","type":"User","site_admin":false},"created_at":"2015-06-03T15:24:07Z","updated_at":"2015-06-03T15:24:07Z","body":"StackOverflow post: http://stackoverflow.com/questions/30623847/magicalrecord-in-method-savewithblock-doesnt-find-existing-entities-with-mr-fin"}}