2

So I have a table view that lists few items and I have two arrays pointing to the same object that the list holds it in. But the models are the same.

Anyways, I have a delete functionality and while deleting, there is a bug. When I delete the last element, I remove it from the array, but don't call the save api. So if I go back (i.e. pop the view controller from the stack), and come back, the get api will fetch the array and show the list again. But if I press delete again on the last element, it shows index out of range error.

I checked using debugger and I put a breakpoint on the array object (in the model) to see when it gets called (according to this answer - https://stackoverflow.com/a/28117078/5316422). But between the delete confirm button and the actual deletion (during the second attempt), the array is not accessed. I check the value of the array and it has 2 elements. But when I click on the second element (array[1]), it shows the index out of range error and when I check the array, there is only one element. So how does the item in the array get deleted and show 'fatal error: Index out of range'?

Any pointers on how to debug in a more advanced way is also welcome. Thanks

Skywalker
  • 1,590
  • 1
  • 18
  • 36
  • 1
    From your description, I think you have two different array pointing to the same set of objects. Maybe you display the table view from one list, and when you click the row, you check for another list. Please show some code, we would know the problem better. – Daniel Tran Nov 07 '17 at 06:47
  • @DanielTran Actually the array is inside a model object. So if I put a breakpoint at the declaration of the array, shouldn't the app pause when accessing the array, doesn't matter which pointer is being used (according to the answer in the link I posted)? – Skywalker Nov 07 '17 at 06:52
  • @DanielTran and the code is too complicated to put here. I will have to put up the entire view controller for you to find which line causes the damage. So I was wondering if anyone could point me in the right direction to debug better.. – Skywalker Nov 07 '17 at 06:53
  • It would really help us if you could provide a more detailed workflow on how you fetch the data from the API(and into a local storage?) and how you provide a datasource for your `UITableView`. Where is the logic for item deletion located? Is it in the cell itself or is it in the `UIViewController` ? Please try to elaborate to provide us with some more data to work with. – Laffen Nov 07 '17 at 07:55
  • @Laffen Thanks for the interest. Sorry I couldn't show more of the code. Anyways I got the issue fixed. We had used a shared instance of an abstract view controller to display the alert to confirm delete. Changed that and used individual controller to call the alert and the issue stopped reproducing. However, I still think something is wrong as the breakpoint I set on the array was not being called before it loses the element on delete. – Skywalker Nov 07 '17 at 08:11
  • @DanielTran Thanks to you too for the interest shown. (Cont. from last comment) When an element changes value, the breakpoint should capture that moment I think (correct me if I am wrong or if I don't know a specific condition where this happens). – Skywalker Nov 07 '17 at 08:12
  • The code for deletion was in the view controller. The arrays were in the view controller. And there are multiple views which details of one array is displayed. Its a highly complex architecture. I didn't make it. Just unlucky enough to have to work on it. (Although I am glad I ran into this bug) – Skywalker Nov 07 '17 at 08:19
  • I'm just curious, how do you delete items fromt the array? Using `mutating` functions on an array doesn't seem to trigger breakpoints, try to set a watchpoint on the array to see when it is accessed. – Laffen Nov 07 '17 at 09:44
  • @Laffen I use a temp array to check the value of a flag and remove it.. and set it to the array object of the instance variable.. and I couldn't seem to set a watchpoint on that particular particular object (of course, I am not very familiar with watchpoints, hence the question).. – Skywalker Nov 07 '17 at 13:16

0 Answers0