0

I have an NSMutableArray with 14 indexes acting as a global NSArray, lets call it 'A'. In each of these indexes I have a sub-array (therefore I have 14 sub-arrays within 'A'). These arrays then form the data for my UITableViews.

If I check with the server and download a new sub-array (on a background thread) lets call it 'B' and want to replace one of the arrays in 'A' (on the main thread), is it safe to be reading from one index within 'A' whilst concurrently rewriting a different array at a different index (I emphasise it will never be the same index)? Will it cause any memory issues?

My knowledge of how memory allocation and pointers work is limited and I can't seem to find information on this.

2
  • Are you using ARC? You also mentioned "normal thread" did you mean main thread? Commented May 29, 2013 at 22:32
  • Yes and that's the thread! Commented May 29, 2013 at 22:39

1 Answer 1

2

Yes you will be fine as long as the threads don't try accessing the same sub-array. If the sub-arrays are themselves NSMutableArrays, you can merely rewrite the data within them, or remove and add a new array to take its place.

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

5 Comments

Thanks a lot for your quick response!
Is it because they reference different points in the memory and NSArray 'A' has essentially 14 pointers to 14 different regions in memory?
Yes, you got it. And by removing an old array from 'A' and inserting a new one in it you are merely redirecting a pointer from one memory location to the location of the newly created array.
Woo I think I'm getting this!
You do have to be careful about adding/removing stuff in the "mother" array -- that should always be synchronized. But updating sub-arrays is fine so long as only one thread works on a given array.

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.