JosL92
JosL92

Reputation: 1

iOS: Unexpected repeated request in AFNetworking

I've been using AFNetworking for sync information and found that it sends more than one request to the server when I apply multithreading but I only execute one request statement.

This problem can be traced using a sniffer application, because Xcode debugger can't make a trace of the request.

Also , I noticed this happen when the internet connection slows down.

Here is some code that I execute

Start sync

- (void)SyncFull
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FinishSyncFull:)
                                                 name:@"FinishSyncFull" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RemoveNotificacions:) name:@"RemoveNotificacions" object:nil];

    [[PivotService getInstance] sync];


}

Notification to continue synchronizing

-(void)FinishSyncFull:(NSNotification*) Notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"FinishSyncFull" object:nil];

    if ([SyncManager getInstance] mustSync])
    {

        [[FMDBHelper getInstance] RemoveDataFromTable:@"SyncInfo"];
        [[FMDBHelper getInstance] RemoveDataFromTable:@"SyncDetails"];


        [self startSyncFull];
     }
}

Description of startSyncFull function:

- (void)startSyncFull
{

    [[ServiceEntity1 getInstance] sync];

    [[ServiceEntity2 getInstance] sync];

    [[ServiceEntity3 getInstance] sync];

(...)
    }

Upvotes: 0

Views: 208

Answers (1)

Andrés Brun
Andrés Brun

Reputation: 185

You can only make http requests in the main thread so you have to execute every request in that threat using:

dispatch_sync(dispatch_get_main_queue(), ^{
  //Your request      
});

Good luck!

Upvotes: -2

Related Questions