我已啓用com.apple.CoreData.ConcurrencyDebug 1
檢查Core Data
併發錯誤。我有一個Swift
類下面的代碼片段:非法訪問主線程中的託管對象上下文,爲什麼?
lazy var context: NSManagedObjectContext! = {
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
return appDelegate.managedObjectContext!
}()
func getAllEntitiesToRootContext() -> [MyEntity]? {
let fetchRequest = NSFetchRequest(entityName:"MyEntity")
do {
let fetchedResults = try context.executeFetchRequest(fetchRequest) as! [MyEntity]
if fetchedResults.count > 0 {
return fetchedResults
} else {
return nil
}
} catch let error as NSError {
print("could not fetch \(error), \(error.userInfo)")
return nil
}
}
如果我理解正確的話,我從AppDelegate
獲取上下文關聯到主線程,對不對?
不過,從另一個Objective-C
類我有,我做的:
self.myEntitiesArray = [mySwiftClass getAllEntitiesToRootContext];
,我得到這個錯誤日誌:
CoreData: error: The current thread is not the recognized owner of this NSManagedObjectContext(0x1a25f8a0). Illegal access during executeFetchRequest:error:
我不明白爲什麼......我應該有這樣的背景相關的主線程,我打電話getAllEntitiesToRootContext
從主線程...
請我需要幫助。在此先感謝
編輯:這些都是AppDelegate
有關Core Data
方法:
- (NSManagedObjectContext *)managedObjectContext
{
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
// Create the coordinator and store
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectModel *)managedObjectModel
{
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyApp" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
編輯2:我使用Xcode 7
和測試iOS 9
設備。
編輯3:如果我禁用com.apple.CoreData.ConcurrencyDebug 1
,我從getAllEntitiesToRootContext()
對象......我真的不明白什麼,爲什麼會出現這種情況?
編輯4:我做了一些測試。如果我這樣做從01類:
- (void)getEntities
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = appDelegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
NSArray *entities = [mainContext executeFetchRequest:fetchRequest error:nil];
for (NSManagedObject *item in entities) {
NSLog(@"Name: %@", ((MyEntity *)item).name);
}
}
我打電話executeFetchRequest
和實體的名稱在日誌控制檯中顯示時,有時會沒有錯誤。其他時候,我也得到類似的核心數據錯誤,我上面貼了一個,我也得這樣做的時候,因爲我是這樣做的:
- (NSArray *)getEntities
{
MyEntityDao *myEntityDao = [[MyEntityDao alloc] init];
self.myEntities = [[myEntityDao getAllEntitiesToRootContext] mutableCopy];
return [[NSArray arrayWithArray:self.myEntities] copy];
}
其中MyEntityDao
是Swift
類,定義lazy var context
和getAllEntitiesToRootContext()
,我得到的核心數據錯誤我也張貼在上面...爲什麼?這兩個代碼片段是否相同?爲什麼我有時會說主線程不是我從AppDelegate
檢索到的MOC的所有者?
我真的需要這方面的幫助...
如何初始化'appDelegate.managedObjectContext'? – Leo
@Leo我從'AppDelegate'默認提供的方法得到它# – AppsDev
@Leo我編輯了我的問題 – AppsDev