我有UICollectionViewController
<NSFetchedResultsControllerDelegate>
和CoreData
单身经理。因此,当我尝试更新我的对象的一些标题时,我看到错误。
CollectionViewController.m
@interface CollectionViewController ()<NSFetchedResultsControllerDelegate>
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext* managedObjectContext;
@implementation CollectionViewController
- (NSManagedObjectContext*) managedObjectContext {
if (!_managedObjectContext) {
_managedObjectContext = [[CoreDataManager sharedManager] managedObjectContext];
}
return _managedObjectContext;
}
-(void)pushTheButton: (UIButton*) sender{
NSIndexPath *indexPath = [self.collectionView indexPathForCell:(PKMapObjectCVCell *)sender.superview.superview];
SomeObjectClass* object = [self.fetchedResultsController objectAtIndexPath:indexPath];
[[CoreDataManager sharedManager] changeValueForObject: object.objectID];
}
CoreDataManager.m
@implementation CoreDataManager
@synthesize mainPrivateManagedObjectContext = _mainPrivateManagedObjectContext;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
+(CoreDataManager*) sharedManager{
static CoreDataManager* manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[CoreDataManager alloc] init];
});
return manager;
}
- (void) changeValueForObject:(NSManagedObjectID*) myobjectID{
NSManagedObjectContext * bgcontext = [self getContextForBGTask];
SomeObjectClass * editableObject = [bgcontext objectWithID:myObjectID];
editableObject.title = @"New Title";
// [bgcontext updatedObjects];
[self saveContextForBGTask:bgcontext];
}
- (void)saveContextForBGTask:(NSManagedObjectContext *)bgTaskContext {
if (bgTaskContext.hasChanges) {
[bgTaskContext performBlockAndWait:^{ //error on this line:
[错误]错误:严重的应用程序错误。在此期间发生了异常 核心数据变更处理。这通常是观察者中的错误 NSManagedObjectContextObjectsDidChangeNotification。左手 ALL或ANY运算符的一侧必须是NSArray或NSSet。 with userInfo(null)CoreData:error:严重的应用程序错误。 在Core Data更改处理期间捕获到异常。这是 通常是观察者中的一个错误 NSManagedObjectContextObjectsDidChangeNotification。左手 ALL或ANY运算符的一侧必须是NSArray或NSSet。 with userInfo(null)2018-03-21 17:07:42.644646 + 0400 AppName [17860:3506816] ***由于未被捕获而终止应用程序 例外&#39; NSInvalidArgumentException&#39;,原因:&#39;左侧 对于ALL或ANY运算符,必须是NSArray或NSSet。
NSError *error = nil;
[bgTaskContext save:&error];
}];
[self saveDefaultContext:YES]; // Save default context
}
}
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
_mainPrivateManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_mainPrivateManagedObjectContext setPersistentStoreCoordinator:coordinator];
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setParentContext:_mainPrivateManagedObjectContext];
return _managedObjectContext;
}
- (NSManagedObjectContext *)getContextForBGTask {
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[context setParentContext:_managedObjectContext];
return context;
}
那么,我做错了什么?
代码删除对象,工作正常:
- (void) switchObjectLike:(NSManagedObjectID*) mapObjectID{
NSManagedObjectContext * bgcontext = [self getContextForBGTask];
PKMapObj * mapObject = [bgcontext objectWithID:mapObjectID];
[bgcontext deleteObject:mapObject];
[self saveContextForBGTask:bgcontext];
}
答案 0 :(得分:0)
因此,问题在于NSFetchedResultsController
中的谓词。更新某些内容后,NSFetchedResultsController
准备重新加载集合视图,并在调用此方法之前:
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
它崩溃了,但从未显示出错误。谢谢大家!