0

I have one ViewController that takes objects from CoreData and build with them a UITableView. When a user press a row, I get the reportage object and pass it to the next view controller using:

Reportage *reportage = [self.reportages objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
ReportageTeaserPanelViewController *rightController = (ReportageTeaserPanelViewController*)self.menuContainerViewController.rightMenuViewController;
rightController.reportage = reportage;
[self.menuContainerViewController toggleRightSideMenuCompletion:nil];

The variable reportage is declared as strong in the controller ReportageTeaserPanelViewController.

The problem is the following. If I have to reload asynchronously the objects in the array of the parent view controller because of there is an update in my web service, the variable reportage gets nil in the controller ReportageTeaserPanelViewController. I thought that using a strong reference, the controller ReportageTeaserPanelViewController should be keep the "old" reportage object although it disappears from the array.

Is the any explanation for this behaviour?

Thanks

1
  • 1
    If reportage is nil after executing the first statement above it's because self.reportages is nil. It's also possible, of course, that you never actually set rightController.reportage because rightController is nil. Commented Jan 9, 2015 at 17:45

2 Answers 2

1

From what I can gather, you start with self.reportages not being nil, but since it's reloaded asynchronously after that initial load, when the user selects the table during an asynchronous reload, you're in danger of self.reportages being nil or potentially incomplete.

To prevent initializing your local reportage variable with a nil value, I'd recommend NOT directly reloading your self.reportages array in the asynchronously block in your parent view controller, but instead creating a local array within that block and then, once that array contains the full and proper contents, setting self.reportages to contain the contents of that local array. That way as long as your web service returns the proper "reportage," self.reportages will never equal nil.

Sign up to request clarification or add additional context in comments.

Comments

0

I thought that using a strong reference, the controller ReportageTeaserPanelViewController should be keep the "old" reportage object.

Strong references are irrevelant. Nothing in this code is "kept" - these are all local, automatic variables, so they go out of existence as soon as the code runs. Only persistent references can make an object persist. An instance variable / property is an example of a persistent reference; it lives as long as the instance itself, unless of course you change its value. So the place to look is you instance variable, reportages.

You are saying:

Reportage *reportage = [self.reportages objectAtIndex:indexPath.row];

If there is no object at that index, there is no object at that index. It has nothing do with strong references. You need to think about where reportages is supposed to get its value.

It sounds from your use of the word "asynchronously" as if you might be setting reportages on a different thread from, and possibly actually later than, this code is running. That would be the issue. You need to get your threading and order of events sorted out.

6 Comments

But the ReportageTeaserPanelViewController instance should still have a reference to reportage even if the array is emptied.
@AaronBrager But his code overwrites that reference with nil.
The object is in the index when I get it to launch the ReportageTeaserPanelViewController. But next, asynchronously, I reload the array in the parent which causes the object in ReportageTeaserPanelViewController be nil.
Yes, and that is what I am suggesting you need to get sorted out.
If there is no object at that index it's because the array is not that big, and he should get an array index out of bounds exception.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.