从核心数据中删除记录不起作用?

时间:2017-10-04 06:22:33

标签: ios objective-c database core-data

我正在使用核心数据,当我从数据库中删除记录时,它无法正常工作。 实体名称:实体 属性:id,日期,标题

- (void) getData {

NSFetchRequest * fetReq = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
NSError * fetchErrorObj;
NSArray *result = [self.ad.managedObjectContext executeFetchRequest:fetReq error:&fetchErrorObj];

NSLog(@"array count is %lu", result.count);

NSMutableArray *idArr = [[NSMutableArray alloc]init];
NSMutableArray *titleArr = [[NSMutableArray alloc]init];
NSMutableArray *dateArr = [[NSMutableArray alloc]init];

for (int i=0; i<result.count; i++) {

    self.storedManagedObj = [result objectAtIndex:i];

    [idArr addObject:[self.storedManagedObj valueForKey:@"id"]];
    [titleArr addObject:[self.storedManagedObj valueForKey:@"title"]];
    [dateArr addObject:[self.storedManagedObj valueForKey:@"date"]];

}
self.idArray = sidArr;
}

删除记录......

- (IBAction)deleteRecord:(UIButton *)sender {

NSNumber *value=[NSNumber numberWithInt:[[self.idArray objectAtIndex:0] intValue]];
NSLog(@"%@", [self.idArray objectAtIndex:0]);
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Entity"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", value];
NSLog(@"predicate :%@", predicate);
[request setPredicate:predicate];
NSLog(@"request :%@", request);
NSError *error = nil;
NSArray *result = [self.ad.managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"result: %@", result);
if (!error && result.count > 0) {
    for(NSManagedObject *managedObject in result){
        NSLog(@"managedObject :%@", managedObject);
        [self.ad.managedObjectContext deleteObject:managedObject];
    }

    //Save context to write to store
    [self.ad.managedObjectContext save:nil];
}


}

我得到这样的结果 谓词:id == 38 request :( entity:Entity; predicate:(id == 38); sortDescriptors:((null)); type:NSManagedObjectResultType;) 结果:( )

1 个答案:

答案 0 :(得分:1)

错误很清楚。它声明您需要为FetchRequest

指定SortDescriptor
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Entity"];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>"
                                                                   ascending:YES];
    request.sortDescriptors = @[sortDescriptor];

OR

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];

阅读:NSFetchRequest without sort descriptors

修改

由于OP不关心以任何特定顺序对结果进行排序,并且问题缺少实体的描述,我可以看到的唯一字段是id更新我在排序描述符字段中使用id的答案

[[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES];

编辑2:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Entity"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", [self.idArray objectAtIndex:0]];

代码中的问题是:

NSNumber *value=[NSNumber numberWithInt:[[self.idArray objectAtIndex:0] intValue]];

OP尝试使用NSNumberself.idArray中的对象创建intValue,这意味着idArray是String而不是NSNumber的数组。这意味着id在核心数据中保存为String,而不是NSNumber。

在谓词中然而OP试图通过NSNumber来与id进行比较。由于id为String且传递的参数为NSNumber,因此比较失败,因此未返回任何对象。