Preston
Preston

Reputation: 1059

How should I go about periodically getting information from a web server?

I have an xml file on a web server that is periodically updated. Right now, my app downloads a xml file every 8 seconds and parses it. This is probably the worst way to handle this kind of situation and doesn't work sometimes. How should I do this? Is there a way to send a packet requesting information from a database, then sending a packet back to the app? I don't know where to start. Thanks for the help.

EDIT: Here is what I am doing right now: 1. The app will initially download the XML file and save it to the documents directory. 2. It will parse the XML file and a 3 second timer will start. 3. After the timer finishes, it calls a function which deletes the XML file and calls a new timer set to 3 seconds. 4. After that timer runs out, the download starts over and the looping continues.

I think the problem is, looking at the XML file in the simulator's document's directory, is that after the xml file is updated and the app downloads it, I still see the old on in the documents directory even though I see that it is updated on my web server. I can also see the app delete the xml file and see it show up again so that is not a problem.

Upvotes: 1

Views: 174

Answers (3)

pradeepa
pradeepa

Reputation: 4164

Follow the Push Notification concept, this might be helpful,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];




    NSLog(@"Registering for push notifications...");    
    [[UIApplication sharedApplication] 
     registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeAlert | 
      UIRemoteNotificationTypeBadge | 
      UIRemoteNotificationTypeSound)];


    return YES;
}


- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 

    NSString *str = [NSString 
                     stringWithFormat:@"Device Token=%@",deviceToken];
    NSLog(str);

}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 

    NSString *str = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(str);    

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }    

}


Upvotes: 0

Alan Zeino
Alan Zeino

Reputation: 4396

One of my preferred options for this is to use apns; though you will have to factor in the possibility that since apns works over UDP there is no guarantee that all push payloads will be received on the device. There is also a chance that in development it will work fine, but excessive push payloads on Apple's production gateway will result in a ban. Otherwise, it is a simple service since it offloads the timer responsibility to the server in this scenario; all your app needs to do is implement the delegate method application:didReceiveRemoteNotification:. One positive of this approach is that for apns push notifications fired between a longer time period; you can notify of critical changes in the xml file.

EDIT: Sorry, apns actually uses TCP - however, Apple does not guarantee the receipt of an apn push on a device.

Upvotes: 0

dbrajkovic
dbrajkovic

Reputation: 3703

Polling, which is what you're currently doing, is probably one of the most popular ways. Alternatively, look into WebSockets. https://github.com/esad/zimt

Upvotes: 2

Related Questions