Is using a socket connection possible within iOS 7 background fetch mode? - ios

Apple added in iOS 7 new possibility to execute activities in the background, fetch mode, which can be used e.g. for Twitter, when frequently small amounts of data need to be downloaded (official description: The app regularly downloads and processes small amounts of content from the network.),Apple Document Reference
However, I need to use a TCP connection.
Does anyone know whenever TCP connection may be used in such scenario between the calls?
The scenario is:
I open the TCP socket in the beginning and then the application can use it in these fetch calls. Of course handlers are needed when the TCP connection drops, but there is a difference between rare necessity to reconnect and constant (i.e. in each fetch iteration) need to reconnect.

Related

To close NSStream objects before resigning active

I have an NSInputStream and NSOutputStream scheduled in a non-main-thread run loop. I'm using CFStreamCreatePairWithSocketToHost. The remote endpoint is an external device that communicates with the app with TCP over wifi.
When applicationWillResignActive is called, I want to close these. I want to be guaranteed that they will close, because another app may want to communicate with the same device, and the device only accepts one TCP connection.
Additional information:
It is a frequent occurrence in the context of this device that multiple apps may need to communicate with the device.
I have tried using performSelector:onThread:withObject:waitUntilDone:
(with waitUntilDone:YES) in applicationWillResignActive. Sometimes this doesn't work, and in those cases it seems that the selector does not get called.
My understanding is that I can't close the NSStream objects or destroy the task queue from the main thread.
I can not change how the device receives connections. It listens on one address/port for TCP connections, and allows only one connection (it doesn't multiplex connections by source port).
Use dispatch_sync() to the background queue.

How MQTT saves battery and supports limited network connectivity?

I've been reading about MQTT and I understand it uses TCP for network transport. So if I have a mobile app that will send subscribe request, I presume this will be an full-duplex connection so the client can be notified for incoming pushed data.
How then is this more battery and network efficient? I mean you still established an open TCP connection. Also how does it handle disconnection, does it auto-reconnect to the broker?
Taking into account my comment on the question, assuming you want to compare to HTTP Long polling these 2 links may help to answer your question:
https://www.ibm.com/developerworks/community/blogs/sowhatfordevs/entry/using_mqtt_protocol_advantages_over_http_in_mobile_application_development5?lang=en
http://stephendnicholas.com/archives/1217
TL;DR version:
The message sizes tend to be significantly smaller with MQTT vs HTTP (especially when you take into account all the http headers that get sent), this saves on network usage and in turn battery usage.
As for the reconnect side of things, the client libraries do not automatically reconnect but they do trigger a call back when the connection drops so you can handle reconnecting as required.

Sync TCP Client in multi-client application without lags

I have application, whitch connents to N devices trought the network.
The problem is:
TCP Client class isn't async, I'm send the command to device, I want get answer now.
So, 2 or more active clients makes my app frozen.
Any ideas how to do some, that's will not freeze my application if many devices is enabled at one time?
TCP Client class uses Synapse classes package.
Thanks!
To avoid application hang, use a separate thread for every TCP client connection.
The thread then will run independently, connect to the device, and do the communication.
Note that the thread must not access objects/data in the main application thread without proper synchronization.

apple push notification - design of the server part

I have seen many tutorials and opinions on that - I have developed a simple script that will send batches of notifications to Apple servers, and I crontask it every 5 seconds. (it's php so far, should be improved soon).
This way, the code is simple, and if 1 batch is larger, it will not delay the next batch. It also allows to run on multiple servers to dispatch faster and solves any concurrency issues
However, I am not clear about Apple tolerance to the fact that I open/close a socket with them every 5 seconds... In sandbox mode, it works perfectly.
Thanks for the advice
How about this approach:
- batch all in some data-structure (I don't know about PHP, but in Objective-C this would be a NSArray containing instances of some custom NotificationClass or NSDictionaries).
- if you have either A) batched up more then X notifications you send them through, or B) waited a certain time (e.g. 5 seconds).
Depending on what you use the push notifications for you can adjust these variables. A chat app for example want them to be sent immediately, while some daily messaging app would probably be OK with sending them all at once each hour or so.
Also, Apple's words on opening connections:
You may establish multiple connections to the same gateway or to
multiple gateway instances. If you need to send a large number of push
notifications, spread them out over connections to several different
gateways. This improves performance compared to using a single
connection: it lets you send the push notifications faster, and it
lets APNs deliver them faster.
Keep your connections with APNs open across multiple notifications;
don’t repeatedly open and close connections. APNs treats rapid
connection and disconnection as a denial-of-service attack. You should
leave a connection open unless you know it will be idle for an
extended period of time—for example, if you only send notifications to
your users once a day it is ok to use a new connection each day.
See here for official docs (Best practies for Managing Connections).
The idea is that you simply leave the connection open between the batches.
In fact, Apple recommends you create multiple connections, so you can divide the 1000 over maybe five or ten or so connections. You could have some connection manager in your code that controls the lifetime of the connections: if no messages have been send trough for x amount of time, shut it down. If some piece of client-code requests a connection, this manager either creates a new one, or returns one that is not currently being used to send data through. The client of this manager should be prepared to not receive a connection, and the connection manager should in turn notify clients about the availability of connections.
Lots of possibilities in terms of architecture, but the main point it is better to leave a connection open and idle for some time, then to open and close them many times.

socket connection in iOS and background tasks

I am designing an app that needs to exchange data in real time over network and I am planning to use socket connection to achieve this. To keep the socket connection open in background I plan to declare my app as a VOIP app (I already know apple's rule prohibiting this and willing to take the risk), and use keepAliveTimeout to keep connections alive.
Now can I declare and own a socket which will listen to any incoming data or I have to use an existing/specific socket given by Apple? Also is the port unique per app or per phone? Means if my app is continuing in background and also a call comes over Skype would they interfere with each other?
Thanks in advance for response. Help would be appreciated.

Resources