After fixing the image rotation bug, we decided to make a proper update and we added a few new features. However, this proved to be a bit more difficult than expected. We encountered a well known problem in computer science, co-called race condition. Simply put, this happens if two or more processes access and update a common resource and this update leads to inconsistencies. This is typically difficult to debug, because debugging itself might change the execution of the program by introducing execution delays. This can either lead to a race conditions or not – it’s a bit like the famous Schrödinger’s Cat.
As you can imagine, fixing such a bug is not easy. In the case of iSENDu we experienced this when sending large amounts of data using bluetooth from one iPhone to another iPhone. The data receiving iPhone used a progress bar to indicate the overall progress of the data transmission while the sending iPhone did not. As a consequence, the receiver spent a bit more time on processing the data (and updating the progress bar). In the meantime, the sender continued to send the data at unchanged speed which lead to conflicts at the receiver (data was received before the data was processed and the receiver simply crashed). The funny thing was, that this behavior didn’t occur always and the insertion of debug messages in the code only made things worse and even less predictable. We noticed that if there are a lot of debug messages on the debug screen, the execution of the application gets slower. As we understood it, the main reason for this is the low level implementation of the delegate method
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
This method is called if data from another peer arrives. There is no buffer and data arriving to fast can lead to application crashes – at least we experienced such a behavior. One solution is to implement a raw data handling buffer that waits until all data has arrived. The other solution (as we did it) is to implement sender and receiver in the same manner (both showing a progress bar).
We will investigate this issue further and keep you posted about our findings. Sample code will be available as soon as we’ve submitted the new version of iSENDu.
Your ikangai team