Async NSURLConnection with delegate pattern - iphone

I know this question has been asked a lot of time and a lot of suggestions exist on the net. But i am still not able to conclude what is the right way.
After ASIHTTPRequest becoming obsolete, i think it is not advisable to use any third party libraries(though AFNetworking is really good), hence i am trying to build a one on my own.
I want to have following features in it Asynchronous concurrent
connections
Support of delegate methods to track connection progress
Support of doing authentication
Ability to cancel any connection and restart
I did came across multiple recipes using NSOperation, GCD, NSBlockOperation, NSRunLoop, etc. Also i did check the code of AFURLConnectionOperation but it is hell lot of a code which i think can be over kill in many situations.
Can anyone suggest me the optimum and minimalist way to achieve my requirements?

Related

multithread database fetch xcode

Hi thank you for the help in advance,
I have looked at some of the posts and I am a bit confused about the multi threading. It seems that it may be pretty easy, however I am very new to programming so I am still trying to grasp this.
These are two calls to pull data from a database, and they take forever as it is... So I'm thinking about multithreading these until I can learn how to build a core data for this. Right now i am using sqllite and the database involves 10,000 + recipes... So not lightning fast like I would like...
Please let me know what you think, and how I can make these happen maybe simultaneously? (If thats even possible)
Thank you in advance.
requestCount++;
[[DataPuller sharedDataPuller] getAllDeletedRecipeList];
[DataPuller sharedDataPuller].target = self;
requestCount++;
[[DataPuller sharedDataPuller] getAllRecipesList];
[DataPuller sharedDataPuller].target = self;
If you're doing SQLite, you might want to contemplate using FMDB which (a) gets you out of the weeds of sqlite3 calls; and (b) offers a FMDatabaseQueue which allows you to coordinate queries from multiple queues, so that the data operations don't stumble across each other.
Having said that, you suggest that you're having significant performance issues which you're hoping to solve with a shift to Core Data or going multi-threaded with SQLite. That's unlikely. Poor performance of local database operations is generally more of a matter of your application design (e.g. it's unlikely to be wise to try to retrieve the entire details for all 10,000 recipes ... you probably want to retrieve just the unique identifiers, perhaps only those required for the given screen, and then only retrieve the particulars for a given recipe at a later point as you need that). For local database interaction, you rarely have to contemplate a multithreaded implementation, but rather just design the system to retrieve the least possible information at any given point that you need for the presentation. I personally find that my database-driven apps generally only need to go multithreaded when I'm doing extensive interaction with some remote web service (in which case, it's the retrieval of server data and the parsing of that which goes on the separate thread, not necessarily the database operations themselves).
If you're having performance issues with your recipe app, I'd suggest you submit a far more detailed question, with code samples, that articulates your particular performance problem. And I wouldn't be surprised if multi-threading was not part of the solution. Likely, appropriate use of indexes and a more judicious retrieval of information at any given point might be more critical.
Get records from database in the form of pages; i.e. 20 or 50 recipes per page. Have a look on YouTube app. on iPhone or have a look on my app. HCCHelper

How do I communicate with remote servers from an iOS application?

I want to create an application which can communicate with my central server database.
Example: One time registration for every user and I want to store registration data to my server database and show all users to show the list of all registered users.
What I have to do to implement this kind of thing?
You need to use NSURLConnection class to get this done. Alternatively, you can use ASIHTTPRequest, which, IMO, is much easier and user friendly. You can find ASIHTTPRequest here as well as detailed guide on how to set it up and use it: ASIHHTPRequest
Despite the advice you will get to use ASIHTTPRequest, or a variety of other frameworks that say they simplify networking, be aware that ASIHTTPRequest is no longer under active development.
If you are unsure about networking - just use the NSURLRequest/NSURLConnection path. It really isn't difficult to get up and running with it, and it works asynchronously with about 4 delegate calls. It's fast, built in, well integrated, well documented, and actively developed.
And, if in your application writing career, you think you might want to move on to using any other of these asynchronous networking wrappers, you'll be able to read the code, understand how they each handle different problems in different ways because you've had to face those problems while writing code yourself, and you'll be able to make an informed choice about which wrapper to use, rather than just picking one at random.

Porting an application with fork() to pthread_create()

I am porting a linux application to the iphone and I would like to know how much re-writing I have to do to make it a multi-threaded application rather than a multi-process application.
Also, if I simply replace the forked code with a call to the functions on another thread I get exec_bad_address at seemingly random places in my flow of execution... Does anyone know why this may be the case?
Thanks!
It is exactly the same effort you would undergo in transitioning your application to a multi-threaded one on a unix platform. Simply replacing the forking code with calls to pthread_create() is - almost always - not enough. For instance, you may need to rewrite all of the code that supports inter-process communication. Say one process forks another, and then the two processes communicate through a pipe. Of course, this will not work anymore. Etc. You get the idea.
By the way, while pthreads are available and perfectly working on iOS, you are strongly encouraged to use other technologies available in iOS. For additional details, please read
Concurrency Programming Guide
in particular the Section called "Migrating Away from Threads".
For details about threading, see
Threading Programming Guide

ASIHTTPRequest alternative

I am using currently ASIHTTPRequest for communicating with the server, however it's causing crashes. The bug is known for a long time, but is still an issue.
Therefore: Are there good alternatives to ASIHTTPRequest? I am flexible on the server side, i could also use sockets or using something else.
What would you recommenend for server client communication?
Here is the link to the topic where the author is giving some ASIHTTPRequest alternatives.
List of them with links from topic:
AFNetworking
LRResty
RestKit
AWS SDK for iOS
ShareKit
NSURLRequest/NSURLConnection
I know it's probably not the answer you are looking for, but if ASIHTTPRequest is crashing then it's at least 90% likely that there's an error in your code - the most common problems all relate around request vs delegate lifetime issues. NSURLConnection might well just give you a different set of problems (though it might just work well). NSURLConnection also has a lot fewer features (no caching etc).
Generally, you need to remove the delegate from ASIHTTPRequest before the delegate is destroyed (ie. at the very start of the delegate's dealloc method). So long as this is done, there is no way for ASIHTTPRequest to call a deallocated delegate. You must do this for every request, including ones that have been cancelled.
The bug you link to seems to relate to one very particular circumstance, and at least a one reason it's not been fixed is that it appears no one else has been able to reproduce it.
(I'll caveat this with the statement that their have been problems with ASIHTTPRequest in the past - in particular the release of v1.7 contained some race conditions that could cause crashes, but these were later fixed in the git tree. I've not personally tried v1.8, but v1.7 with the fixes is working very well for me.)
It might be worth posting a new question detailing the crash you're seeing and sharing some of your code / how you use ASIHTTPRequest.
ASIHTTPRequest is pretty popular and used by a good number of applications so, as JosephH suggests, it's more likely that there's a bug in your code.
Having said that, if it is in ASIHTTPRequest then it's open source -- you can fix it. I'm sure the maintainers would appreciate a patch. A BAD_ACCESS is likely over-releasing memory.
It's difficult to suggest an alternative since you don't say which features you're using. You could just use the native NSURLConnection if you're just using the basics (I wrote a thin wrapper around it and I'm sure there are a bunch of others out there). At the other end of the spectrum, there are REST libraries that integrate with Core Data too (RESTKit).

What's the best way to implement HTTP Post chunked request on iPhone?

There's a really old thread from 2008 that mentioned NSUrlConnection is quite buggy and leaks a lot. Is this still the case?
Is there any Cocoa Touch class that already implements the chunked upload or am I better off using the CF classes?
NSURLConnection doesn't leak, but you DO need to know when and where to release your references to it. Bear in mind that on a mobile device, streaming upload is a bad plan, because you're keeping the radio on fairly consistently, and probably overriding whatever power management is designed to keep it inactive where possible.
In the mobile space, if it is at all possible to upload the file all at once, I would suggest going that way.