2015-06-14 48 views
2

我正在使用下面的函數中的一些數據填充NSComboBox。它填充後,我嘗試滾動項目,我收到CATransaction警告。任何人都可以闡明爲什麼會發生這種情況,我能做些什麼來解決它?我發現它可能與更改主線程以外的線程上的組​​合框的UI有關,但之後我卡住了。CoreAnimation未提交CATranaction NSComboBox

func getAllRecords() 
{ 
    CATransaction.begin() 
    let url = NSURL(string: "http://URL.php") 
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) 
     { 
      (data, response, error) in 
      var d = NSString(data: data, encoding: NSUTF8StringEncoding) 
      var arr = d!.componentsSeparatedByString("<") // spliting the incoming string from "<" operator because before that operator is our required data and storing in array 
      var dataWeNeed:NSString = arr[0] as! NSString // arr[0] is the data before "<" operator and arr[1] is actually no use for us 
      if let data = NSJSONSerialization.JSONObjectWithData(dataWeNeed.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSArray 
       { 
        for dd in data 
         { 
          var name : String = dd["Name"]! as! String 
          var email : String = dd["Email"]! as! String 
          //println("Name: \(name)") 
          //println("Email: \(email)") 
          self.userComboBox.addItemsWithObjectValues([name]) 
         } 
       } 
     } 
    task.resume() 
    CATransaction.commit() 
} 

這是我從調試區域得到的警告。

2015-06-14 13:54:04.756 Green Time Clock[2150:738395] Unexpected  outstanding background CATransaction 
CoreAnimation: warning, encountered thread with uncommitted  CATransaction; created by: 
0 QuartzCore       0x00007fff9ab4d6c2  _ZN2CA11Transaction4pushEv + 312 
1 QuartzCore       0x00007fff9ab689a8 _ZN2CA11Transaction15ensure_implicitEv + 276 
2 QuartzCore       0x00007fff9ab4d842 _ZN2CA11Transaction9set_valueEj12_CAValueTypePKv + 40 
3 QuartzCore       0x00007fff9ab4f452 +[CATransaction setDisableActions:] + 38 
4 AppKit        0x00007fff921a1b8c -[NSView(NSInternal) _updateLayerGeometryFromView] + 389 
5 AppKit        0x00007fff921c7d09 -[NSView setFrameSize:] + 1129 
6 AppKit        0x00007fff921c789a -[NSControl setFrameSize:] + 77 
7 AppKit        0x00007fff922e3891 -[NSTableView setFrameSize:] + 256 
8 AppKit        0x00007fff922e35f9 -[NSTableView _tileAndRedisplayAll] + 180 
9 Green Time Clock     0x00000001000042d6 _TFFC16Green_Time_Clock14ViewController13getAllRecordsFS0_FT_T_U_FTGSQCSo6NSData_GSQCSo13NSURLResponse_GSQCSo7NSError__T_ + 2886 
10 Green Time Clock     0x0000000100004463 _TTRXFo_oGSQCSo6NSData_oGSQCSo13NSURLResponse_oGSQCSo7NSError__dT__XFo_iTGSQS__GSQS0__GSQS1____iT__ + 51 
11 Green Time Clock     0x0000000100001e31 _TPA__TTRXFo_oGSQCSo6NSData_oGSQCSo13NSURLResponse_oGSQCSo7NSError__dT__XFo_iTGSQS__GSQS0__GSQS1____iT__ + 81 
12 Green Time Clock     0x0000000100004493 _TTRXFo_iTGSQCSo6NSData_GSQCSo13NSURLResponse_GSQCSo7NSError___iT__XFo_oGSQS__oGSQS0__oGSQS1___dT__ + 35 
13 Green Time Clock     0x00000001000044fa _TTRXFo_oGSQCSo6NSData_oGSQCSo13NSURLResponse_oGSQCSo7NSError__dT__XFdCb_dGSQS__dGSQS0__dGSQS1___dT__ + 90 
14 CFNetwork       0x00007fff8c09cba2 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 157 
15 Foundation       0x00007fff9a75f7e8 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7 
+0

我只是用NSTextView時發現,在命令行同樣的警告。 @Charles,這個的任何更新?謝謝! – Itachi

回答

0

不知道什麼根本的問題是,我發現,預警更新字符串值到NSTextView對象時,用剛纔下面的代碼解決它。

只要確保在主線程中處理更新過程。

//鏈接:NSTextStorage limitation on size and frequency of updates

dispatch_block_t block =^{ 
    NSAttributedString *attributeString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n", strLog]]; 

    NSTextStorage* store = [_textViewOutputLog textStorage]; 
    [store beginEditing]; 
    [store appendAttributedString:attributeString]; 
    [store endEditing]; 
    [_textViewOutputLog scrollRangeToVisible:NSMakeRange([[_textViewOutputLog string] length], 0)]; 
}; 

if ([NSThread isMainThread]) { 
    block(); 
} else { 
    dispatch_async(dispatch_get_main_queue(), block); 
}