ios - 当我使用 CoreData 时,出现错误 : [NSAsynchronousFetchResult count]: unrecognized selector sent to instance

标签 ios objective-c core-data

我的代码如下:

- (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 Animation[3807:1835973] -[NSAsynchronousFetchResult count]: unrecognized selector sent to instance 0x1c8087670 2017-11-09 17:50:34.495239+0800 Animation[3807:1835973] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSAsynchronousFetchResult count]: unrecognized selector sent to instance 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

关于ios - 当我使用 CoreData 时,出现错误 : [NSAsynchronousFetchResult count]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47198962/

相关文章:

iphone - 如何检测我的 iOS 应用程序是否有更新版本?

ios - 调用 NSLayoutConstraint.activateConstraints 会使约束无效吗?

ios - 在NSMutableArray中查找多个值

ios - 调用 viewWillTransitionToSize : manually

objective-c - Cocoa 在多个窗口中验证菜单项

ios - 核心数据: Inherited entities

iphone - 存储关系时核心数据_NSFaultingMutableSet 中的存储问题

swift - 获取单个 Transformable 属性的请求 (Swift)

iphone - Objective-C for循环问题

ios - 为什么 CILinearGradient 会导致非常非线性的梯度?