0

i need some advice concerning background synchronisation...

At the moment I have an app which starts sync in background after app has started and then continues via timer every 2 minutes. Now I want to enable the user to manually start sync process in case of impatience... but ensure that there is no currently running sync.

If there is an sync running the users request should be "redirected" to this sync if not a new sync shall be started, but prevent the auto sync from being starts while manual sync is in progress.

Unfortunately I don't have an idea on how to do this....

Thx for your help!

BR Boris

Boris
  • 75
  • 1
  • 7

2 Answers2

0

You do not give much detail about how you are trying to do this, but a simple solution should be this one:

  • add a flag to your controller: isAlreadySyncing;

  • when your start syncing, set the flag; reset it when finished;

  • when the user starts syncing out of impatience, check the flag: if it is true, do not do anything; otherwise, start syncing (and also set the flag, as above);

  • when your timer fires and tries to sync, check the flag: if it is set, do not to anything.

Hope this helps.

sergio
  • 68,819
  • 11
  • 102
  • 123
0

I think you have different controllers. What you need to do is use NSUserDefaults.

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"YES" forKey:@"is_sync_auto"];
[defaults setValue:@"NO" forKey:@"is_sync_manual"];
[defaults synchronize]; 

when the user starts syncing out of impatience, check the defaults: if it is YES, do not do anything; otherwise, start syncing (and also set the flag, as above).

To redirect the request, you can create a shared instance and then invoke it again.

OR

A better way would be to create a singleton class. Let that singleton class handle the sync operation, and as no more then one instance can be created, it will solve your problem.

+ (instancetype)sharedConnection {
    static ConnectionClass *_sharedConnection = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shared_sharedConnection = [[ConnectionClass alloc] init];
    });

    return _sharedConnection;
}
Kakshil Shah
  • 3,466
  • 1
  • 17
  • 31