1

i am unable to delete a specific data from my plist file. I googled so many answers in stackoverflow, but i am unable to solve my problem.

Here is my code:

In ViewDidLoad i retrieve data from plist file.

- (void)viewDidLoad
{
   [super viewDidLoad];

   NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsPath = [paths objectAtIndex:0];
   NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];

   if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
   {
       plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"];
   } 

   NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

   self.nameArr = [dict objectForKey:@"Name"]; // Here i got all my name
   self.countryArr = [dict objectForKey:@"Country"]; // here i got all my country
}  

My deleted code :

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];

    NSString *key = [self.nameArr objectAtIndex:indexPath.row];
    [dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];


    [dictionary writeToFile:plistPath atomically:YES];
    self.nameArr = [dictionary allKeys];

    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
 }

When i build this App i got this error :

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Please anybody suggest me, what i have to do ?

Thanks in Advanced. :)

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51

2 Answers2

0

Your root cause lies in these two lines i guess. Not damn sure

self.nameArr = [dict objectForKey:@"Name"]; // Here i got all my name  

self.nameArr = [dictionary allKeys]; //I can see your dictionary contains only two keys, Name and Country 

I will expalin little bit. In first you are getting your names using the key "Name". Later you are getting the same name array like self.nameArr = [dictionary allKeys];. You got the difference? in 2nd case your array contain the keys [Name, Country]. I just explained the code which is causing error.

Then the code you have written for deletion also not proper. You have get the name and country array again like as you did initially. Then delete from there, write it back. Reading and writing file each time is not good. It will affect the app performance.

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • Ya Anil, i follow your code. you write viewerKeys = [dictionary allKeys]; but i can't understand "viewerKeys". So i take self.nameArr = [dictionary allKeys];. can u explain what is this viewerKeys otherwise u r right. u got my point – Soumya Ranjan May 19 '14 at 12:10
  • I dont know which my code you are refferring, it could be written for some other question and it may work only in that case – Anil Varghese May 19 '14 at 12:15
  • http://stackoverflow.com/questions/14891351/ios-how-can-i-delete-an-array-from-a-plist-using-commiteditingstyle – Soumya Ranjan May 19 '14 at 12:16
0

Like Joe Blow, I think the problem is in how you're managing your table view.

Try replacing [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic] with [tableView reloadData] and see what happens.

The reason the file is not being updated is probably because the exception is thrown before the file is written.

Now onto your problem, check your implementation of numberOfRowsInSection:. That's probably where the bug is.

Also, check the return of [dictionary writeToFile:plistPath atomically:YES]. You shouldn't assume the file is written.

if ([dictionary writeToFile:plistPath atomically:YES]) {
    self.nameArr = [dictionary allKeys];
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
} else {
    NSLog(@"Failed saving %@", plistPath);
}
rtiago42
  • 572
  • 3
  • 9