title: semaphore_wait_trap blocking the UI
body: <p>My apps UI stucks because of <strong><code>semaphore_wait_trap</code></strong>. I don't know how to track it. I've checked if I am updating any UI from background thread. But did not find any. Here is the stack trace.</p>
  
  <p><a href="https://i.stack.imgur.com/XhBzr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XhBzr.png" alt="enter image description here"></a></p>
  
  <p>Any help will be appreciated.</p>
  
  <p>Here is the thread backtrace result:</p>
  
  <pre><code>    warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
  * thread #1: tid = 0x57d93, 0x3814800c libsystem_kernel.dylib`semaphore_wait_trap + 8, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    * frame #0: 0x3814800c libsystem_kernel.dylib`semaphore_wait_trap + 8
      frame #1: 0x381f5288 libsystem_platform.dylib`_os_semaphore_wait + 12
      frame #2: 0x008f0178 libdispatch.dylib`_dispatch_barrier_sync_f_slow + 412
      frame #3: 0x0045318a IssMan - Construction punchlist, on-site inspection and snagging`NR__dispatch_sync(queue=0x1667fc50, block=&lt;unavailable&gt;) + 126 at NRGCDOverride.m:89 [opt]
      frame #4: 0x00366116 IssMan - Construction punchlist, on-site inspection and snagging`-[FMDatabaseQueue inDatabase:](self=0x166459f0, _cmd="inDatabase:", block=(IssMan - Construction punchlist, on-site inspection and snagging`__29+[UserDAO getLanguageOfUser:]_block_invoke + 1 at UserDAO.m:445)) + 230 at FMDatabaseQueue.m:151
      frame #5: 0x0038105e IssMan - Construction punchlist, on-site inspection and snagging`+[UserDAO getLanguageOfUser:](self=0x0057d34c, _cmd="getLanguageOfUser:", user=0x16703460) + 298 at UserDAO.m:445
      frame #6: 0x0010288a IssMan - Construction punchlist, on-site inspection and snagging`-[MenuViewController setLanguage](self=0x1717ba00, _cmd="setLanguage") + 226 at MenuViewController.m:316
      frame #7: 0x001038e4 IssMan - Construction punchlist, on-site inspection and snagging`-[MenuViewController viewWillAppear:](self=0x1717ba00, _cmd="viewWillAppear:", animated=YES) + 420 at MenuViewController.m:420
      frame #8: 0x0044ecfa IssMan - Construction punchlist, on-site inspection and snagging`NRMA__boolParamHandler(self=0x1717ba00, selector="viewWillAppear:", targetColor=&lt;unavailable&gt;, p1=&lt;unavailable&gt;) + 42 at NRMAMethodProfiler.m:770 [opt]
      frame #9: 0x2a3ca018 UIKit`-[UIViewController _setViewAppearState:isAnimating:] + 572
      frame #10: 0x2a3c9dba UIKit`-[UIViewController __viewWillAppear:] + 146
      frame #11: 0x2a55f340 UIKit`-[UINavigationController _startCustomTransition:] + 1052
      frame #12: 0x2a46da6e UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 650
      frame #13: 0x2a46d77c UIKit`-[UINavigationController __viewWillLayoutSubviews] + 52
      frame #14: 0x2a46d6f6 UIKit`-[UILayoutContainerView layoutSubviews] + 214
      frame #15: 0x2a3adcc2 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 694
      frame #16: 0x29c75b04 QuartzCore`-[CALayer layoutSublayers] + 128
      frame #17: 0x29c71200 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 352
      frame #18: 0x29c71090 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
      frame #19: 0x29c705b0 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 368
      frame #20: 0x29c70262 QuartzCore`CA::Transaction::commit() + 614
      frame #21: 0x29c69a1e QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 138
      frame #22: 0x26240090 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
      frame #23: 0x2623e386 CoreFoundation`__CFRunLoopDoObservers + 282
      frame #24: 0x2623e7c4 CoreFoundation`__CFRunLoopRun + 972
      frame #25: 0x261910d8 CoreFoundation`CFRunLoopRunSpecific + 516
      frame #26: 0x26190ecc CoreFoundation`CFRunLoopRunInMode + 108
      frame #27: 0x2f506af8 GraphicsServices`GSEventRunModal + 160
      frame #28: 0x2a41a2dc UIKit`UIApplicationMain + 144
      frame #29: 0x000d0872 IssMan - Construction punchlist, on-site inspection and snagging`main(argc=1, argv=0x008c5a78) + 102 at main.m:17
  
    thread #4: tid = 0x57db4, 0x3815d320 libsystem_kernel.dylib`kevent_qos + 24, queue = 'com.apple.libdispatch-manager'
      frame #0: 0x3815d320 libsystem_kernel.dylib`kevent_qos + 24
      frame #1: 0x008f75f6 libdispatch.dylib`_dispatch_mgr_invoke + 254
      frame #2: 0x008e89ce libdispatch.dylib`_dispatch_mgr_thread + 38
  
    thread #5: tid = 0x57db7, 0x3814800c libsystem_kernel.dylib`semaphore_wait_trap + 8, queue = 'harvesterQueue'
      frame #0: 0x3814800c libsystem_kernel.dylib`semaphore_wait_trap + 8
      frame #1: 0x008f54ba libdispatch.dylib`_dispatch_semaphore_wait_slow + 190
      frame #2: 0x25ae3cdc CFNetwork`CFURLConnectionSendSynchronousRequest + 272
      frame #3: 0x25afe2fe CFNetwork`+[NSURLConnection sendSynchronousRequest:returningResponse:error:] + 94
      frame #4: 0x004387ba IssMan - Construction punchlist, on-site inspection and snagging`__65+[NRMANSURLConnectionSupport poseImplementationBlockForSelector:]_block_invoke(.block_descriptor=&lt;unavailable&gt;, _self=&lt;unavailable&gt;, request=0x1657a3f0, response=0x401969dc, error=&lt;unavailable&gt;) + 146 at NRMANSURLConnectionSupport.m:204 [opt]
      frame #5: 0x00442faa IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvesterConnection send:](self=&lt;unavailable&gt;, _cmd=&lt;unavailable&gt;, post=&lt;unavailable&gt;) + 130 at NRMAHarvesterConnection.m:97 [opt]
      frame #6: 0x00443808 IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvesterConnection sendData:](self=0x165574c0, _cmd=&lt;unavailable&gt;, harvestable=&lt;unavailable&gt;) + 416 at NRMAHarvesterConnection.m:160 [opt]
      frame #7: 0x0043c784 IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvester connected](self=&lt;unavailable&gt;, _cmd=&lt;unavailable&gt;) + 724 at NRMAHarvester.m:268 [opt]
      frame #8: 0x0043e23a IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvester execute](self=&lt;unavailable&gt;, _cmd=&lt;unavailable&gt;) + 522 at NRMAHarvester.m:505 [opt]
      frame #9: 0x0043cf4c IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvester disconnected](self=&lt;unavailable&gt;, _cmd=&lt;unavailable&gt;) + 276 at NRMAHarvester.m:350 [opt]
      frame #10: 0x0043e1f6 IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvester execute](self=&lt;unavailable&gt;, _cmd=&lt;unavailable&gt;) + 454 at NRMAHarvester.m:500 [opt]
      frame #11: 0x004471f4 IssMan - Construction punchlist, on-site inspection and snagging`__30+[NRMAHarvestController start]_block_invoke_2(.block_descriptor=&lt;unavailable&gt;) + 412 at NRMAHarvestController.m:143 [opt]
      frame #12: 0x008e6d16 libdispatch.dylib`_dispatch_call_block_and_release + 10
      frame #13: 0x008f161a libdispatch.dylib`_dispatch_queue_drain + 2014
      frame #14: 0x008e9f52 libdispatch.dylib`_dispatch_queue_invoke + 282
      frame #15: 0x008f306e libdispatch.dylib`_dispatch_root_queue_drain + 1802
      frame #16: 0x008f2960 libdispatch.dylib`_dispatch_worker_thread3 + 100
      frame #17: 0x381f8e0c libsystem_pthread.dylib`_pthread_wqthread + 1024
      frame #18: 0x381f89fc libsystem_pthread.dylib`start_wqthread + 8
  
    thread #6: tid = 0x57dba, 0x3815bf14 libsystem_kernel.dylib`__select + 20, name = 'com.apple.CFSocket.private'
      frame #0: 0x3815bf14 libsystem_kernel.dylib`__select + 20
      frame #1: 0x26245930 CoreFoundation`__CFSocketManager + 572
      frame #2: 0x381fac7e libsystem_pthread.dylib`_pthread_body + 138
      frame #3: 0x381fabf2 libsystem_pthread.dylib`_pthread_start + 110
      frame #4: 0x381f8a08 libsystem_pthread.dylib`thread_start + 8
  
    thread #7: tid = 0x57dc4, 0x38147fbc libsystem_kernel.dylib`mach_msg_trap + 20, name = 'com.apple.NSURLConnectionLoader'
      frame #0: 0x38147fbc libsystem_kernel.dylib`mach_msg_trap + 20
      frame #1: 0x38147dbc libsystem_kernel.dylib`mach_msg + 40
      frame #2: 0x2624048c CoreFoundation`__CFRunLoopServiceMachPort + 136
      frame #3: 0x2623e812 CoreFoundation`__CFRunLoopRun + 1050
      frame #4: 0x261910d8 CoreFoundation`CFRunLoopRunSpecific + 516
      frame #5: 0x26190ecc CoreFoundation`CFRunLoopRunInMode + 108
      frame #6: 0x25afdd9e CFNetwork`+[NSURLConnection(Loader) _resourceLoadLoop:] + 486
      frame #7: 0x2705436c Foundation`__NSThread__start__ + 1144
      frame #8: 0x381fac7e libsystem_pthread.dylib`_pthread_body + 138
      frame #9: 0x381fabf2 libsystem_pthread.dylib`_pthread_start + 110
      frame #10: 0x381f8a08 libsystem_pthread.dylib`thread_start + 8
  
    thread #8: tid = 0x57dc8, 0x3815c88c libsystem_kernel.dylib`__workq_kernreturn + 8
      frame #0: 0x3815c88c libsystem_kernel.dylib`__workq_kernreturn + 8
      frame #1: 0x381f8e18 libsystem_pthread.dylib`_pthread_wqthread + 1036
      frame #2: 0x381f89fc libsystem_pthread.dylib`start_wqthread + 8
  
    thread #10: tid = 0x57dca, 0x3815c024 libsystem_kernel.dylib`__semwait_signal + 24, queue = 'NSOperationQueue 0x165eafe0 :: NSOperation 0x16782190 (QOS: USER_INTERACTIVE)'
      frame #0: 0x3815c024 libsystem_kernel.dylib`__semwait_signal + 24
      frame #1: 0x380b391c libsystem_c.dylib`nanosleep + 172
      frame #2: 0x27053856 Foundation`+[NSThread sleepForTimeInterval:] + 142
      frame #3: 0x002e1d24 IssMan - Construction punchlist, on-site inspection and snagging`-[MetaDataSyncOperation main](self=0x16782190, _cmd="main") + 368 at MetaDataSyncOperation.m:48
      frame #4: 0x26f903ce Foundation`-[__NSOperationInternal _start:] + 774
      frame #5: 0x2703e82c Foundation`__NSOQSchedule_f + 192
      frame #6: 0x008f161a libdispatch.dylib`_dispatch_queue_drain + 2014
      frame #7: 0x008e9f52 libdispatch.dylib`_dispatch_queue_invoke + 282
      frame #8: 0x008f2b0e libdispatch.dylib`_dispatch_root_queue_drain + 426
      frame #9: 0x008f2960 libdispatch.dylib`_dispatch_worker_thread3 + 100
      frame #10: 0x381f8e0c libsystem_pthread.dylib`_pthread_wqthread + 1024
      frame #11: 0x381f89fc libsystem_pthread.dylib`start_wqthread + 8
  
    thread #11: tid = 0x57dcd, 0x38147fbc libsystem_kernel.dylib`mach_msg_trap + 20, name = 'AFNetworking'
      frame #0: 0x38147fbc libsystem_kernel.dylib`mach_msg_trap + 20
      frame #1: 0x38147dbc libsystem_kernel.dylib`mach_msg + 40
      frame #2: 0x2624048c CoreFoundation`__CFRunLoopServiceMachPort + 136
      frame #3: 0x2623e812 CoreFoundation`__CFRunLoopRun + 1050
      frame #4: 0x261910d8 CoreFoundation`CFRunLoopRunSpecific + 516
      frame #5: 0x26190ecc CoreFoundation`CFRunLoopRunInMode + 108
      frame #6: 0x26f8388c Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 268
      frame #7: 0x26fd241c Foundation`-[NSRunLoop(NSRunLoop) run] + 80
      frame #8: 0x003e306e IssMan - Construction punchlist, on-site inspection and snagging`+[AFURLConnectionOperation networkRequestThreadEntryPoint:](self=0x0057d914, _cmd="networkRequestThreadEntryPoint:", object=0x00000000) + 354 at AFURLConnectionOperation.m:163
      frame #9: 0x2705436c Foundation`__NSThread__start__ + 1144
      frame #10: 0x381fac7e libsystem_pthread.dylib`_pthread_body + 138
      frame #11: 0x381fabf2 libsystem_pthread.dylib`_pthread_start + 110
      frame #12: 0x381f8a08 libsystem_pthread.dylib`thread_start + 8
  
    thread #12: tid = 0x57dce, 0x38148dc0 libsystem_kernel.dylib`pread + 20, queue = 'fmdb.&lt;FMDatabaseQueue: 0x166459f0&gt;'
      frame #0: 0x38148dc0 libsystem_kernel.dylib`pread + 20
      frame #1: 0x37e41252 libsqlite3.dylib`___lldb_unnamed_function356$$libsqlite3.dylib + 206
      frame #2: 0x37ddded0 libsqlite3.dylib`___lldb_unnamed_function25$$libsqlite3.dylib + 24
      frame #3: 0x37df6824 libsqlite3.dylib`___lldb_unnamed_function77$$libsqlite3.dylib + 120
      frame #4: 0x37df5bb6 libsqlite3.dylib`___lldb_unnamed_function72$$libsqlite3.dylib + 718
      frame #5: 0x37e47f7a libsqlite3.dylib`___lldb_unnamed_function431$$libsqlite3.dylib + 250
      frame #6: 0x37e2ef5a libsqlite3.dylib`___lldb_unnamed_function250$$libsqlite3.dylib + 718
      frame #7: 0x37e3bffe libsqlite3.dylib`___lldb_unnamed_function316$$libsqlite3.dylib + 234
      frame #8: 0x37e186b6 libsqlite3.dylib`___lldb_unnamed_function159$$libsqlite3.dylib + 158
      frame #9: 0x37e0c4b0 libsqlite3.dylib`___lldb_unnamed_function114$$libsqlite3.dylib + 24192
      frame #10: 0x37e05b48 libsqlite3.dylib`sqlite3_step + 472
      frame #11: 0x00280fe0 IssMan - Construction punchlist, on-site inspection and snagging`-[FMResultSet nextWithError:](self=0x17a2d5b0, _cmd="nextWithError:", outErr=0x00000000) + 64 at FMResultSet.m:155
      frame #12: 0x00280f9a IssMan - Construction punchlist, on-site inspection and snagging`-[FMResultSet next](self=0x17a2d5b0, _cmd="next") + 42 at FMResultSet.m:150
      frame #13: 0x001001da IssMan - Construction punchlist, on-site inspection and snagging`__45-[SyncStatusCalculatorOperation updateStatus]_block_invoke101(.block_descriptor=0x40524bcc, db=0x1667e750) + 198 at SyncStatusCalculatorOperation.m:152
      frame #14: 0x00366194 IssMan - Construction punchlist, on-site inspection and snagging`__30-[FMDatabaseQueue inDatabase:]_block_invoke(.block_descriptor=0x40524a54) + 88 at FMDatabaseQueue.m:151
      frame #15: 0x004531c6 IssMan - Construction punchlist, on-site inspection and snagging`__NR__dispatch_sync_block_invoke(.block_descriptor=&lt;unavailable&gt;) + 30 at NRGCDOverride.m:91 [opt]
      frame #16: 0x008e6d02 libdispatch.dylib`_dispatch_client_callout + 22
      frame #17: 0x008f04fa libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 102
      frame #18: 0x0045318a IssMan - Construction punchlist, on-site inspection and snagging`NR__dispatch_sync(queue=0x1667fc50, block=&lt;unavailable&gt;) + 126 at NRGCDOverride.m:89 [opt]
      frame #19: 0x00366116 IssMan - Construction punchlist, on-site inspection and snagging`-[FMDatabaseQueue inDatabase:](self=0x166459f0, _cmd="inDatabase:", block=(IssMan - Construction punchlist, on-site inspection and snagging`__45-[SyncStatusCalculatorOperation updateStatus]_block_invoke101 + 1 at SyncStatusCalculatorOperation.m:140)) + 230 at FMDatabaseQueue.m:151
      frame #20: 0x000ff3e4 IssMan - Construction punchlist, on-site inspection and snagging`-[SyncStatusCalculatorOperation updateStatus](self=0x178ed200, _cmd="updateStatus") + 1884 at SyncStatusCalculatorOperation.m:140
      frame #21: 0x000fec7a IssMan - Construction punchlist, on-site inspection and snagging`-[SyncStatusCalculatorOperation main](self=0x178ed200, _cmd="main") + 290 at SyncStatusCalculatorOperation.m:35
      frame #22: 0x26f903ce Foundation`-[__NSOperationInternal _start:] + 774
      frame #23: 0x2703e82c Foundation`__NSOQSchedule_f + 192
      frame #24: 0x008f161a libdispatch.dylib`_dispatch_queue_drain + 2014
      frame #25: 0x008e9f52 libdispatch.dylib`_dispatch_queue_invoke + 282
      frame #26: 0x008f2b0e libdispatch.dylib`_dispatch_root_queue_drain + 426
      frame #27: 0x008f2960 libdispatch.dylib`_dispatch_worker_thread3 + 100
      frame #28: 0x381f8e0c libsystem_pthread.dylib`_pthread_wqthread + 1024
      frame #29: 0x381f89fc libsystem_pthread.dylib`start_wqthread + 8
  
    thread #13: tid = 0x57dd1, 0x38147fbc libsystem_kernel.dylib`mach_msg_trap + 20, name = 'WebThread'
      frame #0: 0x38147fbc libsystem_kernel.dylib`mach_msg_trap + 20
      frame #1: 0x38147dbc libsystem_kernel.dylib`mach_msg + 40
      frame #2: 0x2624048c CoreFoundation`__CFRunLoopServiceMachPort + 136
      frame #3: 0x2623e812 CoreFoundation`__CFRunLoopRun + 1050
      frame #4: 0x261910d8 CoreFoundation`CFRunLoopRunSpecific + 516
      frame #5: 0x26190ecc CoreFoundation`CFRunLoopRunInMode + 108
      frame #6: 0x351868d6 WebCore`RunWebThread(void*) + 422
      frame #7: 0x381fac7e libsystem_pthread.dylib`_pthread_body + 138
      frame #8: 0x381fabf2 libsystem_pthread.dylib`_pthread_start + 110
      frame #9: 0x381f8a08 libsystem_pthread.dylib`thread_start + 8
  
    thread #15: tid = 0x57e0e, 0x3815c88c libsystem_kernel.dylib`__workq_kernreturn + 8
      frame #0: 0x3815c88c libsystem_kernel.dylib`__workq_kernreturn + 8
      frame #1: 0x381f8e18 libsystem_pthread.dylib`_pthread_wqthread + 1036
      frame #2: 0x381f89fc libsystem_pthread.dylib`start_wqthread + 8
  </code></pre>
  
  <p>Here is the source of updateStatus:</p>
  
  <pre><code>-(void)updateStatus {
  
      if(isCalculating)return;
  
      isCalculating = YES;
      NSMutableArray *tableList = [[NSMutableArray alloc] init];
  
      [[IssMANAppDelegate appDelegate].dbQueue inDatabase:^(FMDatabase *db) {
          FMResultSet *resultsSet = [db executeQuery:@"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"];
          while ([resultsSet next]){
              [tableList addObject:[resultsSet stringForColumn:@"name"]];
          }
          [resultsSet close];
      }];
  
      __block int totalRow = 0;
      __block int pendingRow = 0;
  //    totalPhotoRow = 0;
      __block int pendingPhotoRow = 0;
      __block int pendingDataDownload = 0;
      __block int pendingPhotoDownload = 0;
  
  //    __block NSInteger pendingPhotoRow = 0;
      [[IssMANAppDelegate appDelegate].dbQueue inDatabase:^(FMDatabase *db) {
          FMResultSet *resultSet = nil;
          resultSet = [db executeQuery:@"SELECT COUNT(*) C FROM media_content, media WHERE media.pk_id = media_content.media_pk_id AND media.status = 'active' AND media_content.is_dirty = 1"];
  
          while([resultSet next]){
              pendingPhotoRow = [resultSet intForColumn:@"C"];
          }
          [resultSet close];
      }];
  
      //    pendingPhotoRow = [MediaDAO getPendingUploadImages];
      for(int k=0;k&lt;tableList.count;k++) {
  
          NSString* tableName = (NSString*) [tableList objectAtIndex:k];
  
          if([tableName isEqualToString:@"settings"])continue;
          if([tableName isEqualToString:@"sqlite_sequence"])continue;
          if([tableName isEqualToString:@"temp_purchased_transactions"])continue;
          if([tableName isEqualToString:@"latest_project_report_issue"])continue;
          if([tableName isEqualToString:@"role"])continue;
          if([tableName isEqualToString:@"event_type"])continue;
          if([tableName isEqualToString:@"rel_role_event_type_notification_type"])continue;
          if([tableName isEqualToString:@"rel_user_product"])continue;
          if([tableName isEqualToString:@"media_content"])continue;
  
          [[IssMANAppDelegate appDelegate].dbQueue inDatabase:^(FMDatabase *db) {
              NSString* sql = NULL;
              if([ISSManDBManager columnExists:@"status" inTableWithName:tableName andDB:db]) {
  
                  sql = [NSString stringWithFormat:@"SELECT COUNT(*) C FROM %@ where status &lt;&gt; 'deleted'",tableName];
              }
              else if([ISSManDBManager columnExists:@"sender_status" inTableWithName:tableName andDB:db]) {
  
                  sql = [NSString stringWithFormat:@"SELECT COUNT(*) C FROM %@ where sender_status &lt;&gt; 'deleted'",tableName];
          }
          else {
  
              sql = [NSString stringWithFormat:@"SELECT COUNT(*) C FROM %@",tableName];
          }
  
          FMResultSet *resultSet = [db executeQuery:sql];
  
          while([resultSet next]) {
              int rowCount = [resultSet intForColumn:@"C"];
              if([tableName isEqualToString:@"media_content"]) {
  //                    totalPhotoRow += rowCount;
              }
              else {
                  totalRow += rowCount;
              }
          }
  
          if([ISSManDBManager columnExists:@"status" inTableWithName:tableName andDB:db]) {
              sql = [NSString stringWithFormat:@"SELECT COUNT(*) C FROM %@ WHERE is_dirty = 1 and status &lt;&gt; 'deleted'",tableName];
          }
          else if([ISSManDBManager columnExists:@"sender_status" inTableWithName:tableName andDB:db]) {
              sql = [NSString stringWithFormat:@"SELECT COUNT(*) C FROM %@ WHERE is_dirty = 1 and sender_status &lt;&gt; 'deleted'",tableName];
          }
          else {
              sql = [NSString stringWithFormat:@"SELECT COUNT(*) C FROM %@ WHERE is_dirty = 1",tableName];
          }
          resultSet = [db executeQuery:sql];
  
          while([resultSet next]) {
              NSInteger rowCount = [resultSet intForColumn:@"C"];
              pendingRow += rowCount;
          }
          [resultSet close];
      }];
  }
  
  [[IssMANAppDelegate appDelegate].dbQueue inDatabase:^(FMDatabase *db) {
  
      NSString* sql = NULL;
      sql = [NSString stringWithFormat:@"SELECT COUNT(media.pk_id) C \
             FROM media, media_content \
             WHERE \
             media.pk_id = media_content.media_pk_id AND \
             media.status = 'active' AND \
             media.update_date_time &gt; media_content.last_sync_time"];
  
      FMResultSet *resultingSet = [db executeQuery:sql];
  
      while ([resultingSet next]) {
  
          int rowCount = [resultingSet intForColumn:@"C"];
          pendingPhotoDownload += rowCount;
      }
      [resultingSet close];
  
      NSString *sqlQuery = [NSString stringWithFormat:@"SELECT COUNT(pk_id) C FROM rel_user_product"];
      FMResultSet *resultsSets = [db executeQuery:sqlQuery];
  
  
      while([resultsSets next]) {
  
          int rowCount = [resultsSets intForColumn:@"C"];
          pendingDataDownload += rowCount;
      }
      [resultsSets close];
  }];
  
  double ratio = 0.0;
  if(totalRow)ratio = (double)pendingRow / (double)totalRow * 100.0;
  
  ratio = floor(100.0 - ratio);
  double downloadRation = 0.0;
  if(pendingDataDownload) downloadRation = 100.0;
  
  isCalculating = NO;
  
  NSMutableDictionary* syncStatusDic = [[NSMutableDictionary alloc] init];
  [syncStatusDic setObject:[NSNumber numberWithInt:totalRow] forKey:TOTAL_ROW];
  [syncStatusDic setObject:[NSNumber numberWithInt:pendingRow] forKey:PENDING_ROW];
  [syncStatusDic setObject:[NSNumber numberWithInt:pendingPhotoRow] forKey:PENDING_PHOTO];
  [syncStatusDic setObject:[NSNumber numberWithInt:pendingDataDownload] forKey:PENDING_DATA_DOWNLOAD];
  [syncStatusDic setObject:[NSNumber numberWithInt:pendingPhotoDownload] forKey:PENDING_PHOTO_DOWNLOAD];
  
      [self.delegate updateSyncStatusValues: [NSDictionary dictionaryWithDictionary:syncStatusDic]]; // safe conversion from NSMutableDictionary to NSDictionary
  }
  </code></pre>