{"id":386703,"date":"2024-06-29T06:52:45","date_gmt":"2024-06-29T06:52:45","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=386703"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=386703","title":{"rendered":"<span>\u0422\u0438\u0448\u0438\u043d\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435! \u041a\u0430\u043a \u043c\u044b \u0440\u0435\u0444\u0430\u0447\u0438\u043b\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 API \u0438 \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0441\u0432\u043e\u0439 Repository<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0418\u0433\u043e\u0440\u044c \u0421\u043e\u0440\u043e\u043a\u0438\u043d. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u0434\u0435\u043b\u044e\u0441\u044c \u0438\u0441\u0442\u043e\u0440\u0438\u0435\u0439 \u043e \u0442\u043e\u043c, \u043a\u0443\u0434\u0430 \u043d\u0430\u0441 \u0437\u0430\u0432\u0451\u043b \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433, \u043a\u0430\u043a \u043c\u044b \u043e\u0442\u0442\u0443\u0434\u0430 \u0432\u044b\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u043f\u043e\u043f\u0443\u0442\u043d\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0432 \u0441\u043b\u043e\u0439 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u0422\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043f\u043e\u043a\u0430\u0436\u0443 \u043f\u043e\u0434\u0445\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0438 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u0445, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u044b \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0438\u0441\u044c. \u041d\u043e \u043e\u0431\u043e \u0432\u0441\u0451\u043c \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.<\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430, \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u0434\u0443\u043c\u0430\u043b\u0438\u0441\u044c \u043e \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0438 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0441\u0432\u043e\u0451 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0435\u0451, \u043d\u043e \u0431\u0435\u0437 \u0435\u0434\u0438\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0432\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u043e, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0432\u044b \u0441\u0447\u0430\u0441\u0442\u043b\u0438\u0432\u044b\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f57\/c15\/b8e\/f57c15b8e793be5d8c5dca49240f9d83.png\" width=\"1560\" height=\"880\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f57\/c15\/b8e\/f57c15b8e793be5d8c5dca49240f9d83.png\"\/><figcaption><\/figcaption><\/figure>\n<h2>\u041f\u0440\u0435\u0434\u044b\u0441\u0442\u043e\u0440\u0438\u044f<\/h2>\n<p>\u0418\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438 \u0442\u0430\u043a \u0441\u043b\u043e\u0436\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u042e\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f RestKit \u2014 \u043c\u043e\u0449\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0430\u044f \u0435\u0449\u0451 \u043d\u0430 \u0441\u0442\u0430\u0440\u043e\u043c \u0434\u043e\u0431\u0440\u043e\u043c Objective-C. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c api-\u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0434\u0435\u043a\u043e\u0434\u0438\u0442\u044c JSON \u0432 NSManagedObject \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438 \u0442\u0443\u0442 \u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438\u0445 \u0432 CoreData. \u041e\u0434\u043d\u0430\u043a\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043d\u0435\u0443\u043c\u043e\u043b\u0438\u043c\u043e \u043f\u043e\u0441\u0442\u0430\u0440\u0435\u043b\u0430, \u0430 \u0435\u0451 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0438 \u0432\u043e\u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0430. \u0414\u0430 \u0438 \u0440\u0435\u0431\u044f\u0442\u0430 \u0432 iOS-\u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u043d\u0435\u043e\u0445\u043e\u0442\u043d\u043e \u0435\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c.<\/p>\n<p>\u0422\u0430\u043a, \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u043d\u0430 \u043f\u044f\u0442\u043d\u0438\u0447\u043d\u043e\u0439 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0432 \u043e\u0444\u0438\u0441\u0435 \u0437\u0430 \u043a\u0440\u0443\u0433\u043b\u044b\u043c \u0441\u0442\u043e\u043b\u043e\u043c \u0438 \u0441\u043e \u0432\u043a\u0443\u0441\u043d\u043e\u0439 \u043f\u0438\u0446\u0446\u0435\u0439, \u043c\u044b \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0448\u0438\u043b\u0438 \u0432\u044b\u043f\u0438\u043b\u0438\u0442\u044c RestKit \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430 Alamofire. \u041f\u043e\u0447\u0435\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e Alamofire? \u0412\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e: \u044d\u0442\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0430\u044f \u043d\u0430 Swift. \u0423 \u043d\u0435\u0451 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043c\u044c\u044e\u043d\u0438\u0442\u0438 \u0438 \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435, \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e iOS-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0441 \u043d\u0435\u0439 \u0437\u043d\u0430\u043a\u043e\u043c\u044b. \u041e\u0434\u043d\u0438\u043c \u0441\u043b\u043e\u0432\u043e\u043c, profit!<\/p>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 api-\u043a\u043b\u0438\u0435\u043d\u0442 \u2014 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u0430\u044f, \u043d\u043e \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u0432\u0441\u0442\u0430\u043b \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a \u0431\u044b\u0442\u044c \u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0432 \u0431\u0430\u0437\u0443? \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 CoreData \u0431\u044b\u043b\u0430 \u0440\u0430\u0437\u043c\u0430\u0437\u0430\u043d\u0430 \u043f\u043e \u0432\u0441\u0435\u043c\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0443, \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u043b \u0435\u0434\u0438\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u0438 \u044d\u0442\u043e \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u043b\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b: \u0437\u0430\u043f\u0443\u0442\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430, \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u043d\u0431\u043e\u0440\u0434\u0438\u043d\u0433\u0430, \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438 \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/66b\/a88\/c43\/66ba88c4382315b02c4165ccfbba3c4b.jpeg\" width=\"623\" height=\"415\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/66b\/a88\/c43\/66ba88c4382315b02c4165ccfbba3c4b.jpeg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u0440\u0435\u0448\u0438\u043b\u0438, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0443\u0436 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c api-\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u044b, \u0442\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e! \u0422\u0430\u043a, \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u0432\u0441\u0442\u0430\u043b\u0430 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u2014 \u0441\u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0439 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u0412\u043e\u0442 \u043a\u0430\u043a\u0438\u0435 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u0443\u0447\u043b\u0438 \u0432 \u0446\u0435\u043b\u044f\u0445:<\/p>\n<ul>\n<li>\n<p><strong>\u0446\u0435\u043b\u044c \u21161:<\/strong> \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c CoreData \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435;<\/p>\n<\/li>\n<li>\n<p><strong>\u0446\u0435\u043b\u044c \u21162:<\/strong> \u0441\u043a\u0440\u044b\u0442\u044c \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u2014 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438, \u0430 \u043d\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p><strong>\u0446\u0435\u043b\u044c \u21163:<\/strong> \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u044b \u0437\u0430\u043f\u0430\u0441\u043b\u0438\u0441\u044c \u0442\u0435\u0440\u043f\u0435\u043d\u0438\u0435\u043c, \u043f\u0438\u0446\u0446\u0435\u0439 \u0438 \u0441\u043c\u0443\u0437\u0438, \u0438 \u043f\u043e\u0448\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c\u2026<\/p>\n<h2>\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430<\/h2>\n<p>\u041e\u0442\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u044f\u0441\u044c \u043e\u0442 \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u0435\u0439, \u043c\u044b \u043d\u0430\u043a\u0438\u0434\u0430\u043b\u0438 \u0436\u0435\u043b\u0430\u0435\u043c\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0438 \u043d\u0430\u0437\u0432\u0430\u043b\u0438 \u044d\u0442\u043e Repository. \u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<pre><code class=\"swift\">class Repository&lt;DomainModel, DBEntity> {     func persist&lt;Model, PersistMapper>(_ model: Model,                                        mapper: PersistMapper,                                        completion: ((Result&lt;DomainModel, Error>) -> Void)?) where PersistMapper: PersistableMapper,                                                                                             PersistMapper.FromModel == Model,                                                                                               PersistMapper.ToModel == DBEntity { ... }     func persist&lt;Model, PersistMapper>(_ models: [Model],                                        mapper: PersistMapper,                                        completion: ((Result&lt;[DomainModel], Error>) -> Void)?) where PersistMapper: PersistableMapper,                                                                                               PersistMapper.FromModel == Model,                                                                                                 PersistMapper.ToModel == DBEntity { ... }      func fetch&lt;Query>(searchQuery: Query, completion: @escaping (Result&lt;[DomainModel], Error>) -> Void) where Query: SortedDatabaseQuery, Query.DBEntity == DBEntity { ... }      func fetchAll(completion: @escaping (Result&lt;[DomainModel], Error>) -> Void) { ... }      func removeAll&lt;Query>(matching query: Query, completion: ((Result&lt;Void, Error>) -> Void)?) where Query: DatabaseQuery, Query.DBEntity == DBEntity { ... }      func removeAll(completion: ((Result&lt;Void, Error>) -> Void)?) { ... }      func count&lt;Query>(matching query: Query) -> Int where Query: DatabaseQuery, Query.DBEntity == DBEntity { ... }      func count() -> Int { ... }      func first&lt;Query>(matching query: Query) -> DomainModel? where Query: SortedDatabaseQuery, Query.DBEntity == DBEntity { ... }      func first() -> DomainModel? { ... } }<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a Repository \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432\u044b\u0439, \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0442\u0430\u043d\u0446\u0435\u0432 \u0441 \u0431\u0443\u0431\u043d\u0430\u043c\u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u043c\u044b\u0432\u0430\u043d\u0438\u0438 \u0442\u0438\u043f\u043e\u0432. DomainModel \u2014 \u0442\u0438\u043f \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439, \u0430 DBEntity \u2014 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0430\u0441\u0441\u043e\u0446\u0438\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e. Repository \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 CRUD-\u043c\u0435\u0442\u043e\u0434\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\/\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435, \u0432\u044b\u0431\u043e\u0440\u043a\u0430, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043c\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u043e\u043c \u0438\u043d\u0432\u0435\u0440\u0441\u0438\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e. \u0410 \u043f\u0440\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0435 \u0437\u0430\u0432\u044f\u0437\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u0434\u0435\u0442\u0430\u043b\u044f\u0445 CoreData, \u0430 \u0441\u0442\u0430\u0440\u0430\u043b\u0438\u0441\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441.<\/p>\n<p>\u0412 \u043e\u0431\u0449\u0435\u043c \u0432\u0438\u0434\u0435 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u043c \u043c\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u043b\u0438 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ca3\/4bc\/bd7\/ca34bcbd7625957a258df3774f016029.png\" alt=\"\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\" title=\"\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430\" width=\"1020\" height=\"572\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ca3\/4bc\/bd7\/ca34bcbd7625957a258df3774f016029.png\"\/><figcaption>\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430<\/figcaption><\/figure>\n<h2>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0418\u0442\u0430\u043a, \u043a\u0430\u043a \u0432\u044b \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0432\u0430\u0436\u043d\u043e\u0439 \u0446\u0435\u043b\u044c\u044e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0434\u043b\u044f \u043d\u0430\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438. \u0417\u043d\u0430\u0447\u0438\u0442, \u043f\u043e\u043c\u0438\u043c\u043e \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043c\u0430\u043f\u043f\u0435\u0440, \u0443\u043c\u0435\u044e\u0449\u0438\u0439 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u043d\u0443\u0436\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041c\u044b \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u043b\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u0438 \u0440\u0430\u0437\u0434\u0443\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041c\u0430\u043f\u043f\u0435\u0440 \u043e\u043f\u0438\u0441\u0430\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u043c PersistableMapper.<\/p>\n<pre><code class=\"swift\">protocol PersistableMapper {     associatedtype FromModel     associatedtype ToModel      func createEntity(from model: FromModel) -> ToModel     func updateEntity(_ entity: inout ToModel, from model: FromModel)     func keyPathsForPrimaryKeys() -> [PrimaryKeysPaths&lt;FromModel, ToModel>] }<\/code><\/pre>\n<p>PersistableMapper \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0432\u0430 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u0430:<\/p>\n<ol>\n<li>\n<p>FromModel \u2014 \u043c\u043e\u0434\u0435\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>ToModel \u2014 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.\u00a0<\/p>\n<\/li>\n<\/ol>\n<p>\u0414\u043b\u044f \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<ol>\n<li>\n<p>updateEntity(:from:) \u2014 \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0431\u0430\u0437\u0435 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043d\u0443\u0436\u043d\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>createEntity(from:) \u2014 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0435\u0451 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0442\u043e\u0433\u043e, \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 keyPathsForPrimaryKeys(), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 PrimaryKeysPaths. \u042d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0430\u044f PartialKeyPath \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041f\u043e \u0441\u0443\u0442\u0438, \u044d\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0438, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0443 \u043d\u0430\u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 \u0431\u0430\u0437\u0435 \u0438\u043b\u0438 \u043d\u0435\u0442. \u0415\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <strong>\u0432\u0441\u0435\u0445<\/strong> \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<h2>\u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/h2>\n<p>\u0414\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041c\u044b \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c NSPredicate \u0438 NSSortDescriptor, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0435\u0441\u0442\u044c \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043e\u0448\u0438\u0431\u0438\u0442\u044c\u0441\u044f \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c NSPredicate, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0448\u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. \u0421\u043e\u0437\u0434\u0430\u043b\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b: DatabaseQuery \u2014 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0438, SortedDatabaseQuery \u2014 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/p>\n<pre><code class=\"swift\">protocol SortedDatabaseQuery: DatabaseQuery {     var sortDescriptors: [NSSortDescriptor] { get } }  protocol DatabaseQuery {     associatedtype DBEntity      var predicate: NSPredicate? { get } }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432\u044b\u0435 \u2014 \u0447\u0442\u043e\u0431\u044b \u0437\u043d\u0430\u0442\u044c, \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d Query-\u043e\u0431\u044a\u0435\u043a\u0442. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c Query, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u0434\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e, \u0432 Repository, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0441 \u0434\u0440\u0443\u0433\u043e\u0439. \u042d\u0442\u043e \u0441\u0432\u043e\u0435\u043e\u0431\u0440\u0430\u0437\u043d\u0430\u044f \u0437\u0430\u0449\u0438\u0442\u0430, \u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u044d\u0442\u0430\u043f\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f CoreData.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f<\/h2>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e CoreData \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f NSManagedObject, \u0430 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043e\u0442 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438, \u0442\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0441\u044f \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u043c\u0430\u043f\u043f\u0435\u0440, \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0432 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438. \u0422\u0430\u043a \u0440\u043e\u0434\u0438\u043b\u0441\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b DomainModelCoreDataMapper.<\/p>\n<pre><code class=\"swift\">protocol DomainModelCoreDataMapper {     associatedtype DomainModel     associatedtype DBEntity: NSManagedObject      func model(from entity: DBEntity) -> DomainModel? }<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u0447\u0442\u043e \u043f\u043e\u043c\u0438\u043c\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438, \u0443 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u0435\u0441\u0442\u044c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435, \u0447\u0442\u043e DBEntity \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c NSManagedObject.<\/p>\n<p>CoreDataRepository \u0442\u043e\u0436\u0435 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432\u044b\u0439, \u043d\u043e \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 Repository \u0435\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0435 \u0442\u0438\u043f \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043c\u0430\u043f\u043f\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b DomainModelCoreDataMapper, \u0437\u0430 \u0441\u0447\u0451\u0442 \u0447\u0435\u0433\u043e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 NSManagedObject \u0438\u043b\u0438 \u0435\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c.<\/p>\n<pre><code class=\"swift\">final class CoreDataRepository&lt;DomainModelMapper: DomainModelCoreDataMapper>: Repository&lt;DomainModelMapper.DomainModel, DomainModelMapper.DBEntity> {     typealias DomainModel = DomainModelMapper.DomainModel     typealias EntityMO = DomainModelMapper.DBEntity  init(domainModelMapper: DomainModelMapper, contextProvider: CoreDataContextProvider) {         self.contextProvider = contextProvider         self.domainModelMapper = domainModelMapper     } ... }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b CoreDataContextProvider. \u0415\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 YoulaCoreDataContextProvider. \u042d\u0442\u043e \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d-\u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0434\u043b\u044f CoreDataRepository.<\/p>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0442\u0430\u043a\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 \u0431\u0430\u0437\u0435. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0451. \u0412 \u0440\u0430\u043c\u043a\u0430\u0445 CoreData \u044d\u0442\u0443 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043c\u044b \u0432\u043e\u0437\u043b\u043e\u0436\u0438\u043b\u0438 \u043d\u0430 PersistableMapper, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u0437\u043d\u0430\u0435\u0442 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0438 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u043f\u043f\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0441\u0432\u044f\u0437\u0438 (relations) \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041d\u0430\u0448\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"swift\">extension PersistableMapper where ToModel: NSManagedObject {     typealias Model = FromModel     typealias DBEntity = ToModel      \/\/ \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e PartialKeyPath     private func modelPrimaryKey(_ model: Model, primaryKeyPath: PartialKeyPath&lt;Model>) -> Any {         return model[keyPath: primaryKeyPath]     }      private func entityPrimaryKey(_ entity: DBEntity, primaryKeyPath: PartialKeyPath&lt;DBEntity>) -> Any {         return entity[keyPath: primaryKeyPath]     }      \/\/ \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430     func createEntity(from model: FromModel) -> ToModel {         return DBEntity(entity: DBEntity.entity(), insertInto: nil)     }      \/\/ \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f\/\u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439     \/\/     \/\/ \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c:      \/\/ \u0417\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\/\u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c     \/\/ \u0415\u0441\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434 createEntity(from:), \u0442\u043e \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442     \/\/ \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u043b\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 updateEntity(:from:)     \/\/ \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c     @discardableResult     func entity(from model: Model, in context: NSManagedObjectContext) -> DBEntity { ... }      @discardableResult     func entities(from models: [Model], in context: NSManagedObjectContext) -> [DBEntity] { ... }  }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u043f\u0440\u0438\u0432\u0451\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0431\u043e\u0440\u043a\u0438. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043f\u043e\u0445\u043e\u0436\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"swift\">private func persistModel&lt;Model, PersistMapper>(_ model: Model,                                                     mapper: PersistMapper,                                                     completion: ((Result&lt;DomainModel, Error>) -> Void)?) where DomainModelMapper.DBEntity == PersistMapper.ToModel,                                                                                                                              Model == PersistMapper.FromModel,                                                                                                                              PersistMapper: PersistableMapper {         \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c worker context         let context = contextProvider.workerContext()         context.perform { [weak self] in             guard let self = self else {                 return             }              \/\/ \u041c\u0430\u043f\u043f\u0435\u0440 \u043d\u0430\u0439\u0434\u0435\u0442 \u0432 \u0431\u0430\u0437\u0435 \u043d\u0443\u0436\u043d\u0443\u044e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043d\u0435\u0442, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0435\u0435             \/\/ \u0417\u0430\u043f\u043e\u043b\u043d\u0438\u0442 \u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0438\u0437 model             \/\/ \u0412\u0435\u0440\u043d\u0435\u0442 NSManagedObject             let entity = mapper.entity(from: model, in: context)              \/\/ \u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 \u0434\u043e\u043c\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c             guard let domainModel = self.domainModelMapper.model(from: entity) else {                 \/\/ \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c completion \u043d\u0430 main \u043f\u043e\u0442\u043e\u043a\u0435 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u043c\u0430\u043f\u043f\u0438\u043d\u0433\u0430                 DispatchQueue.main.asyncCompletion(completion: completion, with: .failure(CoreDataError.modelMapping))                 return             }              \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442             self.safelySaveToPersistentStore(context: context, completion: { error in                 if let error = error {                     completion?(.failure(error))                 } else {                     completion?(.success(domainModel))                 }             })         }     }  private func fetchModels(predicate: NSPredicate?,                              sortDescriptors: [NSSortDescriptor]?,                              completion: @escaping (Result&lt;[DomainModel], Error>) -> Void) {          \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c worker context         let context = contextProvider.workerContext()          context.perform { [weak self] in             guard let self = self else {                 return             }              \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c NSFetchRequest             let entityName = EntityMO.entity().name ?? \"\"             let fetchRequest = NSFetchRequest&lt;EntityMO>(entityName: entityName)             fetchRequest.sortDescriptors = sortDescriptors             fetchRequest.predicate = predicate              do {                 \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438                 let entities = try context.fetch(fetchRequest)                 var domainModels: [DomainModel] = []                  \/\/ \u041a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0432 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438                 for entity in entities {                     guard let domainModel = self.domainModelMapper.model(from: entity) else { \/\/ \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c completion \u043d\u0430 main \u043f\u043e\u0442\u043e\u043a\u0435 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 \u043c\u0430\u043f\u043f\u0438\u043d\u0433\u0430 DispatchQueue.main.asyncCompletion(completion: completion, with: .failure(CoreDataError.modelMapping))                         return                     }                     domainModels.append(domainModel)                 }                  \/\/ \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c completion \u043d\u0430 main \u043f\u043e\u0442\u043e\u043a\u0435 \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c                 DispatchQueue.main.asyncCompletion(completion: completion, with: .success(domainModels))             } catch {                 DispatchQueue.main.asyncCompletion(completion: completion, with: .failure(error))             }         }     }<\/code><\/pre>\n<h2>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c?<\/h2>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0431\u0430\u0437\u0443. \u0412\u043c\u0435\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f CoreData \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u043a\u0430\u043a\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c.<\/p>\n<pre><code class=\"swift\">\/\/ \u041c\u043e\u0434\u0435\u043b\u044c \u043e\u0442\u0432\u0435\u0442\u0430 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 struct UserResponse: Decodable {     let identifier: String     let name: String     let type: String     let image: ImageResponse     let isOnline: Bool }  \/\/ \u0414\u043e\u043c\u0435\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c struct User {     let identifier: String     let name: String     let type: UserType     let image: Image     let isOnline: Bool }  \/\/ \u0421\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 CoreData final class UserMO: NSManagedObject {     @NSManaged var identifier: String?     @NSManaged var name: String?     @NSManaged var type: String?     @NSManaged var image: ImageMO?     @NSManaged var isOnline: NSNumber? }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c Persistable-\u043c\u0430\u043f\u043f\u0435\u0440\u044b. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043c\u0430\u043f\u043f\u0435\u0440 \u0438\u0437 UserResponse \u0432 UserMO. \u041c\u0430\u043f\u043f\u0435\u0440 \u0438\u0437 User \u0432 UserMO \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e:<\/p>\n<pre><code class=\"swift\">struct UserResponsePersistableMapper: PersistableMapper {     typealias FromModel = UserResponse     typealias ToModel = UserMO      \/\/ \u0412\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u043f\u043f\u0435\u0440, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044e ImageResponse \u0432 ImageMO     private let imageResponseMapper = ImageResponsePersistableMapper()      func updateEntity(_ entity: inout UserMO, from model: UserResponse) {         \/\/ \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u043b\u044f         entity.name = model.name         entity.type = model.type         entity.isOnline = model.isOnline as NSNumber          guard let context = entity.managedObjectContext else {             return         }          \/\/ \u0414\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f         entity.image = imageResponseMapper.entity(from: model.image, in: context)     }      func keyPathsForPrimaryKeys() -> [PrimaryKeysPaths&lt;UserResponse, UserMO>] {         \/\/ \u0423\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0438         return [PrimaryKeysPaths(modelKeyPath: \\UserResponse.identifier,                                  entityKeyPath: \\UserMO.identifier)]     } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c CoreDataMapper \u0434\u043b\u044f \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 UserMO \u0432 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0439 User.<\/p>\n<pre><code class=\"swift\">final class UserCoreDataMapper: DomainModelCoreDataMapper {     typealias DomainModel = User     typealias DBEntity = UserMO      \/\/ \u0412\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u043f\u043f\u0435\u0440, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044e ImageMO \u0432 Image     private let imageMapper = ImageCoreDataMapper()      func model(from entity: UserMO) -> User? {         guard             let identifier = entity.identifier,             let name = entity.name,             let type = UserType(rawValue: entity.type ?? \"\"),             let imageEntity = entity.image,             let image = imageMapper.model(from: imageEntity),             let isOnline = entity.isOnline?.boolValue         else {             return nil         }          return User(identifier: identifier,                     name: name,                     type: type,                     image: image,                     isOnline: isOnline)     }  }<\/code><\/pre>\n<p>\u041c\u043e\u0434\u0435\u043b\u0438 \u0433\u043e\u0442\u043e\u0432\u044b, \u043c\u0430\u043f\u043f\u0435\u0440\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b. \u041f\u0440\u0438\u0441\u0442\u0435\u0433\u043d\u0438\u0442\u0435\u0441\u044c, \u043c\u044b \u0432\u0437\u043b\u0435\u0442\u0430\u0435\u043c! ?<\/p>\n<pre><code class=\"swift\">final class ProfileInteractor: ProfileInteractorInput {     \/\/ \u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u043e\u0439     private let userRepository: Repository&lt;User, UserMO>     \/\/ \u0421\u0435\u0440\u0432\u0438\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0430\u043f\u0438     private let userService: UserServiceDescription     \/\/ id \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c     private let userId: String      init(userId: String) {         self.userId = userId         \/\/ \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c CoreData         userRepository = CoreDataRepository(domainModelMapper: UserCoreDataMapper(), contextProvider: YoulaCoreDataContextProvider.default)         userService = UserService()     }      func obtainUser(id: String, completion: @escaping (Result&lt;User, Error>) -> Void) {         \/\/ \u0417\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430         userService.obtainUser(id: id, completion: { [weak self] result in             switch result {             case .success(let userResponse):                 \/\/ \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d (UserResponse)                 \/\/ \u041c\u043e\u0436\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c UserResponse, \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043c\u0430\u043f\u043f\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043c\u0435\u0435\u0442 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c                 \/\/ \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0432\u0435\u0440\u043d\u0435\u0442 \u0434\u043e\u043c\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c (User), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0432 completion                 self?.userRepository.persist(userResponse, mapper: UserResponsePersistableMapper(), completion: completion)             case .failure(let error):                 completion(.failure(error))             }         })     }      func updateUserName(_ newName: String) {         \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438         \/\/ \u041e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 NSPredicate \u0438 \u043c\u0430\u0441\u0441\u0438\u0432 NSSortDescriptor         let query = UserIdQuery(id: userId)          \/\/ \u0417\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f         guard var user = userRepository.first(matching: query) else {             return         }          \/\/ \u041c\u0435\u043d\u044f\u0435\u043c \u0438\u043c\u044f         user.name = newName         \/\/ \u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u043f\u043f\u0435\u0440         userRepository.persist(user, mapper: UserPersistableMapper(), completion: nil)     }  private struct UserIdQuery: SortedDatabaseQuery {         typealias DBEntity = UserMO          let id: String          var predicate: NSPredicate? {             return NSPredicate(format: \"%K == %@\", #keyPath(DBEntity.identifier), id)         }          var sortDescriptors: [NSSortDescriptor] {             return []         }     }  }<\/code><\/pre>\n<h2>\u041a\u0440\u0443\u0442\u043e! \u041d\u043e \u044d\u0442\u043e \u043d\u0435 \u043a\u043e\u043d\u0435\u0446<\/h2>\n<p>\u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u0432\u0441\u0451 \u0433\u043e\u0442\u043e\u0432\u043e! \u041d\u043e \u043c\u044b \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0438\u0441\u044c \u0435\u0449\u0451 \u0441 \u043e\u0434\u043d\u0438\u043c \u0447\u0435\u043b\u043b\u0435\u043d\u0434\u0436\u0435\u043c. \u041d\u0430 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430\u0448 \u0431\u044d\u043a\u0435\u043d\u0434 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043f\u043e\u043b\u0435\u0439. \u041d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0438: \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0435 \u043f\u0440\u0438\u0448\u043b\u043e \u0432 \u043e\u0442\u0432\u0435\u0442\u0435, \u0442\u043e \u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438, \u0435\u0441\u043b\u0438 \u0436\u0435 \u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e, \u0442\u043e \u043f\u043e\u043b\u0435 \u0432 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043d\u0435 \u0442\u0440\u043e\u0433\u0430\u0442\u044c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0442\u0430\u043c \u043c\u043e\u0433\u043b\u043e \u0431\u044b\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043e\u0442\u0432\u0435\u0442\u0430\u0445.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/670\/6bf\/0a0\/6706bf0a01b8ee234e430ebe0d0cbdab.png\" width=\"1020\" height=\"572\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/670\/6bf\/0a0\/6706bf0a01b8ee234e430ebe0d0cbdab.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0438\u043d\u0433\u0430 \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u043b\u0438\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Codable, \u043d\u043e \u0441 \u043d\u0438\u043c \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u043b\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0438\u0448\u043b\u043e \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c null, \u0438 \u043f\u043e\u043b\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e \u0432\u043e\u043e\u0431\u0449\u0435, \u0432 \u043c\u043e\u0434\u0435\u043b\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043a\u0430\u043a nil. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0432 \u0431\u0430\u0437\u0443 \u043c\u044b \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0443\u0437\u043d\u0430\u0442\u044c, \u043d\u0430\u0434\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u043b\u0435 \u0438\u043b\u0438 \u043d\u0435\u0442.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/368\/7fe\/ffa\/3687feffa10c0f5a60790a245991d262.jpeg\" width=\"336\" height=\"497\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/368\/7fe\/ffa\/3687feffa10c0f5a60790a245991d262.jpeg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043a\u0443\u043c\u0435\u043a\u0430\u0432, \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 Property Wrapper, \u0430 \u0447\u0442\u043e\u0431\u044b \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u043d\u0433\u0435, \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u043b\u0438 KeyedDecodingContainer. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0432\u0440\u0430\u043f\u0435\u0440\u0443 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043f\u043e\u043b\u044e \u043a\u0430\u043a \u043a \u043e\u0431\u044b\u0447\u043d\u043e\u043c\u0443 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044e, \u0430 \u0435\u0441\u043b\u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f, \u0442\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a projectedValue \u0447\u0435\u0440\u0435\u0437 \u043d\u043e\u0442\u0430\u0446\u0438\u044e \u2014 $.<\/p>\n<pre><code class=\"swift\">@propertyWrapper public struct DetailedResponse&lt;Value> {      public enum Response {          \/\/ \u043f\u043e\u043b\u0435 \u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e         case notCome           \/\/ \u043f\u043e\u043b\u0435 \u043f\u0440\u0438\u0448\u043b\u043e, \u043d\u043e null         case null           \/\/ \u043f\u043e\u043b\u0435 \u043f\u0440\u0438\u0448\u043b\u043e \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c          case value(Value)      }      public let wrappedValue: Value?     public let projectedValue: Response      public init(response: Response) {         self.projectedValue = response          if case let .value(value) = response {             self.wrappedValue = value         } else {             self.wrappedValue = nil         }     } }  public extension KeyedDecodingContainer {      func decodeDetailResponse&lt;T>(_ type: T.Type, forKey key: KeyedDecodingContainer&lt;Key>.Key) throws -> DetailedResponse&lt;T> where T: Decodable {         guard contains(key) else {             return DetailedResponse(response: .notCome)         }          if let result = try decodeIfPresent(type, forKey: key) {             return DetailedResponse(response: .value(result))         } else {             return DetailedResponse(response: .null)         }     }  }<\/code><\/pre>\n<p>\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u043f\u043e\u043c\u0435\u0442\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 \u043a\u0430\u043a <a class=\"mention\" href=\"\/users\/DetailedResponse\">@DetailedResponse<\/a> \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440 Decodable. \u0410 \u0432 \u043c\u0430\u043f\u043f\u0435\u0440\u0435 \u043f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u043d\u0430 projectedValue, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u043f\u0440\u0438\u0448\u043b\u043e \u043f\u043e\u043b\u0435 \u0438\u043b\u0438 \u043d\u0435\u0442.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u043b\u0435 isOnline \u0432 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u044e\u0437\u0435\u0440\u0430 \u0438\u043d\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u0448 \u0434\u0435\u043a\u043e\u0434\u0438\u043d\u0433 \u043d\u0430\u0434\u043e \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"swift\">struct UserResponse: Decodable {     let identifier: String     let name: String     let type: String     let image: ImageResponse      \/\/ \u041f\u043e\u043c\u0435\u0447\u0430\u0435\u043c \u0432\u0440\u0430\u043f\u043f\u0435\u0440\u043e\u043c      @DetailedResponse var isOnline: Bool?      enum CodingKeys: String, CodingKey { ... }      init(from decoder: Decoder) throws {         let container = try decoder.container(keyedBy: CodingKeys.self)         identifier = try container.decode(String.self, forKey: .identifier)         name = try container.decode(String.self, forKey: .name)         type = try container.decode(String.self, forKey: .type)         image = try container.decode(ImageResponse.self, forKey: .image)          \/\/ \u0414\u0435\u043a\u043e\u0434\u0438\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430         _isOnline = try container.decodeDetailResponse(Bool.self, forKey: .isOnline) }  struct User {     let identifier: String     let name: String     let type: UserType     let image: Image      \/\/ \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u0435 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0435     let isOnline: Bool? }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 projectedValue.<\/p>\n<pre><code class=\"swift\">struct UserResponsePersistableMapper: PersistableMapper {     typealias FromModel = UserResponse     typealias ToModel = UserMO      private let imageResponseMapper = ImageResponsePersistableMapper()      func updateEntity(_ entity: inout UserMO, from model: UserResponse) {         entity.name = model.name         entity.type = model.type          \/\/ \u0421\u0432\u0438\u0447\u0438\u043c\u0441\u044f \u043f\u043e DetailedResponse.Response         switch model.$isOnline {         case .notCome:             break         case .null:             entity.isOnline = nil         case .value(let newIsOnline):             entity.isOnline = newIsOnline as NSNumber         }          guard let context = entity.managedObjectContext else {             return         }          entity.image = imageResponseMapper.entity(from: model.image, in: context)     }      func keyPathsForPrimaryKeys() -> [PrimaryKeysPaths&lt;UserResponse, UserMO>] {         return [PrimaryKeysPaths(modelKeyPath: \\UserResponse.identifier,                                  entityKeyPath: \\UserMO.identifier)]     } }<\/code><\/pre>\n<h2>\u0412\u043c\u0435\u0441\u0442\u043e \u0438\u0442\u043e\u0433\u0430<\/h2>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u044b \u0437\u0430\u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u044b \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u0432 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432: api-\u0441\u0435\u0440\u0432\u0438\u0441\u044b \u043d\u0430 Alamofire + \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439. \u0418 \u0443\u0436\u0435 \u0432\u0438\u0434\u0438\u043c \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f, \u0440\u0430\u0434\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044d\u0442\u043e \u0432\u0441\u0451 \u0437\u0430\u0442\u0435\u0432\u0430\u043b\u0438. \u0410 \u0438\u043c\u0435\u043d\u043d\u043e:<\/p>\n<ol>\n<li>\n<p>\u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 RestKit \u0438 \u0441\u043a\u043e\u0440\u043e \u0441\u043c\u043e\u0436\u0435\u043c \u0435\u0433\u043e \u0432\u044b\u043f\u0438\u043b\u0438\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043a\u043e\u0434 \u0441\u0442\u0430\u043b\u043e \u043f\u0440\u043e\u0449\u0435, \u0432\u044b\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0441\u044f \u0435\u0434\u0438\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u043e\u0439;<\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u0442 \u043d\u0443\u0436\u0434\u044b \u0437\u0430\u043c\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u0434\u0435\u0442\u0430\u043b\u044f\u043c\u0438 CoreData \u2014 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412\u0435\u043b\u043a\u043e\u043c \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u044b \u2014 \u043e\u0431\u0441\u0443\u0434\u0438\u0442\u044c \u043d\u0430\u0448\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u044c\u044e \u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438.<\/p>\n<p>PS: \u041f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0432\u0434\u043e\u0445\u043d\u043e\u0432\u043b\u044f\u043b\u0438\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u0435\u0439<a href=\"https:\/\/habr.com\/ru\/post\/542752\/\"> <u>https:\/\/habr.com\/ru\/post\/542752\/<\/u><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/678120\/\"> https:\/\/habr.com\/ru\/articles\/678120\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0418\u0433\u043e\u0440\u044c \u0421\u043e\u0440\u043e\u043a\u0438\u043d. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u0434\u0435\u043b\u044e\u0441\u044c \u0438\u0441\u0442\u043e\u0440\u0438\u0435\u0439 \u043e \u0442\u043e\u043c, \u043a\u0443\u0434\u0430 \u043d\u0430\u0441 \u0437\u0430\u0432\u0451\u043b \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433, \u043a\u0430\u043a \u043c\u044b \u043e\u0442\u0442\u0443\u0434\u0430 \u0432\u044b\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u043f\u043e\u043f\u0443\u0442\u043d\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0432 \u0441\u043b\u043e\u0439 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u0422\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043f\u043e\u043a\u0430\u0436\u0443 \u043f\u043e\u0434\u0445\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0438 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u0445, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u044b \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0438\u0441\u044c. \u041d\u043e \u043e\u0431\u043e \u0432\u0441\u0451\u043c \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.<\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430, \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u0434\u0443\u043c\u0430\u043b\u0438\u0441\u044c \u043e \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0438 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0441\u0432\u043e\u0451 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0435\u0451, \u043d\u043e \u0431\u0435\u0437 \u0435\u0434\u0438\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0432\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u043e, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u0432\u044b \u0441\u0447\u0430\u0441\u0442\u043b\u0438\u0432\u044b\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>\u041f\u0440\u0435\u0434\u044b\u0441\u0442\u043e\u0440\u0438\u044f<\/h2>\n<p>\u0418\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438 \u0442\u0430\u043a \u0441\u043b\u043e\u0436\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u042e\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f RestKit \u2014 \u043c\u043e\u0449\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0430\u044f \u0435\u0449\u0451 \u043d\u0430 \u0441\u0442\u0430\u0440\u043e\u043c \u0434\u043e\u0431\u0440\u043e\u043c Objective-C. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c api-\u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0434\u0435\u043a\u043e\u0434\u0438\u0442\u044c JSON \u0432 NSManagedObject \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438 \u0442\u0443\u0442 \u0436\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438\u0445 \u0432 CoreData. \u041e\u0434\u043d\u0430\u043a\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043d\u0435\u0443\u043c\u043e\u043b\u0438\u043c\u043e \u043f\u043e\u0441\u0442\u0430\u0440\u0435\u043b\u0430, \u0430 \u0435\u0451 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0438 \u0432\u043e\u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0430. \u0414\u0430 \u0438 \u0440\u0435\u0431\u044f\u0442\u0430 \u0432 iOS-\u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u043d\u0435\u043e\u0445\u043e\u0442\u043d\u043e \u0435\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c.<\/p>\n<p>\u0422\u0430\u043a, \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u043d\u0430 \u043f\u044f\u0442\u043d\u0438\u0447\u043d\u043e\u0439 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0432 \u043e\u0444\u0438\u0441\u0435 \u0437\u0430 \u043a\u0440\u0443\u0433\u043b\u044b\u043c \u0441\u0442\u043e\u043b\u043e\u043c \u0438 \u0441\u043e \u0432\u043a\u0443\u0441\u043d\u043e\u0439 \u043f\u0438\u0446\u0446\u0435\u0439, \u043c\u044b \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0448\u0438\u043b\u0438 \u0432\u044b\u043f\u0438\u043b\u0438\u0442\u044c RestKit \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430 Alamofire. \u041f\u043e\u0447\u0435\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e Alamofire? \u0412\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e: \u044d\u0442\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0430\u044f \u043d\u0430 Swift. \u0423 \u043d\u0435\u0451 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043c\u044c\u044e\u043d\u0438\u0442\u0438 \u0438 \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435, \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e iOS-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0441 \u043d\u0435\u0439 \u0437\u043d\u0430\u043a\u043e\u043c\u044b. \u041e\u0434\u043d\u0438\u043c \u0441\u043b\u043e\u0432\u043e\u043c, profit!<\/p>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 api-\u043a\u043b\u0438\u0435\u043d\u0442 \u2014 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u0430\u044f, \u043d\u043e \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u0432\u0441\u0442\u0430\u043b \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a \u0431\u044b\u0442\u044c \u0441 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0432 \u0431\u0430\u0437\u0443? \u0420\u0430\u0431\u043e\u0442\u0430 \u0441 CoreData \u0431\u044b\u043b\u0430 \u0440\u0430\u0437\u043c\u0430\u0437\u0430\u043d\u0430 \u043f\u043e \u0432\u0441\u0435\u043c\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0443, \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u043b \u0435\u0434\u0438\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u0438 \u044d\u0442\u043e \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u043b\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b: \u0437\u0430\u043f\u0443\u0442\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430, \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u043d\u0431\u043e\u0440\u0434\u0438\u043d\u0433\u0430, \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438 \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u0440\u0435\u0448\u0438\u043b\u0438, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0443\u0436 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c api-\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u044b, \u0442\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e! \u0422\u0430\u043a, \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u0432\u0441\u0442\u0430\u043b\u0430 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u2014 \u0441\u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0439 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u0412\u043e\u0442 \u043a\u0430\u043a\u0438\u0435 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u0443\u0447\u043b\u0438 \u0432 \u0446\u0435\u043b\u044f\u0445:<\/p>\n<ul>\n<li>\n<p><strong>\u0446\u0435\u043b\u044c \u21161:<\/strong> \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c CoreData \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435;<\/p>\n<\/li>\n<li>\n<p><strong>\u0446\u0435\u043b\u044c \u21162:<\/strong> \u0441\u043a\u0440\u044b\u0442\u044c \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u2014 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438, \u0430 \u043d\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p><strong>\u0446\u0435\u043b\u044c \u21163:<\/strong> \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u044b \u0437\u0430\u043f\u0430\u0441\u043b\u0438\u0441\u044c \u0442\u0435\u0440\u043f\u0435\u043d\u0438\u0435\u043c, \u043f\u0438\u0446\u0446\u0435\u0439 \u0438 \u0441\u043c\u0443\u0437\u0438, \u0438 \u043f\u043e\u0448\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c\u2026<\/p>\n<h2>\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430<\/h2>\n<p>\u041e\u0442\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u044f\u0441\u044c \u043e\u0442 \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u0435\u0439, \u043c\u044b \u043d\u0430\u043a\u0438\u0434\u0430\u043b\u0438 \u0436\u0435\u043b\u0430\u0435\u043c\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0438 \u043d\u0430\u0437\u0432\u0430\u043b\u0438 \u044d\u0442\u043e Repository. \u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<pre><code class=\"swift\">class Repository&lt;DomainModel, DBEntity> {     func persist&lt;Model, PersistMapper>(_ model: Model,                                        mapper: PersistMapper,                                        completion: ((Result&lt;DomainModel, Error>) -> Void)?) where PersistMapper: PersistableMapper,                                                                                             PersistMapper.FromModel == Model,                                                                                               PersistMapper.ToModel == DBEntity { ... }     func persist&lt;Model, PersistMapper>(_ models: [Model],                                        mapper: PersistMapper,                                        completion: ((Result&lt;[DomainModel], Error>) -> Void)?) where PersistMapper: PersistableMapper,                                                                                               PersistMapper.FromModel == Model,                                                                                                 PersistMapper.ToModel == DBEntity { ... }      func fetch&lt;Query>(searchQuery: Query, completion: @escaping (Result&lt;[DomainModel], Error>) -> Void) where Query: SortedDatabaseQuery, Query.DBEntity == DBEntity { ... }      func fetchAll(completion: @escaping (Result&lt;[DomainModel], Error>) -> Void) { ... }      func removeAll&lt;Query>(matching query: Query, completion: ((Result&lt;Void, Error>) -> Void)?) where Query: DatabaseQuery, Query.DBEntity == DBEntity { ... }      func removeAll(completion: ((Result&lt;Void, Error>) -> Void)?) { ... }      func count&lt;Query>(matching query: Query) -> Int where Query: DatabaseQuery, Query.DBEntity == DBEntity { ... }      func count() -> Int { ... }      func first&lt;Query>(matching query: Query) -> DomainModel? where Query: SortedDatabaseQuery, Query.DBEntity == DBEntity { ... }      func first() -> DomainModel? { ... } }<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a Repository \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432\u044b\u0439, \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0442\u0430\u043d\u0446\u0435\u0432 \u0441 \u0431\u0443\u0431\u043d\u0430\u043c\u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u043c\u044b\u0432\u0430\u043d\u0438\u0438 \u0442\u0438\u043f\u043e\u0432. DomainModel \u2014 \u0442\u0438\u043f \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439, \u0430 DBEntity \u2014 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0430\u0441\u0441\u043e\u0446\u0438\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e. Repository \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 CRUD-\u043c\u0435\u0442\u043e\u0434\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\/\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435, \u0432\u044b\u0431\u043e\u0440\u043a\u0430, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>\u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043c\u044b \u043f\u0440\u0430\u0432\u0438\u043b\u043e\u043c \u0438\u043d\u0432\u0435\u0440\u0441\u0438\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e. \u0410 \u043f\u0440\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0435 \u0437\u0430\u0432\u044f\u0437\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u0434\u0435\u0442\u0430\u043b\u044f\u0445 CoreData, \u0430 \u0441\u0442\u0430\u0440\u0430\u043b\u0438\u0441\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441.<\/p>\n<p>\u0412 \u043e\u0431\u0449\u0435\u043c \u0432\u0438\u0434\u0435 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u043c \u043c\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u043b\u0438 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><figcaption>\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430<\/figcaption><\/figure>\n<h2>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0418\u0442\u0430\u043a, \u043a\u0430\u043a \u0432\u044b \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0432\u0430\u0436\u043d\u043e\u0439 \u0446\u0435\u043b\u044c\u044e \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0434\u043b\u044f \u043d\u0430\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438. \u0417\u043d\u0430\u0447\u0438\u0442, \u043f\u043e\u043c\u0438\u043c\u043e \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043c\u0430\u043f\u043f\u0435\u0440, \u0443\u043c\u0435\u044e\u0449\u0438\u0439 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u043d\u0443\u0436\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041c\u044b \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u043b\u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u0438 \u0440\u0430\u0437\u0434\u0443\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041c\u0430\u043f\u043f\u0435\u0440 \u043e\u043f\u0438\u0441\u0430\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u043c PersistableMapper.<\/p>\n<pre><code class=\"swift\">protocol PersistableMapper {     associatedtype FromModel     associatedtype ToModel      func createEntity(from model: FromModel) -> ToModel     func updateEntity(_ entity: inout ToModel, from model: FromModel)     func keyPathsForPrimaryKeys() -> [PrimaryKeysPaths&lt;FromModel, ToModel>] }<\/code><\/pre>\n<p>PersistableMapper \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0432\u0430 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u0430:<\/p>\n<ol>\n<li>\n<p>FromModel \u2014 \u043c\u043e\u0434\u0435\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>ToModel \u2014 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.\u00a0<\/p>\n<\/li>\n<\/ol>\n<p>\u0414\u043b\u044f \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<ol>\n<li>\n<p>updateEntity(:from:) \u2014 \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0431\u0430\u0437\u0435 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043d\u0443\u0436\u043d\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>createEntity(from:) \u2014 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0435\u0451 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0442\u043e\u0433\u043e, \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 keyPathsForPrimaryKeys(), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 PrimaryKeysPaths. \u042d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0430\u044f PartialKeyPath \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041f\u043e \u0441\u0443\u0442\u0438, \u044d\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0438, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0443 \u043d\u0430\u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 \u0431\u0430\u0437\u0435 \u0438\u043b\u0438 \u043d\u0435\u0442. \u0415\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <strong>\u0432\u0441\u0435\u0445<\/strong> \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<h2>\u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430<\/h2>\n<p>\u0414\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041c\u044b \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c NSPredicate \u0438 NSSortDescriptor, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0435\u0441\u0442\u044c \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043e\u0448\u0438\u0431\u0438\u0442\u044c\u0441\u044f \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c NSPredicate, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0448\u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. \u0421\u043e\u0437\u0434\u0430\u043b\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b: DatabaseQuery \u2014 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0438, SortedDatabaseQuery \u2014 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/p>\n<pre><code class=\"swift\">protocol SortedDatabaseQuery: DatabaseQuery {     var sortDescriptors: [NSSortDescriptor] { get } }  protocol DatabaseQuery {     associatedtype DBEntity      var predicate: NSPredicate? { get } }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432\u044b\u0435 \u2014 \u0447\u0442\u043e\u0431\u044b \u0437\u043d\u0430\u0442\u044c, \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d Query-\u043e\u0431\u044a\u0435\u043a\u0442. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c Query, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u0434\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e, \u0432 Repository, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0441 \u0434\u0440\u0443\u0433\u043e\u0439. \u042d\u0442\u043e \u0441\u0432\u043e\u0435\u043e\u0431\u0440\u0430\u0437\u043d\u0430\u044f \u0437\u0430\u0449\u0438\u0442\u0430, \u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u044d\u0442\u0430\u043f\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f CoreData.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f<\/h2>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e CoreData \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f NSManagedObject, \u0430 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043e\u0442 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438, \u0442\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0441\u044f \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u043c\u0430\u043f\u043f\u0435\u0440, \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0432 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438. \u0422\u0430\u043a \u0440\u043e\u0434\u0438\u043b\u0441\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b DomainModelCoreDataMapper.<\/p>\n<pre><code class=\"swift\">protocol DomainModelCoreDataMapper {     associatedtype DomainModel     associatedtype DBEntity: NSManagedObject      func model(from entity: DBEntity) -> DomainModel? }<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0442\u044c\u0442\u0435, \u0447\u0442\u043e \u043f\u043e\u043c\u0438\u043c\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438, \u0443 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u0435\u0441\u0442\u044c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435, \u0447\u0442\u043e DBEntity \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c NSManagedObject.<\/p>\n<p>CoreDataRepository \u0442\u043e\u0436\u0435 \u0434\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u0432\u044b\u0439, \u043d\u043e \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 Repository \u0435\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0435 \u0442\u0438\u043f \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043c\u0430\u043f\u043f\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b DomainModelCoreDataMapper, \u0437\u0430 \u0441\u0447\u0451\u0442 \u0447\u0435\u0433\u043e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 NSManagedObject \u0438\u043b\u0438 \u0435\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u043e\u043c.<\/p>\n<pre><code class=\"swift\">final class CoreDataRepository&lt;DomainModelMapper: DomainModelCoreDataMapper>: Repository&lt;DomainModelMapper.DomainModel, DomainModelMapper.DBEntity> {     typealias DomainModel = DomainModelMapper.DomainModel     typealias EntityMO = DomainModelMapper.DBEntity  init(domainModelMapper: DomainModelMapper, contextProvider: CoreDataContextProvider) {         self.contextProvider = contextProvider         self.domainModelMapper = domainModelMapper     } ... }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b CoreDataContextProvider. \u0415\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 YoulaCoreDataContextProvider. \u042d\u0442\u043e \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d-\u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0434\u043b\u044f CoreDataRepository.<\/p>\n<p>\u041f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0442\u0430\u043a\u0430\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0432 \u0431\u0430\u0437\u0435. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0451. \u0412 \u0440\u0430\u043c\u043a\u0430\u0445 CoreData \u044d\u0442\u0443 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043c\u044b \u0432\u043e\u0437\u043b\u043e\u0436\u0438\u043b\u0438 \u043d\u0430 PersistableMapper, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d \u0437\u043d\u0430\u0435\u0442 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0438 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u043f\u043f\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0441\u0432\u044f\u0437\u0438 (relations) \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u041d\u0430\u0448\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"swift\">extension PersistableMapper where ToModel: NSManagedObject {     typealias Model = FromModel     typealias DBEntity = ToModel      \/\/ \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e PartialKeyPath     private func modelPrimaryKey(_ model: Model, primaryKeyPath: PartialKeyPath&lt;Model>) -> Any {         return model[keyPath: primaryKeyPath]     }      private func entityPrimaryKey(_ entity: DBEntity, primaryKeyPath: PartialKeyPath&lt;DBEntity>) -> Any {         return entity[keyPath: primaryKeyPath]     }      \/\/ \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430     func createEntity(from model: FromModel) -> ToModel {         return DBEntity(entity: DBEntity.entity(), insertInto: nil)     }      \/\/ \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-386703","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/386703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=386703"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/386703\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}