我的代码如下:
- (void)didClickSave {
if([_addView.name.text isEqualToString:@""]||[_addView.time.text isEqualToString:@""]||[_addView.number.text isEqualToString:@""]||[_addView.country.text isEqualToString:@""]||[_addView.intro.text isEqualToString:@""]) {
UIAlertController *omitAlert = [UIAlertController alertControllerWithTitle:@"message omit" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[omitAlert addAction:okAction];
[self presentViewController:omitAlert animated:YES completion:nil];
}
else {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
Animation *animation = [NSEntityDescription insertNewObjectForEntityForName:@"Animation" inManagedObjectContext:appDelegate.persistentContainer.viewContext];
animation.name = _addView.name.text;
animation.time = _addView.time.text;
animation.number = _addView.number.text;
animation.country = _addView.country.text;
animation.intro = _addView.intro.text;
NSData *imageData = UIImagePNGRepresentation(_addView.imageView.image);
animation.image = imageData;
animation.like = _addView.like.on;
[appDelegate saveContext];
[self.navigationController popViewControllerAnimated:YES];
}
}
错误:
2017-11-09 17:50:34.492561 + 0800动画[3807:1835973]
-[NSAsynchronousFetchResult计数]:无法识别的选择器发送到实例0x1c8087670 2017-11-09 17:50:34.495239 + 0800
动画[3807:1835973] ***由于未捕获的异常而终止应用程序
“NSInvalidArgumentException”,原因:“-[NSAsynchronousFetchResult
计数]:无法识别的选择器发送到实例0x1c8087670'
调用了fetchRequestWithEntityName:的代码如下:
- (NSArray *)animationArr {
if(_animationArr == nil) {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Animation"];
NSError *error = nil;
_animationArr = [appDelegate.persistentContainer.viewContext executeRequest:request error:&error];
}
return _animationArr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.animationArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"cellID";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
self.animation = [self.animationArr objectAtIndex:indexPath.row];
cell.cellName.text = self.animation.name;
return cell;
}
我已经检查了很多次,但是我找不到错误。
提前致谢。
最佳答案
对于Fatch数据,您的代码需要以下方法并传递:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Animation"];
NSAsynchronousFetchRequest *asynchronousFetchRequest = [[NSAsynchronousFetchRequest alloc] initWithFetchRequest:fetchRequest completionBlock:^(NSAsynchronousFetchResult *result) {
dispatch_async(dispatch_get_main_queue(), ^{
// Process Asynchronous Fetch Result
[weakSelf processAsynchronousFetchResult:result];
});
}];
用于显示:- (void)processAsynchronousFetchResult:(NSAsynchronousFetchResult *)asynchronousFetchResult {
if (asynchronousFetchResult.finalResult) {
// Update Items
[self yourarray:asynchronousFetchResult.finalResult];
// Reload Table View
[self.tableView reloadData];
}
}
有关更多详细信息,请参见参考链接:https://code.tutsplus.com/tutorials/ios-8-core-data-and-asynchronous-fetching--cms-22241