I found the solution, switching to GDCAsyncUDPSocket solved the problem. I think this problem is related to (App stops receiving data from socket when UI scroll) or some other issue regarding threading.
Objective C NSTimer and CocoaAsyncUDPSocket issue
-
15-06-2023 - |
Question
My apps contains one UITableView with a list of UDP servers (custom game server I made). When the user pulls the table the app will scan for servers in the network.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// load the connection
self.connection = [[Connection alloc] init];
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:self.refreshControl];
[self handleRefresh:nil];
}
-(void)onServersFound:(NSTimer*)timer
{
self.serverList = self.connection.serverList;
[self.refreshControl endRefreshing];
[self.tableView reloadData];
}
-(void)handleRefresh:(id)sender
{
// get connections
[self.connection searchForServers];
[self performSelector:@selector(onServersFound:) withObject:nil afterDelay:2.0];
}
This is going fine when I launch the app, the TableView is filled with the servers in the network. Put after requesting a couple of refreshes the application hangs (refresh control still circling) and the onServersFound function is never called.
If I comment out the call to [self.connection searchForServers] its working fine.
The 'connection' variable is sending and receiving data from the CocoaAsyncUDPSocket.
// UDP connection and handles messages
@interface Connection : NSObject <AsyncUdpSocketDelegate>
@property (strong, nonatomic) AsyncUdpSocket *udpSocket;
@property (strong, nonatomic) Server *server;
@property NSMutableArray *serverList;
typedef void (^CallbackFunction)(BOOL success);
// methods
- (NSData*)createIdentifyMessage:(NSString*)name;
- (NSData*)createHelloMessage;
- (NSData*)createControlMessage:(CONTROL)input :(int)ids;
- (void)sendPlayerInfo:(NSString*)name :(NSString*)host;
- (void)searchForServers;
- (void)connectToServer:(Server*)serv withCallback:(CallbackFunction)callback;
And the 'search' function:
// update servers
- (void)searchForServers
{
NSLog(@"Called findserver");
NSData *data = [self createHelloMessage];
[udpSocket sendData:data toHost:@"255.255.255.255" port:GAME_PORT withTimeout:-1 tag:-1];
}
Why is the application hanging after a couple of refreshes?
Thanks
EDIT: I stripped the code down to the necessary parts.
Solution