3

Try to download a zip file from the given URL, since the zip file Gets updated daily I wrote the codes in ApplicationDidFinishLunching method but it doesn't work. anything wrong with my code?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    NSString *stringURL = @"http://webspace.apiit.edu.my/intake-timetable/download_timetable/timetableXML.zip";
    NSURL  *url = [NSURL URLWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    //Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths  objectAtIndex:0];

    //Save the data
    NSString *dataPath = [path stringByAppendingPathComponent:@"timetableXML.zip"];
    dataPath = [dataPath stringByStandardizingPath];
    [urlData writeToFile:dataPath atomically:YES];
    return YES;
}
Ben
  • 953
  • 12
  • 27
  • Yeah, you are downloading on the main thread synchronously. That is a big no-no but it may or may not be the reason. If the download takes more than 10 seconds, though, it will crash. Use async methods (dispatch_async, NSURLConnection, etc). – borrrden Jun 24 '13 at 09:51
  • @borrrden I'm aware of not to downloading on main thread but this download is few kb only, do you see any problem in my code? – Ben Jun 24 '13 at 09:59
  • Even though the file is only a few kb the download can take some time and as the download is performed on the main thread this will prevent the app from launching! – Groot Jun 24 '13 at 10:03
  • @Ben You are making a sync network call in the wrong place. `application:didFinishLaunchingWithOptions:` should return immediately or else your app will be killed by the Springboard with error code [`0x8badf00d`](http://developer.apple.com/library/ios/#qa/qa1592/_index.html). To avoid that, initiate async call (on background thread) which will download the required file on every launch. – Amar Jun 24 '13 at 10:07
  • Great responses and all noted. Thank to all of you guys. – Ben Jun 24 '13 at 10:10

1 Answers1

15

Try this instead as it will download the zip file in the background. Even though the file is only a few kb the download can take some time and as the download is performed on the main thread this will prevent the app from launching!

dispatch_queue_t queue = dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{

    NSLog(@"Beginning download");
    NSString *stringURL = @"http://webspace.apiit.edu.my/intake-timetable/download_timetable/timetableXML.zip";
    NSURL  *url = [NSURL URLWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url];

    //Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
    NSLog(@"Got the data!");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths  objectAtIndex:0];

    //Save the data
    NSLog(@"Saving");
    NSString *dataPath = [path stringByAppendingPathComponent:@"timetableXML.zip"];
    dataPath = [dataPath stringByStandardizingPath];
    [urlData writeToFile:dataPath atomically:YES];

});

You should never download anything on the main thread as it will "block" your application. It should however be noted that there are better ways than this to download data. Read for example this.

Community
  • 1
  • 1
Groot
  • 13,943
  • 6
  • 61
  • 72
  • seems a pretty polished code, Currently the webservice is down. I keep on trying it, I will give you a feedback. Tnx – Ben Jun 24 '13 at 10:11