0
votes

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];

(...)
    }
1
Have you added breakpoints to all the places where you have requests executed, to verify that you really aren't executing more than one?KlausCPH
I verified that they're not executing more than one debugging the code.JosL92

1 Answers

-2
votes

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!