Question

-(BOOL)sendMessage:(NSMutableDictionary *)_message {

    //*** Process _message and convert it to NSData here ***//

    NSError *error;
    BOOL success = [currentMatch sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
    if (error != nil) {
        NSLog(@"Sending data ERROR");
    }
    return success;
}

I started a match (stored in currentMatch) with another player and continuously sent data using the above method. Then I turned off the wifi.

However, I am not getting the "Sending data ERROR" log message at all.

Why? I turned off the internet connection, so why is there no error here? What could possibly lead to this scenario?

I've also confirmed that the dictionary I am sending is properly encoded into an NSData object, and success is returning YES.

Was it helpful?

Solution

As per the documentation

Return Value

YES if the data was successfully queued for transmission; NO if the match was unable to queue the data.

The method only enqueues the data for transmission, which happens asynchronously.

If you want to monitor the state of the transmission, implement the proper GKMatchDelegate delegate methods, such as match:didFailWithError:.

However, as stated in the documentation:

The match queues the data and transmits it when the network becomes available.

so if you try to perform the method with no network, the transfer just won't happen until the network is back, meaning that you won't see it failing.

By the way you should check the return value, instead of the error, since the error might be nil despite the operation being unsuccessful.

NSError *error;
BOOL success = [currentMatch sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
if (!success) {
    NSLog(@"Sending data ERROR\n%@", error);
}
return success;

OTHER TIPS

You need to check the return value of the method to know whether or not an error occurred. You cannot test the error parameter directly.

if (!success) {
    NSLog(@"Sending data ERROR -- %@", error);
}
return success;

As to why you don't get an error, that send method is asynchronous. It simply enqueues the data for transmission and immediately returns. You have to catch the error through some other means (I'm not steeped in GameKit to know what that other means might be).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top