{"id":2519,"date":"2016-09-08T19:14:24","date_gmt":"2016-09-08T15:14:24","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=2519"},"modified":"2016-09-08T19:16:50","modified_gmt":"2016-09-08T15:16:50","slug":"how-to-wait-until-several-afnetworking-requests-finish","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=2519","title":{"rendered":"How to wait until several AFNetworking requests finish"},"content":{"rendered":"<p>Use dispatch_group_t, dispatch_group_enter dispatch_group_leave, dispatch_group_notify. <\/p>\n<p>Also, below is code to send a huge array to server by dividing ti to parts. It is actual when a single request is too big for server &#8211; you get &#8220;URL too long error&#8221; or HTTP 414.<\/p>\n<pre>\r\n- (void)divideAndSendContacts:(NSArray *)contactsArray\r\n{\r\n    [self showSpinner];\r\n    \r\n    dispatch_group_t group = dispatch_group_create();\r\n    \r\n    NSInteger maxContactsInOneRequest = 29;\r\n    NSInteger totalCount = [contactsArray count];\r\n    \r\n    \/\/ \u041e\u043a\u0440\u0443\u0433\u043b\u044f\u0435\u043c \u0432 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u043f\u043e \u0444\u043e\u0440\u043c\u0443\u043b\u0435 A\/B = (A+B-1)\/B, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 (700 + 300 - 1)\/300 = 3.33 = 3.\r\n    NSInteger numberOfParts = (totalCount + maxContactsInOneRequest - 1) \/ maxContactsInOneRequest;\r\n\r\n    for (int i = 0; i < numberOfParts; i++) {\r\n        NSRange range = NSMakeRange(i * maxContactsInOneRequest, maxContactsInOneRequest);\r\n        NSInteger lastItemIndex = i * maxContactsInOneRequest + maxContactsInOneRequest; \/\/ \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, i=3, 29*3+29=116. \u0411\u043e\u043b\u044c\u0448\u0435 100, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043c\u0435\u0441\u0442\u043e 29 \u0431\u0435\u0440\u0435\u043c 100-29*3=13\r\n        \r\n        if ( lastItemIndex > totalCount ) {\r\n            NSInteger length = totalCount - i * maxContactsInOneRequest;\r\n            range = NSMakeRange(i * maxContactsInOneRequest, length);\r\n        }\r\n        \r\n        NSArray *contactsArrayPart = [contactsArray subarrayWithRange:range];\r\n        \r\n        NSDictionary *contactsDic = @{@\"phone_book\" : @{@\"contacts\" : contactsArrayPart}};\r\n        \r\n        dispatch_group_enter(group);\r\n        \r\n        [[SSHTTPClient sharedClient] POST:@\"\/api\/contacts\" parameters:contactsDic progress:^(NSProgress * _Nonnull uploadProgress) {\r\n            \r\n        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {\r\n            \r\n            dispatch_group_leave(group);\r\n            \r\n        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {\r\n            \r\n            NSLog(@\"Failed method: %@\", NSStringFromSelector(_cmd));\r\n            dispatch_group_leave(group);\r\n            \r\n        }];\r\n    }\r\n    \r\n    dispatch_group_notify(group, dispatch_get_main_queue(), ^{\r\n        \r\n        [self hideSpinner];\r\n        \r\n        [self loadContactsFromServer];\r\n        \r\n    });\r\n    \r\n    return;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Use dispatch_group_t, dispatch_group_enter dispatch_group_leave, dispatch_group_notify. Also, below is code to send a huge array to server by dividing ti to parts. It is actual when a single request is too big for server &#8211; you get &#8220;URL too long error&#8221; or HTTP 414. &#8211; (void)divideAndSendContacts:(NSArray *)contactsArray { [self showSpinner]; dispatch_group_t group = dispatch_group_create(); NSInteger maxContactsInOneRequest [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[186,146,145,144,185],"class_list":["post-2519","post","type-post","status-publish","format-standard","hentry","category-ios","tag-186","tag-afnetworking","tag-dispatch","tag-dispatch_group","tag-nsarray"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to wait until several AFNetworking requests finish - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=2519\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to wait until several AFNetworking requests finish - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"Use dispatch_group_t, dispatch_group_enter dispatch_group_leave, dispatch_group_notify. Also, below is code to send a huge array to server by dividing ti to parts. It is actual when a single request is too big for server &#8211; you get &#8220;URL too long error&#8221; or HTTP 414. - (void)divideAndSendContacts:(NSArray *)contactsArray { [self showSpinner]; dispatch_group_t group = dispatch_group_create(); NSInteger maxContactsInOneRequest [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=2519\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-08T15:14:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-09-08T15:16:50+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"How to wait until several AFNetworking requests finish\",\"datePublished\":\"2016-09-08T15:14:24+00:00\",\"dateModified\":\"2016-09-08T15:16:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519\"},\"wordCount\":58,\"commentCount\":0,\"keywords\":[\"414\",\"AFNetworking\",\"dispatch\",\"dispatch_group\",\"NSArray\"],\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519\",\"name\":\"How to wait until several AFNetworking requests finish - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2016-09-08T15:14:24+00:00\",\"dateModified\":\"2016-09-08T15:16:50+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2519#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to wait until several AFNetworking requests finish\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to wait until several AFNetworking requests finish - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=2519","og_locale":"en_US","og_type":"article","og_title":"How to wait until several AFNetworking requests finish - Denis Kutlubaev","og_description":"Use dispatch_group_t, dispatch_group_enter dispatch_group_leave, dispatch_group_notify. Also, below is code to send a huge array to server by dividing ti to parts. It is actual when a single request is too big for server &#8211; you get &#8220;URL too long error&#8221; or HTTP 414. - (void)divideAndSendContacts:(NSArray *)contactsArray { [self showSpinner]; dispatch_group_t group = dispatch_group_create(); NSInteger maxContactsInOneRequest [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=2519","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2016-09-08T15:14:24+00:00","article_modified_time":"2016-09-08T15:16:50+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=2519#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=2519"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"How to wait until several AFNetworking requests finish","datePublished":"2016-09-08T15:14:24+00:00","dateModified":"2016-09-08T15:16:50+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2519"},"wordCount":58,"commentCount":0,"keywords":["414","AFNetworking","dispatch","dispatch_group","NSArray"],"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=2519#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=2519","url":"https:\/\/blog.alwawee.ru\/?p=2519","name":"How to wait until several AFNetworking requests finish - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2016-09-08T15:14:24+00:00","dateModified":"2016-09-08T15:16:50+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=2519#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=2519"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=2519#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"How to wait until several AFNetworking requests finish"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2519"}],"version-history":[{"count":3,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2519\/revisions"}],"predecessor-version":[{"id":2523,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2519\/revisions\/2523"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}