В CoreData
, при работе с фоном и основным контекстом я сталкиваюсь с проблемой
В контексте уже есть координатор; не может заменить. "
Я выполняю операцию чтения в основном контексте и операцию записи в фоновом контексте, а также для фонового контекста. Я использую фоновую очередь. можешь подсказать что тут я делаю не так
=============================================== =========================
Fatal Exception: NSInternalInconsistencyException
Context already has a coordinator; cannot replace.
@interface CoreDataCustomClass()
@property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong) NSManagedObjectContext *mainContext;
@property (nonatomic, strong) NSManagedObjectContext *backgroundContext;
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end
static CoreDataCustomClass *sharedInstance = nil;
@implementation CoreDataCustomClass
+(CoreDataCustomClass *)sharedInstance
{
@synchronized([CoreDataCustomClass class])
{
if (!sharedInstance)
{
sharedInstance = [[self alloc] init];
}
return sharedInstance;
}
return nil;
}
#pragma mark - Core Data stack
- (NSManagedObjectContext *)mainContext {
if (_mainContext.persistentStoreCoordinator) {
return _mainContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__weak __typeof(self)weakSelf = self;
_mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_mainContext performBlockAndWait:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf->_mainContext setPersistentStoreCoordinator:coordinator];
}];
}
return _mainContext;
}
-(void)saveMainContext {
NSManagedObjectContext *mainManagedObjectContext = self.mainContext;
[mainManagedObjectContext performBlock:^{
NSError *error = nil;
if ([mainManagedObjectContext hasChanges] && ![mainManagedObjectContext save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[mainManagedObjectContext performBlock:^{
NSError *error = nil;
if ([mainManagedObjectContext hasChanges] && ![mainManagedObjectContext save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}];
}];
}
-(NSManagedObjectContext *)backgroundContext{
if (_backgroundContext.persistentStoreCoordinator){
return _backgroundContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if(coordinator != nil){
__weak __typeof(self)weakSelf = self;
_backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_backgroundContext performBlockAndWait:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf->_backgroundContext setPersistentStoreCoordinator:coordinator];
[strongSelf->_backgroundContext setUndoManager:nil];
}];
}
return _backgroundContext;
}
-(void)saveBackgroundContext {
NSManagedObjectContext *backgrounManagedObjectContext = self.backgroundContext;
[backgrounManagedObjectContext performBlock:^{
NSError *error = nil;
if ([backgrounManagedObjectContext hasChanges] && ![backgrounManagedObjectContext save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[backgrounManagedObjectContext performBlock:^{
NSError *error = nil;
if ([backgrounManagedObjectContext hasChanges] && ![backgrounManagedObjectContext save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}];
}];
}
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"abc" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSDictionary *persistentOptions = @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES};
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"abc.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
[[NSFileManager defaultManager] removeItemAtURL: storeURL error: nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration: nil URL: storeURL options: nil error: &error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
if (![[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:[storeURL path] error:&error]) {
NSLog(@"error");
}
return _persistentStoreCoordinator;
}