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
Related
Can someone help me out, i am facing a issues since many days.
My Application is lagging(Users feel like the app is almost struck) at the launch screen while handling image data from server and saving it in the local SQLite Database. It would be great if someone could provide me with some solution for the same.
Thank You.
It is hard to figure out the cause without any code but if the user is complaining that the application is lagging, there is a good chance that you are doing work on the main thread and locking up the UI. You should do as much of this work as you can on a background thread as to not freeze the User Interface. If your app is useless unless this data is ready, you should design a flow where the application lets the user know that it is doing some work instead of just freezing the application UI.
I have an iOS app with some performance issues. In particular, when the user selects an item in a particular Core Data-backed UITableView there's a delay before the UI updates. Since there's very little multithreading in the app at the moment, I'm guessing I'm making a method call somewhere that's taking too long and blocking the UI thread.
Stepping through the code isn't revealing anything to me. (Perhaps because the problem is on another trip through the run loop? Not sure...I'm a solid iOS developer but a novice when it comes to the debugging and profiling tools.)
What's the best way to tackle a problem like this? Some way to step through or trace what the thread is doing in order and how long each call is taking, or something?
Thanks.
Check out the Time Profiler in Instruments. See WWDC 2012 - Building Concurrent User Interfaces on iOS for a practical demonstration of how you can use profiler to identify candidate methods that you might want to put in a background queue to avoid blocking the main queue.
See the Instruments User Guide for a basic description of the Instruments tool, but I think that WWDC video is a more effective way to gain familiarity with this particular instrument, the Time Profiler. Also, if you've never done concurrent Core Data, you might also want to see the Concurrency with Core Data section of the Core Data Programming Guide.
I will second what's Rob said: you will find more easily what's the bottleneck of your app using Time Profiler in Instruments. All references from Rob are good specially the one from WWDC. I also run into this tut in mobile tuts which covers the basics.
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. :)
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.
The Apple "Concurrency with Core Data" documentation states the following when discussing using core data with background threads.
Saving in a Background Thread is Error-prone
Asynchronous queues and threads do not prevent an application from
quitting. (Specifically, all NSThread-based threads are “detached”—see
the documentation for pthread for complete details—and a process runs
only until all not-detached threads have exited.)
and in particular:
If you need to save on a background thread, you must write additional code such that the main thread prevents the application from quitting until all the save operation is complete.
What is the recommended approach for achieving this inside an IOS application?
In the app delegates applicationWillTerminate and related methods, you need to check if any background threads have unsaved changes and save them before allowing the app to terminate or go into the background.
I recommend taking a look at using Magical Record (https://github.com/magicalpanda/MagicalRecord/). It greatly simplifies dealing with core data on background threads. I recently found this and used it for a project. We've now undertaken a maintenance effort to update a variety of existing apps to use the new Magical Record Core Data wrapper. It has saved us tons of time and frustration in the few weeks we have been using it.