Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russbishop/7a8b505a09c3df5cb9bb to your computer and use it in GitHub Desktop.
Save russbishop/7a8b505a09c3df5cb9bb to your computer and use it in GitHub Desktop.
Storing Info in background requests.
/*
Basically what we are going to do is use and profit from NSURLRequests being conformant to NSCoding,
and a little known API from NSURLProtocol which allows us attach info to requests.
*/
//Step 0: For the purpose of this gist, we'll already have a background session setup and assume a bunch of stuff.
NSURLSession *bgSession = [NSURLSession magicMethodWhichGivesMeTheAlreadySetupSession]; //Geeez, Methods are long in Obj-C.
//IMPORTANT: Request must be mutable in order for this to work. Got an immutable one. Make a copy. Can't? Well, Make it so!.
//Step 1: Setup your basic request.
NSMutableURLRequest *mutableRequest = ...;
//Step 2: Have some values to store under our request.
//IMPORTANT 2: All Keys and values must be encodable, so no custom classes or you will cry when your device explodes.
//You have been notified.
static NSString *const DAKey = @"ThePathToTheTruth";
NSDictionary *aBunchOfValues = @{...};
//Step 3: Store the values using NSURLProtocol API.
[NSURLProtocol setProperty:aBunchOfValues forKey:DAKey inRequest:mutableRequest];
//Step 4: Create your task and resume it. (We gotta do the request sometime right?)
NSURLSessionTask *task = [bgSession downloadTaskWithRequest:mutableRequest];
[bgTask resume];
/**
And now, now you app DIEEEES, DIE APP, DIEEEEE.
Ok, now that we took care of that, well, that's it folks, bye!.
- "hey, what happened with my request and my bunch of values!, the app is dead, I'll never get them back."
Hey, Calm down... remember NSURLRequest's conform to NSCoding? well, NSURLSession's have to store the info of the
request *somewhere*, well, turns out they use NSCoding to do that, which is kinda the duh solution but you never know with Apple.
So... NSURLProtocol basically uses some private methods to modify the info of the NSURLMutableRequest. (See, that's why it has to be mutable)
Which then tag along when encoded by the session.
- "Oh, ok, get it, can you show me how to get the info back out now"
*/
//Step 5: Coming back from death.
//Either your app was launched by the system or the user and you got ahold of the task again.
//How? well, go watch the nice talk by @purpleyay, she tells you how.
NSURLSessionTask *ourPreviousTask = ...; //Wow, I love I can get away with code with 3 simple dots.
//Step 6 (And final, yey!): Getting the info out.
NSDictionary *aBunchOfValues = [NSURLProtocol propertyForKey:DAKey inRequest:ourPreviousTask.originalRequest];
//That's it, you have all the info you need
//NOTE: I haven't tested the limits of storage in this method but the API was done yo hold simple keys and values
//so don't you dare store your whole database of kitten images there. Thank you.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment