3

我正在使用下面函数中的一些数据填充 NSComboBox。填充后,我尝试滚动浏览我收到 CATransaction 警告的项目。任何人都可以阐明为什么会发生这种情况以及我可以做些什么来解决它?我发现它可能与在主线程以外的线程上更改组合框的 UI 有关,但在那之后我被卡住了。

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
4

1 回答 1

1

不确定根本问题是什么,我在将字符串值更新为 NSTextView 对象时发现了该警告,并使用以下代码解决了它。

只需确保在主线程中处理更新过程。

// 链接:NSTextStorage 对大小和更新频率的限制

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);
}
于 2015-08-25T10:26:35.397 回答