开发者

core data delete not work

I use the following code to populate a UITableView

self.navigationItem.leftBarButtonItem = self.editButtonItem;

test_coredataAppDelegate *appDelegate = (test_coredataAppDelegate *)[[UIApplication sharedApplication] d开发者_Go百科elegate];
self._context = appDelegate.managedObjectContext;


NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"patients" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSError *error;
self.PatientsList = [_context executeFetchRequest:fetchRequest error:&error];
self.title = @"patients"; 
[fetchRequest release];

where PatientsList is NSArray

when I want to delete record I use

[self._context  deleteObject:[_PatientsList objectAtIndex:indexPath.row]];

    NSError *error; 

    if (![_context save:&error]) {
        // Handle error
        NSLog(@"Unresolved error series %@, %@", error, [error userInfo]);

    }

    [PatientsList removeObjectAtIndex:indexPath.row] ; 


    [self.tableView reloadData];

but this doesn't have any effect.

How can I solve this issue and delete successfully, especially when my model contain relations?


Here you delete it from db but not from array so you need to delete from array or call database for again load the arry with new db details.

so for first approach delete it from array make your array mutable and use removeObjectAtIndex method for deleting the object

or in second approach you need to fetch data again from core data.

and then reload table by using

[table reloadData];


Could you post a little more information about the results you're getting from fetch requests before and after you call deleteObject:? You could also check the userInfo property of the NSError object that you pass to some Core Data functions as that often gives very help information in Core Data. Also remember that your modifications aren't "set in stone" until the data model is saved using the save: method of your NSManagedObjectContext instance.

The way Core Data handles the relationships of a deleted object is defined in your Core Data schema (.xcdata file), which you can edit in Xcode. You can set the delete action of relationships to "Deny", "Nullify", "Cascade" or "No Action" here. See the 'Relationship Delete Rules' section of the documentation found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜