0

I am working with core data, when I deleting record from DB it's not working. Entity name : Entity Attributes : id, date, title

- (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;
}

To delete record...

- (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];
}


}

I am getting result like this predicate :id == 38 request : (entity: Entity; predicate: (id == 38); sortDescriptors: ((null)); type: NSManagedObjectResultType; ) result :( )

Naresh
  • 16,698
  • 6
  • 112
  • 113

1 Answers1

1

The error is pretty clear. It states that you need to specify SortDescriptor to your FetchRequest

    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];

Read : NSFetchRequest without sort descriptors

EDIT:

As OP isn't concerned about sorting the result in any specific order and the question lacks the description of entity and the only field that I can see in question is id updating my answer to use id in sort descriptor field

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

EDIT 2:

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

Issue in the code was :

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

OP was trying to create a NSNumber from an object in self.idArray using intValue which means idArray is a array of String and not NSNumber. That means id is saved as String in core data and not as NSNumber.

In predicate however OP was trying to pass NSNumber to compare with id. Since id is String and passed argument is NSNumber comparison was failing hence was not returning any objects.

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78