Monday 15 June 2015

ios - Magical Record, delete from UITableView "invalid number of rows in section 0..." -



ios - Magical Record, delete from UITableView "invalid number of rows in section 0..." -

i'm using magical record manage coredata. when user delete specific cell in table, is:

set flag , delete row 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 nil."); nsassert(messageid != nil, @"messageid cannot 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 server ids delete, server send ids , if match, delete them coredata:

@try { treenode *root = [treenode treenode]; xmlparser *parser = [[xmlparser alloc] init]; root = [parser parsexmlfromdata:data]; if (root.key && [root.key.lowercasestring isequaltostring:@"messages"]) { (treenode *children in root.children) { if ([children.key.lowercasestring isequaltostring:@"message"]) { (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 { homecoming 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // homecoming self.datasource.count; homecoming [humanresponse allacceptedrecords].count; } + (nsarray *)allacceptedrecords { homecoming [coredataphotorecord mr_findallsortedby:@"message.originaldate" ascending:no withpredicate:[nspredicate predicatewithformat:@"message.delete_message == %@", @no]]; }

edit - 1:

log 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. number of rows contained in existing section after update (12) must equal number of rows contained in section before update (12), plus or minus number of rows inserted or deleted section (0 inserted, 1 deleted) , plus or minus number of rows moved or out of 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 uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (12) must equal number of rows contained in section before update (12), plus or minus number of rows inserted or deleted section (0 inserted, 1 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).' *** first throw phone 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 uncaught exception of type nsexception (lldb)

what doing wrong?

as sebastian wramba suggested, changed way i'ms aving context:

- (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 nil."); nsassert(messageid != nil, @"messageid cannot 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]; }

ios objective-c uitableview core-data magicalrecord

No comments:

Post a Comment