semaphore_wait_trap 阻塞 UI

Posted

技术标签:

【中文标题】semaphore_wait_trap 阻塞 UI【英文标题】:semaphore_wait_trap blocking the UI 【发布时间】:2015-11-24 04:20:35 【问题描述】:

由于 semaphore_wait_trap,我的应用程序 UI 卡住了。我不知道如何追踪它。我检查了我是否正在从后台线程更新任何 UI。但是没有找到。这是堆栈跟踪。

任何帮助将不胜感激。

这是线程回溯结果:

    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=<unavailable>) + 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=<unavailable>, p1=<unavailable>) + 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=<unavailable>, _self=<unavailable>, request=0x1657a3f0, response=0x401969dc, error=<unavailable>) + 146 at NRMANSURLConnectionSupport.m:204 [opt]
    frame #5: 0x00442faa IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvesterConnection send:](self=<unavailable>, _cmd=<unavailable>, post=<unavailable>) + 130 at NRMAHarvesterConnection.m:97 [opt]
    frame #6: 0x00443808 IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvesterConnection sendData:](self=0x165574c0, _cmd=<unavailable>, harvestable=<unavailable>) + 416 at NRMAHarvesterConnection.m:160 [opt]
    frame #7: 0x0043c784 IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvester connected](self=<unavailable>, _cmd=<unavailable>) + 724 at NRMAHarvester.m:268 [opt]
    frame #8: 0x0043e23a IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvester execute](self=<unavailable>, _cmd=<unavailable>) + 522 at NRMAHarvester.m:505 [opt]
    frame #9: 0x0043cf4c IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvester disconnected](self=<unavailable>, _cmd=<unavailable>) + 276 at NRMAHarvester.m:350 [opt]
    frame #10: 0x0043e1f6 IssMan - Construction punchlist, on-site inspection and snagging`-[NRMAHarvester execute](self=<unavailable>, _cmd=<unavailable>) + 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=<unavailable>) + 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.<FMDatabaseQueue: 0x166459f0>'
    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=<unavailable>) + 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=<unavailable>) + 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

这里是updateStatus的来源:

-(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<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 <> 'deleted'",tableName];
            
            else if([ISSManDBManager columnExists:@"sender_status" inTableWithName:tableName andDB:db]) 

                sql = [NSString stringWithFormat:@"SELECT COUNT(*) C FROM %@ where sender_status <> '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 <> '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 <> '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 > 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

【问题讨论】:

它正在等待执行队列中的一个块。队列被锁定,所以它可能正在另一个线程上执行一个块。您需要查看其他线程的堆栈跟踪,以了解哪个线程阻塞了队列以及原因。 @robmayoff > 对不起,如果这听起来很愚蠢,我怎么知道哪个线程阻塞了其他线程的队列? @robmayoff > 我添加了另一个线程队列,我认为这可能是原因。我对么?感谢您的宝贵时间。 在调试器中(在(lldb) 提示符处),键入thread backtrace all。复制输出并将其粘贴到您的问题中。 @robmayoff > 结果已添加。 【参考方案1】:

回溯中的线程#12 正在数据库队列上运行,执行-[SyncStatusCalculatorOperation updateStatus](帧#13)。在该块返回之前,没有其他线程可以在数据库队列上运行。您的主线程(线程 #1)将阻塞,直到线程 #12 退出数据库队列。

您需要查看-[SyncStatusCalculatorOperation updateStatus] 才能弄清楚为什么需要这么长时间。

【讨论】:

你能建议如何克服这个问题吗?我在后台线程中做了这部分。无法确定为什么会阻塞。我已经添加了该功能的来源。关于我的问题。 在调试导航器中找到该线程,然后单击-[FMResultSet next] 正上方的框架。它正在执行您的方法的哪个块?什么是 SQL 查询?您是否有适当的索引来加快查询速度? 是的,我有索引。我已经编辑了我的问题并添加了方法。 我看到您添加了消息,但我不知道哪一行是第 152 行。 152 是空行,153 和 154 是这些: NSString *sqlQuery = [NSString stringWithFormat:@"SELECT COUNT(pk_id) C FROM rel_user_product"]; FMResultSet *resultsSets = [db executeQuery:sqlQuery];

以上是关于semaphore_wait_trap 阻塞 UI的主要内容,如果未能解决你的问题,请参考以下文章

semaphore_wait_trap、GCD 和 CocoaAsyncSocket

CoreData 阻塞 UI

Android Studio学习随笔-UI线程阻塞以及优化

Async/Await 仍然阻塞 UI?

如何追踪阻塞主(UI)线程的调用?

等待QThread时UI会阻塞/如何正确使用QThread