خطأ «This application is modifying the autolayout engine from a background thread» — كيفية إصلاحه؟
مرحبًا بالجميع! أنا مبتدئ في Swift وتطوير تطبيقات macOS. واجهت خطأ غريبًا لا يظهر دائمًا، بل فقط عندما ينفذ التطبيق عملية طويلة. في الـ console تظهر الرسالة:
```
This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
```
لدي مثال بسيط: أقوم بتحميل بيانات من الإنترنت في background thread، وبعد الانتهاء أريد تحديث `NSTextField` على الشاشة. إليك الكود الخاص بي:
```swift
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var statusLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// محاكاة تحميل طويل
DispatchQueue.global().async {
self.loadData()
}
}
func loadData() {
// محاكاة التحميل
Thread.sleep(forTimeInterval: 2.0)
let result = "البيانات