0
votes

I am currently of unsure of how i can go around this issue. Basically i have a tableview where i can delete data by swiping. Below is the code with affected line.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {
        // Delete the row from the data source

        guard let id = CastService.instance.datsSource[indexPath.row].id else {return}

        CastService.instance.deleteCast(id: id, completion: { (success) in

            if success {
                // Have the below line and that crashes. I believe this is because database has already removed the data
                tableView.deleteRows(at: [indexPath], with: .fade)

                // Added below line to see if reloading data does anything, however nothing happened, data was still there
               // self.tableview.reloadData()
            }
        })
    }
}

Declaration of dataSource

fileprivate var dataSource = CastService.instance.datsSource

numberOfRowsInSection Code

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataSource.count
}

I am currently trying to figure out how i can go around this, so user can row being deleted

Crash Error Below

Interstellar Battalion[62182:2255764] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UITableView.m:2011
2018-01-10 22:13:26.879942+0000 Interstellar Battalion[62182:2255764] *** 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 (1) must be equal to the number of rows contained in that section before the update (1), 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:
(
    0   CoreFoundation                      0x000000010f7ed12b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010e934f41 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010f7f22f2 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x000000010e3d5d69 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
    4   UIKit                               0x0000000110f4f209 -[UITableView _endCellAnimationsWithContext:] + 19416
    5   UIKit                               0x0000000110f6b67a -[UITableView _updateRowsAtIndexPaths:withUpdateAction:rowAnimation:usingPresentationValues:] + 1380
    6   UIKit                               0x0000000110f6b8ba -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 121
    7   Interstellar Battalion              0x000000010ddab8f2 _T022Interstellar_Battalion28MovieCastTableViewControllerC05tableF0ySo07UITableF0C_SC0iF16CellEditingStyleO6commit10Foundation9IndexPathV8forRowAttFySbcfU_ + 258
    8   Interstellar Battalion              0x000000010ddababc _T022Interstellar_Battalion28MovieCastTableViewControllerC05tableF0ySo07UITableF0C_SC0iF16CellEditingStyleO6commit10Foundation9IndexPathV8forRowAttFySbcfU_TA + 140
    9   Interstellar Battalion              0x000000010dda9604 _T022Interstellar_Battalion11CastServiceC06deleteC0ySi2id_ySbc10completiontFy9Alamofire12DataResponseVySSGcfU_ + 612
    10  Interstellar Battalion              0x000000010dda9ae2 _T022Interstellar_Battalion11CastServiceC06deleteC0ySi2id_ySbc10completiontFy9Alamofire12DataResponseVySSGcfU_TA + 66
    11  Alamofire                           0x000000010e135ff5 _T09Alamofire12DataResponseVySSGIxx_ADIxi_TR + 213
    12  Alamofire                           0x000000010e136092 _T09Alamofire12DataResponseVySSGIxx_ADIxi_TRTA + 66
    13  Alamofire                           0x000000010e131809 _T09Alamofire11DataRequestC8responseACXDSo13DispatchQueueCSg5queue_x0D10SerializeryAA0B8ResponseVy16SerializedObjectQzGc17completionHandlertAA0biH8ProtocolRzlFyycfU_yycfU_ + 905
    14  Alamofire                           0x000000010e13a07e _T09Alamofire11DataRequestC8responseACXDSo13DispatchQueueCSg5queue_x0D10SerializeryAA0B8ResponseVy16SerializedObjectQzGc17completionHandlertAA0biH8ProtocolRzlFyycfU_yycfU_TA + 110
    15  Alamofire                           0x000000010e0e87f9 _T0Ix_IyB_TR + 41
    16  libdispatch.dylib                   0x0000000114c122f7 _dispatch_call_block_and_release + 12
    17  libdispatch.dylib                   0x0000000114c1333d _dispatch_client_callout + 8
    18  libdispatch.dylib                   0x0000000114c1e5f9 _dispatch_main_queue_callback_4CF + 628
    19  CoreFoundation                      0x000000010f7afe39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    20  CoreFoundation                      0x000000010f774462 __CFRunLoopRun + 2402
    21  CoreFoundation                      0x000000010f773889 CFRunLoopRunSpecific + 409
    22  GraphicsServices                    0x00000001183469c6 GSEventRunModal + 62
    23  UIKit                               0x0000000110e2b5d6 UIApplicationMain + 159
    24  Interstellar Battalion              0x000000010dda6c37 main + 55
    25  libdyld.dylib                       0x0000000114c8fd81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
1

1 Answers

5
votes

You are receiving this crash because when you add or delete rows in a UITableView you also need to add or delete the same number of items from your datasource.

For example, you might change your function to this:

self.dataSource.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)