Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-03-2011, 05:29 PM   #1 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Question Tableview crash when deleting row?

Hello again,

I am having a crash with deleting a cell from a tableview. I am using a NSMutableArray loaded from NSUserDefaults to load the cells into the tableview.
This is the crash:
Code:
2011-09-03 18:21:06.097 App[10356:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1906/UITableView.m:1046
2011-09-03 18:21:06.100 App[10356:707] *** 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 (3) must be equal to the number of rows contained in that section before the update (3), 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).'
This is how I delete the rows...
Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray objectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
    [[NSUserDefaults standardUserDefaults] setObject:cellArray forKey:@"cellArray"];
}
This is my numberofrows delegate method:
Code:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.cellArray = [NSMutableArray array];
    [self.cellArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"cellArray"]];
    
    if ([self.cellArray count] == 0) {
        [ivstatstableView setHidden:YES];
        [sortbar setEnabled:NO];
    }
    return [cellArray count];
}
Any idea what is wrong here?

Thanks!,
O.Z
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 09-03-2011, 06:03 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Why in the world are you using user defaults for this?

At any rate, you aren't removing anything from your array. The error message really is quite clear on this. It's one of the better ones.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-03-2011, 08:16 PM   #3 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Why in the world are you using user defaults for this?

At any rate, you aren't removing anything from your array. The error message really is quite clear on this. It's one of the better ones.
I am using NSUserDefaults because my dataSource comes from another view controller.
I do realize that I am actually not deleting something from my nsmutablearray, I must have confused it for the 2nd line where I am deleting the row and actually nothing in the array.
And lastly, I realize and that end of the deletion code, I should do [mytableview reloadData];
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 09-03-2011, 08:44 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I'm pretty sure there is a better option than user defaults.

And if you do the deletion correctly, reloading is not necessary.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-03-2011, 11:50 PM   #5 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

I know your a promoter for properties :P, but I don't think that will work in this case, since I need this dataSource to be in memory when the app closes, opens, (all the time). So I don't think that will work.

Also I am now trying this code but with no luck as I am getting the same crash:
Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray objectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
    [self.cellArray removeObjectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults] setObject:cellArray forKey:@"cellArray"];
}
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 09-04-2011, 02:24 AM   #6 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Quote:
Originally Posted by Objective Zero View Post
I am using NSUserDefaults because my dataSource comes from another view controller.
NSUserDefault is not a reasonable way to share something between viewControllers.

I mean, if your purpose is to permanent save and retrieve something, it is a good way to use it, but for sure not just for pass variables between classes....


However the problem can be in a missing synchronize
NSUserDefaults Class Reference - synchronize
__________________

Last edited by dany_dev; 09-04-2011 at 02:28 AM.
dany_dev is offline   Reply With Quote
Old 09-04-2011, 07:40 AM   #7 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Generally, the datasource must be updated before the tableView, and I guess for the first line in that method you meant to write removeObjectAtIndex rather than objectAtIndex. Apart from that the code should be fine, just remove the other instance of your new first line. Also, if you are storing the array in NSUserDefaults and then setting the cellArray property to the NSUserDefaults instance, you should really be reloading the tables datasource afterwards?

Quote:
Originally Posted by Objective Zero View Post
I know your a promoter for properties :P, but I don't think that will work in this case, since I need this dataSource to be in memory when the app closes, opens, (all the time). So I don't think that will work.

Also I am now trying this code but with no luck as I am getting the same crash:
Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray objectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
    [self.cellArray removeObjectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults] setObject:cellArray forKey:@"cellArray"];
}
iSDK is offline   Reply With Quote
Old 09-04-2011, 11:30 PM   #8 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Ok so let's put aside that I shouldn't be using NSUserDefaults for now. I tried both dany_dev's suggestion and iSDK's suggestion but neither seemed to rectify the issue, it is the same crash as from before.
This is now the code I am using:
Code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
    [[NSUserDefaults standardUserDefaults] setObject:cellArray forKey:@"cellArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
This leaves me with 2 questions:
1. Should I update NSUserDefaults before I do the line [tableview deleteRowAtIndexPaths.... ?
2. Also are you recommending that I should use properties to share the data? And if so, what would be the logic behind that? Right now I use user defaults because it is easier logically for me. Are there disadvantages using that to properties?

Thanks!

P.S - I typed this all from my iPhone
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8

Last edited by Objective Zero; 09-04-2011 at 11:37 PM.
Objective Zero is offline   Reply With Quote
Old 09-04-2011, 11:48 PM   #9 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by iSDK View Post
Generally, the datasource must be updated before the tableView
+

A careful examination of what you are actually doing

=

the answer
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-05-2011, 12:54 AM   #10 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

Stupid ol' me. That fixed it.

Anyway do you have any ideas for what I said for #2 in my last post?

Right now it works fine no crashes or anything and actually kind of fast, so what makes a difference or not whether I use properties or not?
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Old 09-05-2011, 01:13 PM   #11 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

When you share objects between other objects (in this case view controllers), they remain in memory so there's no extra overhead. What you are doing is committing them to permanent storage, then reading them from there into memory again which is quite an expensive process. What exactly are you doing, what is the scenario, and why do you feel that sharing through properties is not a goo option?
baja_yu is offline   Reply With Quote
Old 09-05-2011, 01:17 PM   #12 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Not to mention, you already ARE using properties. You're just using properties AND using user defaults. How could that possibly be faster/easier than using properties alone?

Oh yeah, and if you would have a multi-section table, your technique as-is falls apart really quickly.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-05-2011, 01:30 PM   #13 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

@baja_yu I'll explain how I am doing everything currently. So I have a game here. When they lose the game, a game over view is shown which generates a NSDictionary from information about the user that is playing. That NSDictionary is added to a NSMutableArray and then stored in NSUserDefaults. Then once I go back to the Main Menu and then go to my Statistics view, I load that NSMutableArray from NSUserDefaults in my numberOfRowsInSection delegate method. Then when I go to delete a cell, I delete the object from my array and then save it to NSUserDefaults.
So technically, AFAIK, if I do it the way I do it now, if the user gets the game over view and then does not go to the Statistics view before the app is terminated, wouldn't that just pretty much delete the info for that game instance?
That is why I am using NSUserDefaults, because it is the easiest way (I think), with my scenario here.

@BrianSlick I am using only a one section tableview so that doesn't matter.
__________________
Questions?

Check out my OCR app!
http://itunes.apple.com/app/ocr-pro/id486512712?mt=8
Objective Zero is offline   Reply With Quote
Reply

Bookmarks

Tags
delete

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 382
5 members and 377 guests
JackReidy, jeroenkeij, Sami Gh, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0