I'm using Magical Record
to manage my CoreData
. When user delete specific cell in the table, what I do is:
我正在使用Magical Record来管理我的CoreData。当用户删除表中的特定单元格时,我所做的是:
Set flag and delete the row from table:
设置标志并从表中删除行:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
MessageTableViewCell *cell = (MessageTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
NSArray *sortedMessageID = [cell.photoDetails.message.message_id componentsSeparatedByString:@"_"];
NSString *messageID = sortedMessageID[1];
NSLog(@"cell.photoDetails.message.message_id: %@", cell.photoDetails.message.message_id);
NSLog(@"sortedMessageID: %@", sortedMessageID);
NSAssert(sortedMessageID[1] != nil, @"sortedMessageID[1] cannot be nil.");
NSAssert(messageID != nil, @"messageID cannot be nil.");
if (cell.photoDetails) {
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
cell.photoDetails.message.delete_message = @YES;
}];
}
// [self.dataSource removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self performSelectorInBackground:@selector(deleteMessagesFromServer:) withObject:messageID];
}
[tableView endUpdates];
}
Update the server which IDs to delete, server send back the IDs and if they match, I delete them from CoreData:
更新服务器要删除的ID,服务器发回ID,如果匹配,我从CoreData中删除它们:
@try {
TreeNode *root = [TreeNode treeNode];
XMLParser *parser = [[XMLParser alloc] init];
root = [parser parseXMLFromData:data];
if (root.key && [root.key.lowercaseString isEqualToString:@"messages"]) {
for (TreeNode *children in root.children) {
if ([children.key.lowercaseString isEqualToString:@"message"]) {
for (TreeNode *children2 in children.children) {
if ([children2.key isEqualToString:@"id"]) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"message.message_id ==[c] %@", children2.leafvalue];
BOOL isDelete = [CoreDataPhotoRecord MR_deleteAllMatchingPredicate:predicate];
[[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfAndWait];
NSLog(@"%s, isDelete: %@", __PRETTY_FUNCTION__, @(isDelete));
}
}
}
}
NSLog(@"allAcceptedRecords.count: %lu", (unsigned long)[HumanResponse allAcceptedRecords].count);
// NSLog(@"allAcceptedRecords.count: %lu, self.dataSource.count: %lu",
// (unsigned long)[HumanResponse allAcceptedRecords].count, (unsigned long)self.dataSource.count);
}
}
@catch (NSException *exception) {
NSLog(@"%s, exception.reason: %@", __PRETTY_FUNCTION__, exception.reason);
}
Additional code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return self.dataSource.count;
return [HumanResponse allAcceptedRecords].count;
}
+ (NSArray *)allAcceptedRecords
{
return [CoreDataPhotoRecord MR_findAllSortedBy:@"message.originalDate"
ascending:NO
withPredicate:[NSPredicate predicateWithFormat:@"message.delete_message == %@", @NO]];
}
Edit - 1:
编辑 - 1:
Log is provided:
提供日志:
2014-10-23 11:12:07.140 Human Response[426:94571] tableView.isEditing: 1
2014-10-23 11:12:07.140 Human Response[426:94571] cell.photoDetails.message.message_id: m_1000075
2014-10-23 11:12:07.141 Human Response[426:94571] sortedMessageID: (
m,
1000075
)
2014-10-23 11:12:07.144 Human Response[426:94756] deleteMessages: userID=3&ids=1000075&mobile=0543111797
2014-10-23 11:12:07.144 Human Response[426:94571] -[MainTableViewController tableView:numberOfRowsInSection:], count: 12
2014-10-23 11:12:07.145 Human Response[426:94571] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-3318.16.14/UITableView.m:1582
2014-10-23 11:12:07.146 Human Response[426:94571] CRASH:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (12) must be equal to the number of rows contained in that section before the update (12), 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).
2014-10-23 11:12:07.158 Human Response[426:94571] Stack Trace:
(
0 CoreFoundation 0x277a2c37 <redacted> + 150
1 libobjc.A.dylib 0x34f4dc8b objc_exception_throw + 38
2 CoreFoundation 0x277a2af5 <redacted> + 0
3 Foundation 0x28472d7f <redacted> + 90
4 UIKit 0x2ae2c7ef <redacted> + 8150
5 Human Response 0x000cd823 -[MainTableViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 1802
6 UIKit 0x2ae445e3 <redacted> + 142
7 UIKit 0x2af1e025 <redacted> + 68
8 UIKit 0x2ac92c2b <redacted> + 70
9 UIKit 0x2ac92bd1 <redacted> + 44
10 UIKit 0x2ac7d863 <redacted> + 582
11 UIKit 0x2ac9263d <redacted> + 588
12 UIKit 0x2ac5742d <redacted> + 10268
13 UIKit 0x2ac8c2f1 <redacted> + 784
14 UIKit 0x2ac8bbcd <redacted> + 524
15 UIKit 0x2ac623dd <redacted> + 196
16 UIKit 0x2aed5c29 <redacted> + 13888
17 UIKit 0x2ac60e39 <redacted> + 1296
18 CoreFoundation 0x27769377 <redacted> + 14
19 CoreFoundation 0x27768787 <redacted> + 218
20 CoreFoundation 0x27766ded <redacted> + 772
21 CoreFoundation 0x276b5211 CFRunLoopRunSpecific + 476
22 CoreFoundation 0x276b5023 CFRunLoopRunInMode + 106
23 GraphicsServices 0x2eaae0a9 GSEventRunModal + 136
24 UIKit 0x2acc11d1 UIApplicationMain + 1440
25 Human Response 0x000bfc41 main + 116
26 libdyld.dylib 0x354cdaaf <redacted> + 2
)
2014-10-23 11:12:07.161 Human Response[426:94571] *** 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 (12) must be equal to the number of rows contained in that section before the update (12), 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).'
*** First throw call stack:
(0x277a2c1f 0x34f4dc8b 0x277a2af5 0x28472d7f 0x2ae2c7ef 0xcd823 0x2ae445e3 0x2af1e025 0x2ac92c2b 0x2ac92bd1 0x2ac7d863 0x2ac9263d 0x2ac5742d 0x2ac8c2f1 0x2ac8bbcd 0x2ac623dd 0x2aed5c29 0x2ac60e39 0x27769377 0x27768787 0x27766ded 0x276b5211 0x276b5023 0x2eaae0a9 0x2acc11d1 0xbfc41 0x354cdaaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
What am I doing wrong?
我究竟做错了什么?
1 个解决方案
#1
0
As Sebastian Wramba
suggested, I changed the way I'ms aving the context:
正如Sebastian Wramba建议的那样,我改变了我在上下文中的方式:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"tableView.isEditing: %@", @(tableView.isEditing));
MessageTableViewCell *cell = (MessageTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
NSArray *sortedMessageID = [cell.photoDetails.message.message_id componentsSeparatedByString:@"_"];
NSString *messageID = sortedMessageID[1];
NSLog(@"cell.photoDetails.message.message_id: %@", cell.photoDetails.message.message_id);
NSLog(@"sortedMessageID: %@", sortedMessageID);
NSAssert(sortedMessageID[1] != nil, @"sortedMessageID[1] cannot be nil.");
NSAssert(messageID != nil, @"messageID cannot be nil.");
if (cell.photoDetails) {
cell.photoDetails.message.delete_message = @YES;
[self saveContext];
// [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
// cell.photoDetails.message.delete_message = @YES;
// }];
}
// [self.dataSource removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self performSelectorInBackground:@selector(deleteMessagesFromServer:) withObject:messageID];
}
[tableView endUpdates];
}
#pragma mark - Magical Records
- (void)saveContext
{
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}
#1
0
As Sebastian Wramba
suggested, I changed the way I'ms aving the context:
正如Sebastian Wramba建议的那样,我改变了我在上下文中的方式:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"tableView.isEditing: %@", @(tableView.isEditing));
MessageTableViewCell *cell = (MessageTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
NSArray *sortedMessageID = [cell.photoDetails.message.message_id componentsSeparatedByString:@"_"];
NSString *messageID = sortedMessageID[1];
NSLog(@"cell.photoDetails.message.message_id: %@", cell.photoDetails.message.message_id);
NSLog(@"sortedMessageID: %@", sortedMessageID);
NSAssert(sortedMessageID[1] != nil, @"sortedMessageID[1] cannot be nil.");
NSAssert(messageID != nil, @"messageID cannot be nil.");
if (cell.photoDetails) {
cell.photoDetails.message.delete_message = @YES;
[self saveContext];
// [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
// cell.photoDetails.message.delete_message = @YES;
// }];
}
// [self.dataSource removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self performSelectorInBackground:@selector(deleteMessagesFromServer:) withObject:messageID];
}
[tableView endUpdates];
}
#pragma mark - Magical Records
- (void)saveContext
{
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}