在对 CoreData 写增删改查方法之后编译工程 run 的时候抛出了 unrecognized selector sent to instance 的异常;
全局断点停留的位置并非异常的出处,起初很懵,不过既然是因为操作 CoreData 后才出现的异常,无疑肯定问题是出在了这里,经过前后的排查 code 逻辑,最终发现了问题的所在,好大意所导致!
具体详见 code:
一、异常 code
#pragma mark - ************************************ Insert
+ (void)insertObjForEntityWithDataSource:(NSDictionary *)dataSource ResultState:(void (^)(BOOL, NSError * _Nonnull))result {
CoreDataManager *dataManager = [CoreDataManager sharedCoreDataManager];
// Query
NSString *key = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"key"]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:EntityName
inManagedObjectContext:dataManager.managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"key=%@", key];
[request setPredicate:predicate];
NSError *error;
NSArray *resultData = [dataManager.managedObjectContext executeFetchRequest:request
error:&error];
if (kArrayIsEmpty(resultData)) {// Insert new obj to entity
NSLog(@"CoreData --- Insert new obj entity --- %@", EntityName);
User *userEntity = [NSEntityDescription insertNewObjectForEntityForName:EntityName
inManagedObjectContext:dataManager.managedObjectContext];
userEntity.imei = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"imei"]];
userEntity.userType = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"userType"]];
userEntity.project = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"project"]];
} else {// Update current entity operation
NSLog(@"CoreData --- Update current entity operation --- %@", EntityName);
for (User *userEntity in dataSource) {
if ([userEntity.key isEqualToString:key]) {
userEntity.imei = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"imei"]];
userEntity.userType = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"userType"]];
userEntity.project = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"project"]];
}
}
}
if (error) {
NSLog(@"****** CoreData Error - [%@] insert obj for entity ******\n%@", EntityName, error);
}
// Save processing result
BOOL saveState = [dataManager.managedObjectContext save:&error];
// Callback result state
if (result) {
result(saveState, error);
}
}
二、排除异常之后的 code
#pragma mark - ************************************ Insert
+ (void)insertObjForEntityWithDataSource:(NSDictionary *)dataSource ResultState:(void (^)(BOOL, NSError * _Nonnull))result {
CoreDataManager *dataManager = [CoreDataManager sharedCoreDataManager];
// Query
NSString *key = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"key"]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:EntityName
inManagedObjectContext:dataManager.managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"key=%@", key];
[request setPredicate:predicate];
NSError *error;
NSArray *resultData = [dataManager.managedObjectContext executeFetchRequest:request
error:&error];
if (kArrayIsEmpty(resultData)) {// Insert new obj to entity
NSLog(@"CoreData --- Insert new obj entity --- %@", EntityName);
User *userEntity = [NSEntityDescription insertNewObjectForEntityForName:EntityName
inManagedObjectContext:dataManager.managedObjectContext];
userEntity.imei = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"imei"]];
userEntity.userType = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"userType"]];
userEntity.project = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"project"]];
} else {// Update current entity operation
NSLog(@"CoreData --- Update current entity operation --- %@", EntityName);
for (User *userEntity in resultData) {
if ([userEntity.key isEqualToString:key]) {
userEntity.imei = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"imei"]];
userEntity.userType = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"userType"]];
userEntity.project = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"project"]];
}
}
}
if (error) {
NSLog(@"****** CoreData Error - [%@] insert obj for entity ******\n%@", EntityName, error);
}
// Save processing result
BOOL saveState = [dataManager.managedObjectContext save:&error];
// Callback result state
if (result) {
result(saveState, error);
}
}
通过如上 code 的对比,心细的大家应该发现了问题的所在,事后排查完问题的所在,发现自己当时好!@#¥%,因为 for in 遍历数据的时候一个数据源的错误导致了这个异常问题 。。。
// 如下遍历的数据源应该是数据库查询结果后的 resultData,而非方法传参的数据库 。。。 大意啊大意!
for (User *userEntity in resultData) {
if ([userEntity.key isEqualToString:key]) {
userEntity.imei = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"imei"]];
userEntity.userType = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"userType"]];
userEntity.project = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"project"]];
}
}
不过异常虽小,但汤过坑也算给往后长个记性,更加的细心吧!
以上便是此次分享的内容,希望内容能对大家实际有所帮助,有什么不足之处欢迎指点共同进步!
网友评论