objective c - Asynchronous Block Return Issue -


using function here return value asynchronous block (just database query). problem application freezes , terminates due memory issue. seeking advice whether better run on main thread or should avoid that? note being executed on thread.

- (nsstring *)databasequery:(nsstring*)ingredient {     __block nsstring *valuetype = nil;     __block bool done = no;     [[[_ref child:@"ingredients"] queryequaltovalue:valuetype childkey:ingredient] observesingleeventoftype:firdataeventtypevalue withblock:^(firdatasnapshot * _nonnull snapshot) {         (firdatasnapshot *child in snapshot.children) {             valuetype = child.value;         }         done = yes;     } withcancelblock:^(nserror * _nonnull error) {         nslog(@"%@", error.localizeddescription);         done = yes;     }];      while (!done) {         [[nsrunloop currentrunloop] rununtildate:[nsdate datewithtimeintervalsincenow:0.1]];     }     return valuetype; } 

update 1: attempted using code below , produces same outcome.

- (nsstring *)databasequery:(nsstring*)ingredient {     __block nsstring *valuetype = nil;     dispatch_semaphore_t sem = dispatch_semaphore_create(0);     firdatabasequery *query = [[_ref child:@"ingredients"] queryequaltovalue:valuetype childkey:ingredient] ;     [query observeeventtype:firdataeventtypechildadded                   withblock:^(firdatasnapshot * _nonnull snapshot) {                       valuetype = snapshot.value;                       dispatch_semaphore_signal(sem);                   }             withcancelblock:^(nserror * _nonnull error) {                 nslog(@"%@", error.localizeddescription);                 dispatch_semaphore_signal(sem);             }];      dispatch_semaphore_wait(sem, dispatch_time_forever);     return valuetype; } 

update 2:

changed format function not returning block. returns firdatabasequery.

- (firdatabasequery *)databasequery:(nsstring*)ingredient {     __block nsstring *valuetype = nil;     firdatabasequery *query = [[_ref child:@"ingredients"] queryequaltovalue:valuetype childkey:ingredient];     return query; } 

the part below in procedure. except value returned null.

query = [self databasequery:substring];             [query observeeventtype:firdataeventtypechildadded                           withblock:^(firdatasnapshot * _nonnull snapshot) {                               idvalue = snapshot.value;                           }                     withcancelblock:^(nserror * _nonnull error) {                         nslog(@"%@", error.localizeddescription);                     }];             nslog(@"%@", idvalue); 

solved: as want avoid nsrunloop, problem had 1 running , hadn't stopped it.

answer add cfrunloopstop(cfrunloopgetcurrent()); application. should replace using semaphores.

problem is, in loop. after first items searched, get's stuck , hangs again. solve this, i've used dispatch groups suggested.

note: declare dispatch group before loop idvalue.

dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high, 0);                 dispatch_group_enter(_groupsearch);                 dispatch_async(queue, ^{                 [[self databasequery:searchitem] observeeventtype:firdataeventtypechildadded                               withblock:^(firdatasnapshot * _nonnull snapshot) {                                   idvalue = snapshot.value;                                   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, ^{                         if (idvalue != null) {                             nslog(@"%@",idvalue);                         }                     });                 }); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -