【问题标题】:When I use CoreData, I got an error : [NSAsynchronousFetchResult count]: unrecognized selector sent to instance当我使用 CoreData 时,出现错误:[NSAsynchronousFetchResult count]: unrecognized selector sent to instance
【发布时间】:2017-11-09 10:05:30
【问题描述】:

我的代码如下:

- (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 count]: 无法识别的选择器发送到实例 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;
}

我检查了很多次,但我找不到错误。 提前致谢。

【问题讨论】:

  • 该错误似乎与该代码无关。
  • 是的...我找不到任何与 NSAsynchronousFetchResult 相关的代码。和 FetchRequest 有关系吗?
  • @LeeG4ng 顾名思义,我猜NSAsynchronousFetchResult 只是内部iOS SDK 类的名称。你在哪里取的?
  • @Larme 我已经使用 etchRequestWithEntityName: 方法添加了代码。但是当我不调用这个方法时,我也得到了错误。
  • 您正在使用 NSAsynchronousFetchResult 的对象而不是 NSArray 或 NSMutablearray 来获取您的应用程序在 animationArr.count 处崩溃的项目计数检查 animationArr 数据类型是否正确,是否为数组

标签: ios objective-c core-data


【解决方案1】:

对于您的代码需要遵循方法和传递的 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

【讨论】:

  • 我解决了刚才的问题,把executeRequest改成executeFetchRequest。还是谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 2020-01-19
相关资源
最近更新 更多