iOS web service sync -ascyn - ios

From the web service delay the data how can o do that the same process.When data came from the web service I want the write to the db .But data a little delaying .How can ı solve this problem.

Not sure I understood your question completely. It looks there are two parts.
You can use multithreading for your web service call so it won't block your current UI. Grand Central Dispatch can be used to manage such process for you. Here is a tutorial on GCD.
If you are using NSURLConnection, there are delegates that you can implement to wait for those callbacks. Here is one document from Apple.

Related

Implementing multiple network calls in iOS programmatically in background

I am an iOS application developer. I am implementing NSURLConnection in iOS to handle webservices calls.
In my application I want to implement the multiple network connection simultaneously. And also I want full control over stopping the network call manually.
For example , I am navigating to table view which fills data from server using connection on background thread so in this case the UI is responsive. But lets say I want to navigate back from this tableView in this case the connection is not stopped. I want to stop it.
Please help me with this scenario. I want my UI responsive anyway. Thanks a lot. It will be really helpful.
Thanks,
Ganesh
You could use NSOperation/NSOperationQueue to manage the states of your operations, which includes suspending and canceling operations.
NSOperationQueue
NSOperation
use a property to store the NSURLConnection. For your ViewController override dealloc, or ViewWillDisapper (depending on your design) call the cancel method on the NSURLConnection

Using AFNetworking to process multiple JSON responses for a single request

I'm trying to find a way to open up a connection to a web service and have that service send down JSON objects on an as-needed basis.
Say I request 20 profiles from a service. Instead of waiting for the service to build all 20, the service would build the first profile and throw it back down to the client until all 20 are created.
I've been using AFNetworking and would like to continue using it. Eventually I'd like to contribute this component back to the community if it requires an addition.
Anyone have any ideas on tackling something like this? Right now I have a service pushing JSON every few seconds to test with.
A couple of thoughts:
If you want to open a connection and respond to transmissions from the server, socket-based model seems to make sense. See Ray Wenderlich's How To Create A Socket Based iPhone App and Server for an example (the server-side stuff is likely to change based upon your server architecture, but it gives you an example). But AFNetworking is built on a NSURLConnection framework, not a socket framework, so if you wanted to integrate your socket classes into that framework, a non-inconsiderable amount of work would be involved.
Another, iOS-specific model is to use Apple's push notification service (see the push-related sections of the Local and Push Notification Programming Guide).
A third approach would be to stay with a pull mechanism, but if you're looking for a way to consume multiple feeds in a non-serial fashion would be to create multiple AFURLConnectionOperation (or the appropriate subclass) operations, and submit them concurrently (you may want to constraint maxConcurrentOperations on the queue to 4 or 5 as iOS can only have so many concurrent network operations). By issuing these concurrently, you mitigate many of the delays that result from network latencies. If you pursue this approach, some care might have to be taken for thread safety, but it's probably easier than the above two techniques.
This sounds like a job for a socket (or a web socket, whatever is easier).
I don't believe there is support for this in AF. This could be implemented in the NSURLConnection's didRecieveData method. This is triggered every time a piece of data is received, so you can do your parsing and messaging from that point. Unfortunately, I can't think of a very clean way to implement this.
Perhaps a better approach to this is to handle the appropriate rerequest via a pagination-style technique. You would request page 1 of profiles with 1/page, then request page 2, etc. You could then control the flow, i.e. if you want to request all in paralel or request one then the next sequentially. This would be less work to implement, and would (in my opinion) be cleaner and easier to maintain.
AFNetworking supports batching of requests with AFHTTPClient -enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:.
You can use this method to return on each individual operation, as well as when all of the operations in the batch have finished.

Using GCD or not?

I have an iPhone app which pretty much is a mobile app for a website. Pretty much everything it does is call API methods from our server. The app retrieves the user's information, and keeps updating the server using the API.
My colleague and I had a discussion whether to introduce GCD to the downloading aspect on the app. My colleague argues that since the UI needs to wait for the download to complete before it can display the pictures, text or whatever, there is absolutely no need for GCD. My argument is that we should keep the main thread busy with UI rendering (even if there is no data), and introduce GCD to the app to create other threads for download.
Which argument is right here? In my case, if the UI renders with no data, will there be some sort of lag? Which will yield a cleaner, sleeker and faster app?
One argument would be : what will happen when the download fails and times out because there is a problem at the server end ?
Without GCD the app will remain blocked and will crash after a time
out since the UI can not be blocked for longer than 20 seconds.
With GCD the application remains functional but there will be no data
being downloaded and the application will not crash.
Other aspects to take into account are :
the thread safety of the objects that you are using
how you handle downloads that are no longer necessary because the user navigates away from the page
I don't think doing time consuming operations in the main thread is a good idea.
Even if user have to wait for the data te be downloaded before he can do anything meaningful, still he will not hope UI is blocked.
Let's assume you have a navigator view, and after user tap some button, you push a new view to it and start download something. If user suddenly decides he don't want to wait anymore, he tap the "back" button. If your downloading operation blocks UI, user will have to wait it to end, it's really bad.
A more appropriate question would perhaps be if you should download asynchronously or on the main thread for your app, since there are several different methods to download asynchronously on iOS (e.g. using NSThread, NSOperation or indeed GCD). An easy approach to achieve your goals could be to use the AFNetworking library. It makes multithreaded networking / internet code very easy to implement and understand.
Personally I'm very fond of GCD and recommend you learn it someday soon, though in itself it is not as suitable for asynchronous downloading compared to a library like AFNetworking (that uses GCD under the hood, I believe).
Here is a good read on using NSOperationQueues (that uses GCD behind the scenes) to download images. There is also some Github code you can check out. They have an elegant solution to pause downloads and enqueue new downloads when the user moves to different parts of your app.
http://eng.alphonsolabs.com/concurrent-downloads-using-nsoperationqueues/?utm_medium=referral&utm_source=pulsenews
Use GCD / NSOperationQueues as opposed to using NSThreads. You will have a good learning on core fundamentals and at the same time create a well architectured app. :)

iOS - run multiple async requests at the same time

I'm a beginning iOS developer and in my first app I want to load data for my view controller, but the problem is that the data comes over from three different web services. I use NSMutableRequest in an attempt to implement this functionality.
What I want to do is issue three requests to load data at the same time and when the last one finishes, remove the activity indicator.
I tried using an NSOperationQueue, but with async it doesn't execute the didReceiveData or didFailWithError methods. Please do comment if you need more detail or explanation (my English is not very good).
Thanks.
I would look at GCD, and create 1 synchronous request that contains your 3 asynchronous requests to your web services. This way you let GCD do the work of waiting for each of the async operations to complete. There are a few really good videos on itunes from WWDC (Apple's developer conference) that are incredibly informative with regard to GCD.

IOS Core Data Concurrency

We are developing an IPad application with synchronization support, this synchronization logic is launched at the startup of the application to check if there are any updates by invoking a WCF service.
As the synchronization process takes time, we would like to put it on a background worker to not block the user interface, so my question is:
is it possible to develop a background worker that synchronize the sqlite database on background and on the same time the user interface is using this database? If it's possible can you please provide code or links to help me implement this?
Thank's in advance
This is definitely possible. What you are referring to is Threading where a process is done in the Background. Read up on Apple's Thread Programming Guide and Concurrency Programming Guide to get a better idea on how exactly it's implemented in iOS

Resources