简体   繁体   中英

iPhone : How to sync data with the web-service when internet is reachable?

I know how to check internet connection. Tell me how to sync app data.

How does the synchronization works when internet is available ?

My confusion :

I have app which uses the web-service to display data on iPhone and also on site. Now I can update user's work when the net is available by calling web-service at same time when user does some task.

But when internet is not available , I have to keep user's data locally(say SQLite) in iPhone and when the internet is reachable I have to update all the data on server .

For Example, 

      If internet is reachable, user works on some task and I update the data on server at the dame time.

      But Now internet is not reachable, user works on `N task`. I can not update on server, I have to wait for internet 
      How can I update all task on web-server at time?

So my question is :

How can I update all the data when net is available ?

Any link or code which can help me ?

Try to put a Field as isOfflinerecord in your database (In case If you are using Database) and when connectivity comes back update or Insert only those data. This could be your initial step.

use reachability class and then you can set a notifier that will notify when there will be a change of the network status

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

hostReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
[hostReachable startNotifier];


- (void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

{
    case NotReachable:
    {
        break;

    }
    case ReachableViaWiFi:
    {

        break;

    }
    case ReachableViaWWAN:
    {

        break;
    }
}

}

This question might be able to help you. using the Reachability class from Apple.

` How to check for an active Internet connection on iOS or OSX?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM