views:

42

answers:

2

I've not found a answer to this question anywhere, but this seems like a typical problem:

I would like to send some POST-Requests (with ASIHTTPRequest, what I already do), but if something goes wrong, ther user can decide to "Try Later", that means, the task should be put on a queue and this queue should be read next time the application starts. So, that's my question: how to "save" the queue, so that the app can read it next time it starts? Is it possible to "read" the queue and try sending this POST-Request again, let's say, 10 min later, even if the application is not running?

What kind of documentation should I read in order to be able to do this?

I would be very glad to hear any answers. Thanks in advance.

P.S.: Another Idea I have: as I just have to Upload Photos, I could have a folder with all the Photos that still need to be uploaded, and when the App starts, the app looks at this folder and try to send all the photos in this folder. Does it make sense?

+2  A: 

I'm going to assume you've already looked at NSOperationQueue and NSOperation. AFAIK there is no built-in support for serializing NSOperation, but you could very easily write your own serialization mechanism for an NSOperation subclass that you use for posting data and write the an NSOperationQueue's operations to disk if something goes wrong.

Without knowing too many details it's hard to give a precise answer. There are many ways to write data to disk and load it again later, the direction you take will be largely dependent on your situation.

Bryan Kyle
Thanks for answering Kyle. Yes, I do have some knowledgment about the queues. My issue is exactly which way I should use to "save" the queue and load it again when the app starts. I have no kind of experience in this field. What would you recommend me? Would "Core Data" maybe be useful? Thanks!
jcdmb
Have a look at the NSCoding protocol. So long as your operations implement that protocol they can be written using something like [[myQueue operations] writeToFile: @"somefile"]
Bryan Kyle
I thought Core Data would be the solution, but your recommendation seens simpler, so I am gonna try it. Just started reading "Archives and Serializations Programming Guide". Thanks Kyle!
jcdmb
Hi Kyle, I started doing this but afterwards I realized it can not be done this way cause the the ASIHTTPRequest class does not implement the NSCoding protocol (which means I can't save the requests). ans this class has a lot of parameters, which means that if I subclass this class it would take the whole day to implement the NSConding protocol :( Would appreciate any other suggestions.
jcdmb
+1  A: 

My approach for this issue would be like this:

  1. Whenever you fail to send details - write content of the array to a file using '[NSArray writeToFile:]' you can use serialization if array contain any data which is custom defined (if your array contain standard cocoa objects(NSString,NSData etc) they already implemented with serialization )
  2. When app launches; load the content from file directly to an array object ('[NSArray arrayWithContentsOfFile:]')
  3. then construct http request and try sending. In application the data(in your case array) is stored/serialized not the request, you need to reconstruct the http request when you want to try one more time.(don't try serializing ASIHTTPRequest, you have reconstruct it)
Girish Kolari
Gonna try this right now. Thanks!
jcdmb
Yeah, that worked great. Thanks you and Kyle!
jcdmb