問題:
I am duplicating an existing Objective-C TV Show app to a new Swift version using Xcode 6.1 and am having some issues with CoreData.我正在使用 Xcode 6.1 將現有的 Objective-C 電視節目應用程序複製到新的 Swift 版本,並且在 CoreData 方面遇到了一些問題。
I have created a model of 4 entities, created their NSManagedObject subclass (in Swift), and all files have the proper app targets set (for 'Compile Sources').我創建了一個包含 4 個實體的模型,創建了它們的 NSManagedObject 子類(在 Swift 中),並且所有文件都設置了正確的應用程序目標(用於“編譯源”)。
I am still getting this error whenever I try to insert a new entity:每當我嘗試插入新實體時,我仍然收到此錯誤:
CoreData: warning: Unable to load class named 'Shows' for entity 'Shows'. CoreData:警告:無法爲實體“Shows”加載名爲“Shows”的類。 Class not found, using default NSManagedObject instead.未找到類,使用默認 NSManagedObject 代替。
A few comments:幾點意見:
When saving to Core Data, I use the parent-child context way to allow background threading.保存到 Core Data 時,我使用父子上下文方式來允許後臺線程。 I do this by setting up the ManagedObjectContext using:我通過使用以下方法設置 ManagedObjectContext 來做到這一點:
lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
and by saving data using:並使用以下方法保存數據:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
var context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
context.parentContext = self.managedObjectContext!
...rest of core data saving code here...
})