How to receive and store binary data from server in iOS?

大兔子大兔子 提交于 2019-12-11 18:04:12

问题


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.


回答1:


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

Hope this helps.




回答2:


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.



来源:https://stackoverflow.com/questions/9704698/how-to-receive-and-store-binary-data-from-server-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!