iPhone downloading many small JSON files and storing in DB asynchronously - iphone

I have to download 100 thousand JSONS, each JSON not more than 200 characters. I am using AF networking. Is there a way to encode to reduce the size at the server side and send and i should be able to decode at iPhone side.
Moreover has anyone got a method to download the JSONS and store it in the DB on the background thread.Because when i do that directly the UI thread is blocked. Sample Code would be really helpful.
Need the best way to download the HUGE-JSON and store it in the DB.Thanks!

For encoding you can, for example, use gzip-compressed data in your http response that will be unpacked by ios automatically without the need to code anything. Just add "Content-Encoding: gzip" to your http response on the server side. On iOS, i think NSURLRequest accepts gzip encoding by default, or you can set
[request setValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"]
Of course you can download the JSONS in the background. A good source for information and example code is here http://iosdevelopmentjournal.com/blog/2013/01/27/running-network-requests-in-the-background/

Related

how to upload a picture or a wav file with some parameters using Alamofire.upload function

I want to upload some file to a server, but it needs a token to post with the file together. I got the token when I logged in, so how could I post to the server? Can I write code like this?
var par=[
"token":"xxxxxxxxxx",
"file":"filename.file"
]
Alamofire.upload(.POST, "http://www.xxxxx.xxx", parameters: par)
This is most likely not supported in the current version of Alamofire depending on your server implementation.
Multipart Form Data
Your server most likely expects the data to be multipart/form-data encoded. Currently, multipart form data is not supported by Alamofire. You will need to encode the data yourself according to the RFC-2388 and RFC-2045.
If this ends up being the case, you can either implement your own version of the specs, or you could use AFNetworking. I would encourage you at the moment to use AFNetworking if this is the case. Here is a thread (courtesy of #rainypixels) to get you started if you decide you really want to implement this yourself.
You need to be careful with this option as it is an in-memory solution. Do NOT attempt to upload videos or large numbers of images in this way or your app out-of-memory very quickly.
File Upload
If the server does not expect multipart/form-data encoding, then you can use the Alamofire upload method.
public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request
You could create an NSURLRequest with the token appended as a parameter, then pass the fileURL off to Alamofire to be uploaded.
In summary, I'm pretty certain that the first approach is what your server is going to require. Either way, hopefully this helps you get heading in the right direction.

What is the best way to transfer images from a server to an iPhone app?

What is the best way to transfer images from a server to an iPhone app? Is sending a base64 string faster or sending a link to the image source and then downloading from this source?
Is sending a base64 string faster
Definitely not - base64 is, on average, 1.4x larger than binary. (http://en.wikipedia.org/wiki/Base64)
or sending a link to the image source and then downloading from this source?
I'm not sure what you mean here - but two requests when you could simply make one (the request for the image) is also not a good idea.
Simply download the image like normal is the best approach.
You could make sure you're making effective use of caching and GZIP (standard HTTP stuff).
The second option is better than first,it would be better to have http link.
just make sure you load the images just once.

About resume broken upload with AsiHttpRequest

I use ASIFormDataRequest to upload my file to Server.
But when the wireless signal is not good enough, Uploading is always failed in the end.
How can I do to resume broken upload? Do AsiHttpRequest support it?
Thanks!
HTTP in general doesn't support resuming uploads.
I think your best approach would be to change the way you upload the file, so that you upload it in chunks.
ie. split the file down into segments of a smaller size (eg. 256 kilobytes) and send each of them in a separate ASIFormDataRequest. Then get your server software to glue them back together.

Sending data to a server for computation

I want to create an application which will will be a webapp. I want to collect the data from the user, send it to a server where the computation will take place, and have the result displayed on the iPhone screen. The server normally takes results from a regular webpage via text fields and computes it and displays the result on the webpage. I just want the send the data via iPhone. Navigating my iPhone safari to the webpage is NOT an option, as the webpage is not optimized. So I how do I send data to the server, make it compute the results and have the results displayed on my iPhone?
Thank you.
Regards
EDIT:
I have no control over the server. Imagine my case to be as follows: The user enters a word, the word is sent by the iPhone to a Google server, the server compiles the search results and sends it back to my iPhone, and then the iPhone displays this result on the screen.Any more suggestions?
You might consider using ASIHTTPRequest/ASIFormDataRequest if you want to submit form data to your existing web page using form fields (per your description.)
In general I find ASIHTTPRequest friendlier to use than NSURLConnection / NSURLRequest.
http://allseeing-i.com/ASIHTTPRequest/
The most straightforward way is to use NSMutableURLRequest to create the GET or POST request, and then NSURLConnection to (asynchronously) send the data and receive the result. You could also use any number of third-party libraries to do the same thing.
As for the server side of things, you would have it accept a GET or POST just as you would with a web-based app, and output data in an appropriate format.
As for the output format that will be parsed by your app: With the standard classes, you can easily parse plist data and (with a little more work) XML; third-party libraries can be found to parse json and many other formats, if you so desire.

AQXMLParser unable to parse byte-array?

I've a general question, I've been using NSXMLParser to parse a pretty large XML file containing byte array's of images sent from a web service to the iPhone. I tried switching to AQXMLParser to bring down the memory footprint however now I'm unable to decode my byte arrays in the same way.
When parsing the URL I use foundCharacters to read in the string of the byte[] and pass this to some standard code I found on the net which gives me back the NSData I then use to create my image. The error I'm getting with AQXMLParser is "Application transferred too few scanlines". My suspicion was that because AQXMLParser chunks up the data my parsing class isn't getting enough of the image data in one chunk to enable it to parse the whole image?
Off the top of your head does anyone know why this would work ok with NSXMLParser but not with AQXMLParser? Or can anyone suggest a better way to send images from my web service, pretty new to all of this! The images need to be sent to the device and are then stored locally for future access, hosting them on the web and accessing them via a url is not an option.
Any thoughts that anyone has would be helpful, I can post some code if need be.
Thanks :)
Not directly answering your question, but XML is pretty terrible for binary data. Have you considered hosting it as individual files, or in a zip? ZipArchive is vaguely reasonable.