私のアプリは1つだけを使用し、コアデータのmanagedObjectContext
変更をiCloudと同期するように設定されておりNSFetchedResultsController
、UITableView
. 表のセルを編集 (並べ替え) しようとすると、問題が発生します。
次のシナリオを想像してください。
[tableView setEditing:YES animated:YES];
行の上で指を押したままにして、tableView
(並べ替え)の周りを動かし始めます。その間、Mac アプリで行を削除し、変更を保存します。
iOS アプリは新しい変更を認識し、fetchedResultsController
呼び出しを試みて...編集中に[tableView beginUpdates]
行を削除します。tableView
この方法で動作するかどうかはわかりませんが、クラッシュします。
これをどのように解決しようとしますか?
ログ:
***[3758:707] asynchronously added persistent store!
***[3758:707] numberOfRowsInSection:3
***[3758:707] numberOfRowsInSection:2
***[3758:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1912.3/UITableView.m:1046
***[3758:707] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. 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). with userInfo (null)
***[3758:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 2 beyond bounds [0 .. 0]'
最初の 2 つのログは - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section メソッドにあり、私が返しているものを記録します。1回目は3、2回目は2だった!
編集:そして今考えています...それはこのように機能するべきですか、それとも別の方法を見つけなければなりませんか?セルをドラッグして [tableView moveRowAtIndexPath:toIndexPath:]; を呼び出すとどうなるか ?