Search code examples
iosserializationafnetworking

Send AFNetworking Requests in order reliably


So we have a messenger ( I know we should just switch to Xmpp, but no time right now) - problem being if a user sends say 20 messages rapid fire there is more than a good chance of them storing ALLLLL out of order.Is there any way to ensure the requests are sent in order without blocking the user from sending at the pace they want to send?


Solution

  • You can create a queue of NSURLRequest. When the user fires a message, this message is added to the queue and the oldest message of the queue is sent. When the completion blocks (success or failure) are called, send the new oldest message, and so on...

    Just be careful to have only one process which send messages.

    You can also take a look to NSOperation and NSOperationQueue.