objective c - Waiting for code to execute (already used dispatch groups) -
got lengthy section of code shown below , i've hit brick wall. code runs , want do. however, needs finish running code within section before printing "finished" @ end. adding semaphores or dispatch group forces breakpoint. might obvious, give me bit of advice on please?
note: cannot use dispatch @ bottom call method. remember within loop.
for (id in arr) { searchbyname = nil; if ([i containsstring:@"word1"] || [i containsstring:@"word2"]) { nsrange searchfromrange = [i rangeofstring:@"ong>"]; nsrange searchtorange = [i rangeofstring:@"</str"]; nsstring *substring = [i substringwithrange:nsmakerange(searchfromrange.location+searchfromrange.length, searchtorange.location-searchfromrange.location-searchfromrange.length)]; [allergens addobject:substring]; if ([substring isequaltostring:@"examee"] && veg_lac_ovosafe == true) { veg_ovosafe = false; vegsafe = false; } else if ([substring isequaltostring:@"example"] && veg_lac_ovosafe == true) { //use of heuristics veg_lacsafe = false; vegsafe = false; } else if ([substring isequaltostring:@"exam"]) { pescetariansafe = true; vegsafe = false; veg_ovosafe = false; veg_lacsafe = false; veg_lac_ovosafe = false; pollotariansafe = false; } nscharacterset *characterstoremove = [[nscharacterset alphanumericcharacterset] invertedset]; nscharacterset *numberstoremove = [nscharacterset charactersetwithcharactersinstring:@"0123456789"]; substring = [[substring componentsseparatedbycharactersinset:characterstoremove] componentsjoinedbystring:@""]; searchbyname = [[[substring componentsseparatedbycharactersinset:numberstoremove] componentsjoinedbystring:@""] lowercasestring]; } else { nscharacterset *characterstoremove = [[nscharacterset alphanumericcharacterset] invertedset]; nscharacterset *numberstoremove = [nscharacterset charactersetwithcharactersinstring:@"0123456789"]; nsstring *searchitem = [[i componentsseparatedbycharactersinset:characterstoremove] componentsjoinedbystring:@""]; searchbyname = [[[searchitem componentsseparatedbycharactersinset:numberstoremove] componentsjoinedbystring:@""] lowercasestring]; } if (![searchbyname isequaltostring:@" "]) { dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high, 0); dispatch_group_enter(_groupsearch); dispatch_async(queue, ^{ [[self databasequery:searchbyname] observeeventtype:firdataeventtypechildadded withblock:^(firdatasnapshot * _nonnull snapshot) { if (snapshot.value != null) { nslog(@"%@", snapshot.value); (int i=0; < [[nsstring stringwithformat:@"%@", snapshot.value] length]; i++) { nsstring *x = [nsstring stringwithformat:@"%c", [[nsstring stringwithformat:@"%@", snapshot.value] characteratindex:i]]; nslog(@"%@", x); if ([x isequaltostring:@"1"]) { vegsafe = false; } else if ([x isequaltostring:@"2"]) { vegsafe = false; veg_lacsafe = false; } else if ([x isequaltostring:@"3"]) { vegsafe = false; veg_ovosafe = false; } else if ([x isequaltostring:@"4"]) { //could use switch case. vegsafe = false; veg_lac_ovosafe = false; veg_lacsafe = false; veg_ovosafe = false; } else if ([x isequaltostring:@"5"]) { pescetariansafe = false; } else if ([x isequaltostring:@"6"]) { pollotariansafe = false; } } } dispatch_group_leave(_groupsearch); } withcancelblock:^(nserror * _nonnull error) { nslog(@"%@", error.localizeddescription); dispatch_group_leave(_groupsearch); }]; }); dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ dispatch_group_wait(_groupsearch, dispatch_time(dispatch_time_now, (int64_t)(2.0 * nsec_per_sec))); dispatch_sync(queue, ^{ //hi }); }); } } nslog(@"finished");
your use of dispatch_group_wait
in wrong place. needs after for
loop, not inside it. , can create queue
before loop , reuse needed.
dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high, 0); (id in arr) { searchbyname = nil; // lots of other code here if (![searchbyname isequaltostring:@" "]) { dispatch_group_enter(_groupsearch); dispatch_async(queue, ^{ [[self databasequery:searchbyname] observeeventtype:firdataeventtypechildadded withblock:^(firdatasnapshot * _nonnull snapshot) { if (snapshot.value != null) { nslog(@"%@", snapshot.value); (int i=0; < [[nsstring stringwithformat:@"%@", snapshot.value] length]; i++) { nsstring *x = [nsstring stringwithformat:@"%c", [[nsstring stringwithformat:@"%@", snapshot.value] characteratindex:i]]; nslog(@"%@", x); if ([x isequaltostring:@"1"]) { vegsafe = false; } else if ([x isequaltostring:@"2"]) { vegsafe = false; veg_lacsafe = false; } else if ([x isequaltostring:@"3"]) { vegsafe = false; veg_ovosafe = false; } else if ([x isequaltostring:@"4"]) { //could use switch case. vegsafe = false; veg_lac_ovosafe = false; veg_lacsafe = false; veg_ovosafe = false; } else if ([x isequaltostring:@"5"]) { pescetariansafe = false; } else if ([x isequaltostring:@"6"]) { pollotariansafe = false; } } } dispatch_group_leave(_groupsearch); } withcancelblock:^(nserror * _nonnull error) { nslog(@"%@", error.localizeddescription); dispatch_group_leave(_groupsearch); }]; }); } } dispatch_group_wait(_groupsearch, dispatch_time(dispatch_time_now, (int64_t)(2.0 * nsec_per_sec))); nslog(@"finished");
Comments
Post a Comment