2015-10-21 83 views
4

enter image description hereenter image description herePDFDocument removePageAtIndex:不更新時工作到Mac OS X 10.11

我使用PDFKit框架到我的可可應用程序的PDFViewer.When我想刪除的頁面的一個從PDFDocument的應用凍結在代碼

[[self pdfDocument] removePageAtIndex:0]; // can see this Problem only in Mac OS X 10.11 

行這完美的作品,當我在的Mac OS上運行的應用程序X 10.10

我讀了所有相關文件蘋果,但我還沒有得到任何解決方案。

這裏是回溯:

* thread #1: tid = 0x85e1, 0x00007fff92571f5e libsystem_kernel.dylib`__psynch_cvwait + 10, queue = 'com.apple.main-thread', stop reason = signal SIGTERM 
    frame #0: 0x00007fff92571f5e libsystem_kernel.dylib`__psynch_cvwait + 10 
    frame #1: 0x00000001006c05f7 libsystem_pthread.dylib`_pthread_cond_wait + 767 
    frame #2: 0x00007fff904c6e32 Foundation`-[__NSOperationInternal _waitUntilFinished:] + 131 
    frame #3: 0x00007fff904921fa Foundation`-[NSOperationQueue waitUntilAllOperationsAreFinished] + 254 
    * frame #4: 0x000000010017efe1 Neat`-[NRMPDFCoordinator waitUntilAllOperationsAreFinished](self=0x0000608000ad3be0, _cmd=0x00007fff8877e285) + 145 at NRMPDFCoordinator.m:1362 
    frame #5: 0x00000001000109cf Neat`-[NRMItemEditorDocument saveDocumentWithDelegate:didSaveSelector:contextInfo:](self=0x000060000094a190, _cmd=0x00007fff88777581, delegate=0x0000000000000000, didSaveSelector=0x0000000000000000, contextInfo=0x0000000000000000) + 1151 at NRMItemEditorDocument.m:325 
    frame #6: 0x000000010001018a Neat`-[NRMItemEditorDocument saveDocument:](self=0x000060000094a190, _cmd=0x00007fff8874cbb4, sender=0x00006080003a8b20) + 58 at NRMItemEditorDocument.m:234 
    frame #7: 0x0000000100013bef Neat`-[NRMItemEditorWindowController saveAndClose:](self=0x00006080003a8b20, _cmd=0x00000001002cf2d2, sender=0x000060000094a5b0) + 95 at NRMItemEditorWindowController.m:244 
    frame #8: 0x00007fff87068082 libsystem_trace.dylib`_os_activity_initiate + 75 
    frame #9: 0x00007fff87fc79b5 AppKit`-[NSApplication sendAction:to:from:] + 460 
    frame #10: 0x00007fff87fd9bb2 AppKit`-[NSControl sendAction:to:] + 86 
    frame #11: 0x00007fff87fd9adc AppKit`__26-[NSCell _sendActionFrom:]_block_invoke + 131 
    frame #12: 0x00007fff87068082 libsystem_trace.dylib`_os_activity_initiate + 75 
    frame #13: 0x00007fff87fd9a39 AppKit`-[NSCell _sendActionFrom:] + 144 
    frame #14: 0x00007fff87068082 libsystem_trace.dylib`_os_activity_initiate + 75 
    frame #15: 0x00007fff87fd805e AppKit`-[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2693 
    frame #16: 0x00007fff88020d1c AppKit`-[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 744 
    frame #17: 0x00007fff87fd6788 AppKit`-[NSControl mouseDown:] + 669 
    frame #18: 0x00007fff88524575 AppKit`-[NSWindow _handleMouseDownEvent:isDelayedEvent:] + 6322 
    frame #19: 0x00007fff88525559 AppKit`-[NSWindow _reallySendEvent:isDelayedEvent:] + 212 
    frame #20: 0x00007fff87f6ad31 AppKit`-[NSWindow sendEvent:] + 517 
    frame #21: 0x00007fff87eeaccb AppKit`-[NSApplication sendEvent:] + 2540 
    frame #22: 0x0000000100225f35 Neat`-[NRMApplication sendEvent:](self=0x00006000001205a0, _cmd=0x00007fff88749e04, event=0x0000608000725640) + 1141 at NRMApplication.m:95 
    frame #23: 0x00007fff87d51f3e AppKit`-[NSApplication run] + 796 
    frame #24: 0x00007fff87d1b162 AppKit`NSApplicationMain + 1176 
    frame #25: 0x0000000100012d67 Neat`main(argc=3, argv=0x00007fff5fbff718) + 119 at main.m:21 
    frame #26: 0x0000000100001e74 Neat`start + 52 

這裏是我使用PDFDocument的removePageAtIndex方法

-(NSError *)removePageOpImpl:(NRMPDFOperation *)op 
{ 
    NSLog(@"\n Inside removePageOpImpl Method ..."); 
    NSError* error = [self loadDocument]; 
    if(!error) 
    { 
     NSUInteger index = [self pageIndexForId:[op pageId]]; 
     NSLog(@"Page count: %ld", [self pageCount]); 
     if(index < [self pageCount]) 
     { 
      NSLog(@"PDF Document:-- %@", [self pdfDocument]); 
      NSLog(@"Index is: %ld", index); 
      @try { 

       [(PDFDocument *)[self pdfDocument] removePageAtIndex:index];//At this line the app getting freezed and control is ended. 

       NSLog(@"Page count after delete: %ld", [self pageCount]); 


      } 
      @catch (NSException *exception) { 
       NSLog(@"Exception: %@", exception); 
      } 
      @finally { 
       NSLog(@"Finally called"); 
       [[self mutablePageIdList] removeObjectAtIndex:index]; 
       [self updatePageLabelsFromIndex:index]; 
       [self updateChangeCount:NSChangeDone]; 
       self.contentsChanged = YES; 
      } 
     } 
     else 
     { 
      // TODO: error 
     } 
    } 
    return error; 
} 

任何人都可以請建議我可能是什麼問題的方法......還附上阻塞UI的隊列屏幕截圖

我嘗試在主隊列上應用dispatch_async到PDFDocument頁面刪除操作,如下所示

- (NSError *)removePageOpImpl:(NRMPDFOperation *)op 
{ 
    NSError* error = [self loadDocument]; 
    if(!error) 
    { 
     NSUInteger index = [self pageIndexForId:[op pageId]]; 
     if(index < [self pageCount]) 
     { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
         [[self pdfDocument] removePageAtIndex:index]; 
         [[self mutablePageIdList] removeObjectAtIndex:index]; 
         [self updatePageLabelsFromIndex:index]; 
         [self updateChangeCount:NSChangeDone]; 
         self.contentsChanged = YES; 

         }); 
     } 
     else 
     { 
      // TODO: error 
     } 
    } 
    return error; 
} 

現在的應用程序沒有掛,但我陷入了另一個問題。我有其他的操作應該在removePageOpImpl操作後同步運行。但是它們在removePageOpImpl完成之前執行,它正在改變我的應用程序的行爲。你能建議我如何在removePageOpImpl後同步執行其他操作。我讀了關於完成處理程序,但在這種情況下,我很困惑如何使用它。

請建議

+0

你可以發佈堆棧回溯? – Aderstedt

+0

@Aderstedt:我試圖打印回溯但沒有打印。 –

+0

您是否正在Xcode內運行應用程序?如果是這樣,你應該在那裏看到回溯。 – Aderstedt

回答

2

您已經創建了一個可愛的小僵局在這裏。

你的主線程等待操作完成,但操作(該軌跡示)...

enter image description here

在後臺線程上運行,正如你所看到的,它的等待最有可能僅從主線程發出信號的信號量。這對主線程具有諸如dispatch_sync之類的特徵。由於它發生在PDFDocument實現中,我想它會試圖確保它在返回給用戶之前在主線程上運行某些內容(讀寫器鎖

因此,您的主線程正在等待某個操作完成,並且該操作正在等待主線程完成它正在嘗試執行的操作經典死鎖

發生這種情況是因爲主線程啓動了保存,然後等待所有內容在完成之前完成(但其他一些內容工作也需要在主線程上運行)

你需要啓動保存爲異步操作,所以它不在ma上運行在等待操作完成時在線程中。然後,當完成保存時,如果您必須更新UI,則可以通過主線程上運行的完成塊或委託來報告其成功/失敗。

+0

您的回答真的很有用謝謝您能否清楚我的觀點如果我的處理隊列的設計不合適,它如何在所有以前的OS X版本中工作,無需UI這是我唯一懷疑的唯一疑問 –

+0

誰知道,也許他們改變了PDFDocume nt與主隊列協調。有時,我們做的東西是不正確的,但不會中斷...這就是爲什麼我們應該總是嘗試做「正確的事情」 –

+0

我更新了我的問題,我遇到了另一個問題,能否請你給我建議這個。 –