0
votes

I am running an app that is continually looking for data from a server. I am creating a nsurlsession with datataskwithrequest and completionhandler. I repeat the request every thirty seconds. The problem is that after a certain amount of time, I am geting a dnssd_clientstub Connect to server: socket failed 24 to many open files. I think I need to close each session after I get a response with or without data or use the same session over and over again. I am looking for any suggestions.

1

1 Answers

0
votes

I’d suggest you don’t constantly reinstantiate new NSURLSession objects, and instead make sure you re-use your existing instance.

If you absolutely need separate NSURLSession instances, you can clean up prior ones with finishTasksAndInvalidate. I think it’s better to use single instance, but this is one way to clean up your old instances if you absolutely need to have separate ones.


BTW, you might consider a different network pattern, namely socket-based solution. Then, rather than being like Bart Simpson in the back seat of the car, repeatedly asking “are we there yet?”, you can have your server tell your client when there’s something the client needs to be informed about. It’s a radically different approach, but is much better than polling every 30 seconds. If you don’t want to roll your own sockets-based solution, frameworks like Firebase and Realm provide this sort of realtime interaction.

Or, if you really want to use your own server and you’re not ready to make the jump to sockets (or if you have no control over the server’s RESTful interface), consider having your server initiate a push notification when there is data for the client. It isn’t as reliable, but still better than polling every 30 seconds. Push notifications also has the virtue that the server can tell your clients when there is data to be retrieved even when the app isn’t running and the user can choose whether to fire up the app (or the app can fetch the data in the background so that if and when the user does fire it up, the data is already there).