NSURLConnection lost after ipad sleeps

Discussion in 'iOS Programming' started by tiltem, Dec 8, 2010.

  1. tiltem macrumors newbie

    Joined:
    Oct 18, 2010
    #1
    I have an ipad app that has a download that takes about 30 min. If the device is plugged in it works fine, but if not the connection seems to get lost and the app stops after the ipad "sleeps" from inactivity. How can I keep it awake or keep the connection going while the device sleeps?

    connection code:
    Code:
    // Create the request.
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
    											  cachePolicy:NSURLRequestUseProtocolCachePolicy
    										  timeoutInterval:60.0];
    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    	if (theConnection) {
    		// Create the NSMutableData to hold the received data.
    		// receivedData is an instance variable declared elsewhere.
    		[[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil];		
    	} else {
    		// Inform the user that the connection failed.
    	
    	}
    }
    
    
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        // release the connection, and the data object
       
    	
    	[connection release];
    	
    	
        // inform the user
        if (kDebugMode) NSLog(@"Connection failed! Error - %@ %@",
              [error localizedDescription],
              [[error userInfo] objectForKey:NSLocalizedFailureReasonErrorKey]);
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath: filepath];
        [file seekToEndOfFile];
    	progressView.progress = (float)[file offsetInFile]/(float)[response expectedContentLength];
    	[file writeData: data];
        [file closeFile];	
    
    }
    
    
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Address this issue first. No user should have to wait 30 mins for something to download. Find a way to seriously optimize this or come up with another approach.
     
  3. tiltem, Dec 8, 2010
    Last edited by a moderator: Dec 8, 2010

    tiltem thread starter macrumors newbie

    Joined:
    Oct 18, 2010
    #3
    Well really about 15-20 min. and only once. Then they have all the content about 1G.

    Used:
    Code:
    	[UIApplication sharedApplication].idleTimerDisabled = YES;
    
    to get around the issue, and reset after done.
    If it could be shorter that would be great!
     
  4. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #4
    Why don't you just include the data in the app bundle if they have to download it anyway?
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    If it's only once, you should do as cnstoll suggested and just include the data in the app bundle. If you need to ensure they have the latest data, still include the base data in the app and then build a mechanism to download updated pieces only!
     
  6. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #6
    That would ultimately piss me off and make me uninstall the application. Not only would my iPad be unavailable to me during the download, but the battery would be torn to pieces while the download is processing.

    Incremental updates is the way to go.
     
  7. tiltem thread starter macrumors newbie

    Joined:
    Oct 18, 2010
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Is that a big concern for you?
     
  9. tiltem thread starter macrumors newbie

    Joined:
    Oct 18, 2010
    #9
    client wants it to available for download over 3g network
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  11. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #11
    haha.. no kidding. Talk about hammering by about 5 times, what, about 95% of people's 3G quotas? That should fly well when they run the app and get slammed with a huge increase in the monthly bill.
     
  12. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #12
    So, if someone with 100 megs of free space downloads your app (expecting it to work), then finds out that it won't, their reaction will be -- what exactly? Understanding? Normal users don't want their apps to act like this. Putting it in the app description isn't good enough. For the user to need *fifty times* the extra space is just asking a lot.

    I'm sorry your client is pushing for this. I would recommend explaining to your client that not only is this demand producing technical issues (the bug), but will also produce a less-than-optimal user experience; the result will be bad customer reviews and fewer downloads -- the 3G availability will be a non-issue.
     
  13. MacBill macrumors newbie

    MacBill

    Joined:
    Dec 31, 2010
    Location:
    Western Montana
    #13
    Set auto lock to NEVER, plug iPad into charger, then go get a snack, go bowling, take your girlfriend to a movie. I wish the iPad had a faster processor.
     

Share This Page