Strange issue while using NSMutableArray as a queue.
Given a singleton object SO, a ViewController object VC, and a NSMutableArray MA. MA is being used as a queue for an ID that is received by SO, and passed to VC via putting the ID in MA and sending a notification that VC handles. There is a problem with MA losing what it holds.
Steps relating to issue:
- Seque is initiated to load a view.
- SO requests an ID from a server.
- VC’s viewDidLoad completes
- SO block function receives an ID and puts it in MA, and sends a notification to VC.
- VC receives notification and retrieves ID from MA
The first time the view loads there is no problem. However if I go out of the view and back into it repeating the steps above the size of MA in step 4 is 1, and the size of MA in step 5 is 0. I.e. MA becomes empty!
I’m perplexed as to how MA mysteriously becomes empty between the time the notification is sent (step 4), and when the notification handler is called (step 5). I’ve checked that nothing outside of the VC notification handler is clearing the array.
A possible clue is that there is a method called (message sent in obj C parlance) from viewDidLoad that if commented out the issue described doesn’t happen. Also if I put the following line after the method in viewDidLoad (instead of commenting it out) then the issue stops occurring:
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow: 0.5]];
I’ve been scratching my head on this one. My latest thought is that there is something about NSMutableArray that I don’t understand. I thought MA was storing strong references to IDs placed in it, but if it weren’t then maybe there’d be some strangeness.
Anyone have ideas?