0

I am working on an application for the iPhone (iOS 5). What I have to do is create a map by using binary data that I reveive from a server. If the server has bytes available, I read them into a buffer: uint8_t[1024]. Then I parse through this data and create objects (e.g. a path that contains points with longitude and latitude) from it, but those objects are often larger than my buffer. On the simulator this is not a huge problem, because I have enough memory to store them into mutable arrays.

But how do I have to handle this to make my application safe for a device? What array size should I use for iOS devices?

I hope my issue was understandable.

Coren
  • 5,517
  • 1
  • 21
  • 34
Bautzi89
  • 346
  • 1
  • 5
  • 21
  • I think that an iOS device will have more than 1 KB of memory available, but if you are really worried, use objc++ and a vector. – Richard J. Ross III Mar 14 '12 at 15:16
  • I'm quite new to iOS programming and I don't want the application to face memory issues or performance problems all the time. So I wanted to know wether there can be a problem with the increasing array size, because it's only the buffer that has the 1KB size... – Bautzi89 Mar 14 '12 at 15:47

2 Answers2

0

Have you considered using NSData (or its mutable subclass NSMutableData) instead?

These provide an object wrapper for byte buffers, and can be grown arbitrarily using the appendData: selector.

From the documentation:

NSMutableData (and its superclass NSData) provide data objects, object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation objects.

That said, if you're only allocating on the order of kilobytes you're not likely to face memory issues.

jnic
  • 8,695
  • 3
  • 33
  • 47
  • So you mean I should keep appending the bytes to an NSMutableData object and then use this to work with my object. Am I getting you right? – Bautzi89 Mar 14 '12 at 15:42
  • That's correct. That way, you allocate only as much memory as is required for your binary data. – jnic Mar 14 '12 at 15:52
0

You can use NSMutableArray and store data temporarily and expand its size as needed.

Hope this helps.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
MrTJ
  • 13,064
  • 4
  • 41
  • 63
  • Thanks for your answer. Is there any limitation that I should be aware of? – Bautzi89 Mar 14 '12 at 15:39
  • I think only general out of memory warnings that the OS will post you via AppDelegate's `applicationDidReceiveMemoryWarning`. I successfully used NSData of a size of ~2Mbytes, I don't think NSMutableArray would be different from memory limitations constrains. – MrTJ Mar 14 '12 at 15:41