Why does my application crash when sending data via Game Kit? - iphone

I have been following this great guide on setting up bluetooth between 2 iPhones.
However, what I need to do is send binary (for instance a video) instead of text.
I load the data in to a NSData
localData = [NSData dataWithContentsOfFile:videoPath];
Then a few methods on send it
[self.gameSession sendDataToAllPeers:localData
withDataMode:GKSendDataReliable
error:nil];
But my application crashes. Do I need to encode it?

As quoted from the GameKit Documentation, "For best performance, it is recommended that the size of the data objects be kept small (under 1000 bytes in length). Larger messages (up to 95 kilobytes) may need to be split into smaller chunks and reassembled at the destination, incurring additional latency and overhead." I would assume, you're trying to transfer a video. You would need to break this up in chunks and send in pieces to be put back together on the other side.

Related

Adaptive bitrate streaming protocol (for any data)

I'm looking for some ideas/hints for streaming protocol (similar to video/audio streaming) to send any data in so called real-time.
In simple words:
I'm producing some data each second (let's say one array with 1MB of data per second) and I'm sorting that data from most important to not so important (like putting them to priority queues or similar)
I would like to keep streaming those data via some protocol and in perfect case I would like to send all of it
If not possible (bandwidth, dropping packets etc.) I would like to send from each produced array as much as possible (first n-bytes) just to keep data going (it is important to start sending new produced array each second).
And now - I'm looking for such protocol/library that will handle adaptive bit rate stuff for any data. I would expect from it to tell me how much data I can send (put into send buffers or similar approach). The most similar thing is video/audio streaming when in poor network conditions (en)coder is changing quality depending on network conditions.
It is also OK if I miss some send data (so UDP deep down of this stuff is OK) but preferably I would like to send as much data as possible per second without loosing anything (from those first n-bytes send).
Do you have any ideas of what protocol/libraries I could use for client/server? (hopefully some libs in python, C or C++).
I think IPFIX (the generic NetFlow standard) has everything you need.
You can avoid a timestamp per sample by sending a samplingInterval update every time you change your rate. You can also add other updating the change in sampling asynchronously.
As for where to put your data. You can create a new field or just use an existing one with that has a datatype you want. IE: if you are just sending uint64 sample values then it might be easier to use packetDeltaCount then create your own field definition.
There are plenty of IPFIX libraries.

Encode binary or ASCII (at least 7000 bytes) into image/barcode?

I'm wondering if there's some way to encode data (either binary or ASCII) into a printable image or data pattern that can easily be rescanned again and interpreted back into a file. The problem with QR codes is that they won't handle file sizes of 7-10KB. Any suggestions?
EDIT: One catch: Can't store said data on the server. Security reasons. The data must not exist anywhere except on a printed piece of paper.
7 kilobytes is 57344 bits, hence the graphical code needs a lot of bars or squares (in case of QR) in order to represent the data, and that's without thinking of data error correction, format information, positioning, alignment etc...
I think a sound solution will be to put the data on a server. map it with index and create a service to retrieve the data by index.
the QR/barcode will scan index and get the data from the service

NSData dataWithContentsOfFile vs NSInputStream

I have to process XML, now using NSInputStream breaks my code as I have to rewrite lots of things.
Will dataWithContentsOfFile entire file into memory, or only read contents requested for getBytes method?
I am using NSData as input parameter to NSXMLParser, I wonder is there any documentation regarding this?
There is no documentation on apple's doc regarding internals of NSData's dataWithContentsOfFile or its implementation.
When you allocating NSData for NSXMALParser ,it means creating data buffer for that object and every object occupy memory (RAM) ,because iPhone IOS know very well how to use virtual memory. When you reading entire data from the file, it occupying object data and if the data size is more than a few(depended upon OS algo) memory pages, the object uses virtual memory management. A data object can also wrap preexisting data, regardless of how the data was allocated. The object contains no information about the data itself (such as its type); the responsibility for deciding how to use the data lies with the client. In particular, it will not handle byte-order swapping when distributed between big-endian and little-endian machines.
I recommend you to read again this link
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/BinaryData/BinaryData.html#//apple_ref/doc/uid/10000037i its related to iOS. But yes one thing concern with you ….in IOS there is type of owner of object. One is user and another one is IOS object. if you creating NSData that means you allocating memory buffer and assigning data pointer to this veriable, but at that moment entire data is resides inside memory. Its our assumption, but during this period IOS know how to handle this scenario. IOS uses vertual memory technique to handle data pages.

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

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.
You can use NSMutableArray and store data temporarily and expand its size as needed.
Hope this helps.
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.

iPhone App consuming webservices how to avoid performance loss?

I have an iPhone app that grabs information from different sources in XML format. For example i have a UITableView displaying information, in xml format after being parsed, from an url like this one http://mysite.com/posts.php?top=100 -> returns posts information like this
<postsList><post><title>test1</title><pubDate>..</pubdate></item><post><title>test1</title><pubDate>..</pubdate></item></postsList>
so i have one request only for showing the table with posts. when i click on a cell i have to make three calls to three different sources to gather the data i want to show in the details of the cell.
I was thinking to write some ASP.NET C# webservices that, when called, gather all the info i need and return a single response(so i make one request) when i show the initial UITableView.
The xml response will have 10-15kb max with all the data included. From my experience network access(wireless or 3g) is expensive on the iphone from the performance point of view and if possible i would like to avoid it if it's not necessary.
So the question is: is it a good idea to get as much data as possible from a single request or is it better to make requests only when data is needed(in this case when a cell is displayed)?
If you're on a slow cellular connection, the latency will hurt you more than the limited bandwidth. This means that setting up a connection will take a lot of time because of the latency.
If you're talking about 10-15 kb, I would get all the data at once (of course, you should do that asynchronously)
Let's say you're on a GPRS connection, which is 384kbps (in our country at least). The latency is in the order of 500ms, so setting up a HTTP connection could take about 1 second (best case). Transfer speed is about 40KB/s.
Using a single download:
1 second setup + 0.5 second download = 1.5 seconds
Using 10 small downloads:
10 x 1 second setup + 10 x 0.05 second download = 10.5 seconds
This is not a scientific test, but it simply shows how you should tackle these kind of decisions.
I have been dealing with these issues alot on my current project. It has more to do with the 3G performance and latency then the actual iphone itself. Even with larger amounts of data it would be more efficient to download it all in one hit.
So i would agree with Philippe.